一、增加 ecs-impl 项目依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
</dependencies>
二、创建ec-impl-phone yml文件
创建:ec-impl-phone\src\main\resource\application.yml
#端口号
server:
port: 8200
#服务注册名称
spring:
application:
name: phone-server
#注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka/
三、创建 ec-impl-member yml 文件
#端口号
server:
port: 8300
#服务注册名称
spring:
application:
name: member-server
#数据库相关配置
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://10.16.163.76/dbtest?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai
username: hhyu
password: Yhh920205
type: com.alibaba.druid.pool.DruidDataSource
#注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka/
四、创建会员服务处理类
package com.yhh.member.controller;
/**
* 会员服务处理类
*/
@RestController
@RequestMapping("member/api/v1")
public class MemberController {
/**
* 发送手机注册码
* @param mobile
* @return
*/
@PostMapping("sendRegCode")
public String sendRegCode(String mobile){
return "";
}
}
五、创建手机短信服务接口
5.1 创建接口文件
5.2 增加com.yhh.phone.service.PhoneService 接口
5.3 编写Phone 接口
/**
* 手机短信服务接口
*/
@RestController
@RequestMapping("phone/api/v1")
public interface PhoneService {
@PostMapping("sendRegCode")
String sendRegCode(String mobile);
}
六、创建手机短信服务接口实现
6.1 创建文件
6.2 编写实现代码
package com.yhh.phone.service.impl;
/**
* 手机服务接口的实现
*/
@Service // 实现类注解
@Transactional(rollbackFor = RuntimeException.class) //事务管理,手动捕捉的异常,发生回滚
public class PhoneServiceImpl implements PhoneService {
}
6.3 添加依赖、添加引用、添加实现
6.4 编写方法代码(临时)
@Override
public String sendRegCode(String mobile) {
Random random = new Random();
int code = random.nextInt(8999) + 1000;
return String.valueOf(code);
}
6.5 创建手机实现的微服务接口(Feign)
package com.yhh.member.feign;
/**
* 手机服务feign客户端
*/
@Component
@FeignClient(name = "phone-server") //和 ec-impl-phone 的application.yml 中的服务注册名称相同
public interface PhoneServiceFeign {
@PostMapping("phone/api/v1/sendRegCode") // 指向ecs-api-phone Phoneervice
String sendRegCode(@RequestParam("mobile") String mobile);
}
6.6 在Member控制器中调用
七、创建启动类
7.1 创建Member 启动类
7.2 创建Phone 启动类
八、启动项目
依次执行ApplicationEureka、ApplicationMember、ApplicationPhone 微服务
九、 执行结果
访问http://localhost:8100/ 发现有PhoneServer、MemberServer 证明部署成功