一、增加 ecs-impl 项目依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-openfeign</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.mybatis.spring.boot</groupId>
  12. <artifactId>mybatis-spring-boot-starter</artifactId>
  13. <version>2.0.0</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>mysql</groupId>
  17. <artifactId>mysql-connector-java</artifactId>
  18. <scope>runtime</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>com.alibaba</groupId>
  22. <artifactId>druid</artifactId>
  23. <version>1.1.6</version>
  24. </dependency>
  25. </dependencies>

二、创建ec-impl-phone yml文件

创建:ec-impl-phone\src\main\resource\application.yml

  1. #端口号
  2. server:
  3. port: 8200
  4. #服务注册名称
  5. spring:
  6. application:
  7. name: phone-server
  8. #注册中心地址
  9. eureka:
  10. client:
  11. serviceUrl:
  12. defaultZone: http://localhost:8100/eureka/

三、创建 ec-impl-member yml 文件

  1. #端口号
  2. server:
  3. port: 8300
  4. #服务注册名称
  5. spring:
  6. application:
  7. name: member-server
  8. #数据库相关配置
  9. datasource:
  10. driver-class-name: com.mysql.jdbc.Driver
  11. url: jdbc:mysql://10.16.163.76/dbtest?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai
  12. username: hhyu
  13. password: Yhh920205
  14. type: com.alibaba.druid.pool.DruidDataSource
  15. #注册中心地址
  16. eureka:
  17. client:
  18. serviceUrl:
  19. defaultZone: http://localhost:8100/eureka/

四、创建会员服务处理类

  1. package com.yhh.member.controller;
  2. /**
  3. * 会员服务处理类
  4. */
  5. @RestController
  6. @RequestMapping("member/api/v1")
  7. public class MemberController {
  8. /**
  9. * 发送手机注册码
  10. * @param mobile
  11. * @return
  12. */
  13. @PostMapping("sendRegCode")
  14. public String sendRegCode(String mobile){
  15. return "";
  16. }
  17. }

五、创建手机短信服务接口

5.1 创建接口文件

image.png

5.2 增加com.yhh.phone.service.PhoneService 接口

image.png

5.3 编写Phone 接口

  1. /**
  2. * 手机短信服务接口
  3. */
  4. @RestController
  5. @RequestMapping("phone/api/v1")
  6. public interface PhoneService {
  7. @PostMapping("sendRegCode")
  8. String sendRegCode(String mobile);
  9. }

六、创建手机短信服务接口实现

6.1 创建文件

image.png

6.2 编写实现代码

  1. package com.yhh.phone.service.impl;
  2. /**
  3. * 手机服务接口的实现
  4. */
  5. @Service // 实现类注解
  6. @Transactional(rollbackFor = RuntimeException.class) //事务管理,手动捕捉的异常,发生回滚
  7. public class PhoneServiceImpl implements PhoneService {
  8. }

6.3 添加依赖、添加引用、添加实现

image.png

6.4 编写方法代码(临时)

  1. @Override
  2. public String sendRegCode(String mobile) {
  3. Random random = new Random();
  4. int code = random.nextInt(8999) + 1000;
  5. return String.valueOf(code);
  6. }

6.5 创建手机实现的微服务接口(Feign)

image.png

  1. package com.yhh.member.feign;
  2. /**
  3. * 手机服务feign客户端
  4. */
  5. @Component
  6. @FeignClient(name = "phone-server") //和 ec-impl-phone 的application.yml 中的服务注册名称相同
  7. public interface PhoneServiceFeign {
  8. @PostMapping("phone/api/v1/sendRegCode") // 指向ecs-api-phone Phoneervice
  9. String sendRegCode(@RequestParam("mobile") String mobile);
  10. }

6.6 在Member控制器中调用

image.png

七、创建启动类

7.1 创建Member 启动类

image.png

7.2 创建Phone 启动类

image.png

八、启动项目

依次执行ApplicationEureka、ApplicationMember、ApplicationPhone 微服务
image.png

九、 执行结果

访问http://localhost:8100/ 发现有PhoneServer、MemberServer 证明部署成功
image.png