Spring Cloud开发实践(六): 基于Consul和Spring Cloud 2021.0的演示项目
2023/5/18 11:22:10
本文主要是介绍Spring Cloud开发实践(六): 基于Consul和Spring Cloud 2021.0的演示项目,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录
- Spring Cloud开发实践(一): 简介和根模块
- Spring Cloud开发实践(二): Eureka服务和接口定义
- Spring Cloud开发实践(三): 接口实现和下游调用
- Spring Cloud开发实践(四): Docker部署
- Spring Cloud开发实践(五): Consul - 服务注册的另一个选择
- Spring Cloud开发实践(六): 基于Consul和Spring Cloud 2021.0的演示项目
Consul 服务
启动Consul服务, 在Win10下可以执行以下命令, 或者存成bat文件运行, 保持窗口打开
consul agent -dev -client=0.0.0.0 -data-dir .\ -advertise 127.0.0.1 -ui -config-dir .\
浏览器访问 http://127.0.0.1:8500 , 用于观察后面注册的Node和Health情况
Spring Cloud 项目
这个演示项目使用的 Spring Boot 和 Spring Cloud 都不是最新版本, 因为最新版本最低要求 JDK17. 这里选择的是对应 JDK11 可用的最高版本, 各组件版本明细为
- Consul 1.15
- JDK 11
- Spring Boot 2.7.11
- Spring Cloud 2021.0.6
整体结构
这个用于演示的项目名称为 Dummy, 包含3个子模块, 分别是 dummy-common-api, dummy-common-impl 和 dummy-admin, 其中
- dummy-common-api 和 dummy-common-impl 逻辑上属于同一个模块 dummy-common. api 是对外输出的接口, impl是对应的实现
- dummy-admin 依赖 dummy-common-api , 使用其提供的接口
打包后, 需要部署的是两个jar: dummy-common.jar 和 dummy-admin.jar, 前者提供服务接口, 后者消费前者提供的接口, 并对外(例如前端, 小程序, APP)提供接口
项目的整体结构如下
│ pom.xml ├───dummy-admin │ │ pom.xml │ ├───src │ │ ├───main │ │ │ ├───java │ │ │ └───resources │ │ │ application.yml │ │ └───test │ └───target ├───dummy-common-api │ │ pom.xml │ ├───src │ │ ├───main │ │ │ ├───java │ │ │ └───resources │ │ └───test │ └───target └───dummy-common-impl │ pom.xml ├───src │ ├───main │ │ ├───java │ │ └───resources │ │ application.yml │ └───test └───target
根模块 Dummy
根模块的 pom.xml 中,
- 定义了子模块, module标签中的内容, 要和子模块目录名一致.
- 设置JDK版本 11
- 引入全局 Spring Boot Dependencies, 版本 2.7.11
- 引入全局 Spring Cloud Dependencies, 版本 2021.0.6
- 还有一些是Plugin相关的版本, 略
<?xml version="1.0" encoding="UTF-8"?> ... <name>Dummy: Root</name> <modules> <module>dummy-common-api</module> <module>dummy-common-impl</module> <module>dummy-admin</module> </modules> <properties> <!-- Global encoding --> <project.jdk.version>11</project.jdk.version> <project.source.encoding>UTF-8</project.source.encoding> <!-- Global dependency versions --> <spring-boot.version>2.7.11</spring-boot.version> <spring-cloud.version>2021.0.6</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <!-- Spring Boot Dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Spring Cloud Dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> ... </build> </project>
Dummy Common API 模块
这个模块用于生成依赖的jar包, 作用非常重要. 以下详细说明
pom.xml 中除了定义和父模块的关系, 需要引入 openfeign
<?xml version="1.0" encoding="UTF-8"?> ... <parent> <groupId>com.rockbb.test</groupId> <artifactId>dummy</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>dummy-common-api</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>Dummy: Commons API</name> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ... </dependencies> <build> ... </build> </project>
定义一个 UserDTO, 这个是用于传输的数据对象
@Data public class UserDTO implements Serializable { private Long id; private String name; }
对应的服务接口. 这里用到了 @FeignClient 注解
- @FeignClient 是给 dummy-admin 模块用的
- name= CommonConstant.SERVICE_NAME 就是 "dummy-common", 因为这个API模块中所有Service接口都使用同样的名称, 这边做成常量
- contextId = "userDTOService" 如果不加这个参数, 多个 FeignClient 使用同样的 name 时, 就会冲突. 这个一般直接定义为这个 service 的bean名称
- path = "/userDTOService" 用于指定当前类中所有接口的请求前缀. 在更早的版本中, 可以将 @RequestMapping 和 @FeignClient 联用, 这个是定义在 @RequestMapping 中的, 后来不允许了, 因为有安全风险.
- @GetMapping 和 @PostMapping 同时用于 dummy-admin 和 dummy-common
- 对于 dummy-admin, 这就是 FeignClient 的请求路径
- 对于 dummy-common, 这就是 Contoller 方法的服务路径
- 需要注意 @GetMapping 请求的接口形式, 必须显式添加 @RequestParam("id") 这类 GET 模式的参数注解, 否则使用 @GetMapping 的 Feign 请求也会被转为 POST 而导致请求错误.
@FeignClient(name = CommonConstant.SERVICE_NAME, contextId = "userDTOService", path = "/userDTOService") public interface UserDTOService { @GetMapping("/get") UserDTO get(@RequestParam("id") long id); @PostMapping("/add") int add(@RequestBody UserDTO dto); }
在 dummy-admin 中, 这个接口会被实例化为 feign 代理, 在模块中可以像普通 service 一样调用, 而在 dummy-common 中, 不引入 feign 依赖, 或者在 @EnableFeignClients 的 basePackages 中避开本包路径, 就会忽略这个注解, 从而实现模块间接口的关联.
与现在很多 Spring Cloud 项目中单独拆出一个 Service 模块的做法, 这种实现有很多的优点
- 开发过程友好. 与单机开发几乎一样的代码量, 唯一区别是要注意 Get 和 Post 对请求参数的格式和个数的约束
- 易重构易扩展. 可以借助 IDE 的代码分析能力, 改动自动标红, 避免人为错误和遗漏
- 性能开销小, 如果 DTO 直接映射到数据库字段, 可以全程使用一个类.
Dummy Common Impl 模块
模块的 pom.xml
- 引入 spring-boot-starter-web, 因为要提供 RestController 的能力
- 引入 spring-cloud-starter-consul-discovery 或 spring-cloud-starter-consul-all, 因为要接 Consul
- 引入 dummy-common-api 依赖, 因为 Controller 请求定义在 API 中
- 打包使用 spring-boot-maven-plugin 的 repackage, 因为要打 fat jar, 在服务器上实现单包部署
<?xml version="1.0" encoding="UTF-8"?> ... <name>Dummy: Common Implementation</name> <dependencies> <!-- Spring Boot Dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Cloud Dependencies consul-discovery 和 consul-all 二选一 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-all</artifactId> </dependency> ... <dependency> <groupId>com.rockbb.test</groupId> <artifactId>dummy-common-api</artifactId> <version>${project.version}</version> </dependency> </dependencies> <build> <finalName>dummy-common</finalName> <resources> ... </resources> <plugins> ... <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
配置部分 application.yml
- 定义服务端口 8762
- 定义 servlet 路径, 必须定义, 否则不会配置 Controller 请求
- spring.application.name: dummy-common 定义了本服务的名称, 这个名称就是在 FeignClient 中引用的服务名称, 需要与 FeignClient 中的值一致
- spring.config.import 如果使用这个设置, 依赖要使用 consul-all, 因为 consul-discovery 中不带 consul-config. 使用这个设置后, 会自动使用默认的 Consul 地址和端口
- cloud.consul.host 和 port 如果使用了config.import, 在这里可以修改默认的值, 如果不使用config.import, 则必须配置 host 和 port, 依赖可以换成 consul-discovery
- cloud.consul.discovery.health-check-path 用于更改默认的 health 检查请求路径, 默认的是 /actuator/health, 这里改为 /health
- cloud.consul.discovery.instance-id 用于定义当前实例在 Consul 里的实例ID. 默认使用 application.name-port, 如果正好这个服务在两个服务器上分别跑了一个实例, 且实例端口一样, 就会产生冲突, 可以改为 application.name-[随机串] 的形式避免冲突
server: port: 8762 tomcat: uri-encoding: UTF-8 servlet: context-path: / spring: application: name: dummy-common config: import: 'optional:consul:' #This will connect to the Consul Agent at the default location of "http://localhost:8500" # cloud: # consul: # host: 127.0.0.1 # port: 8500 # discovery: # health-check-path: /health # replace the default /actuator/health # instance-id: ${spring.application.name}:${random.value}
代码部分, 首先是实现 health 检查的处理方法, 这部分是普通的 RestController 方法. 返回字符串可以任意指定, 只要返回的 code 是 200 就可以
@RestController public class HealthCheckServiceImpl { @GetMapping("/health") public String get() { return "SUCCESS"; } }
服务接口的实现类, 这里实现了两个接口方法 get 和 add
- 使用 @RestController 注解, 与 API Service 中方法上的 @GetMapping 和 @PostMapping 配合, 将 Service 方法映射为 Controller 方法
- 在类上的 @RequestMapping("userDTOService") 方法是必须的, 因为在 API Service 中与 @FeignClient 冲突无法定义, 只能在这里定义
- 方法和参数上除了 @Override 不需要任何注解, 因为都在 API Service 上定义过了. 这里加上注解也没问题, 但是要手工保持一致.
@RestController @RequestMapping("userDTOService") public class UserDTOServiceImpl implements UserDTOService { @Autowired private UserRepo userRepo; @Override public UserDTO get(long id) { log.debug("Get user: {}", id); UserDTO user = new UserDTO(); user.setId(id); user.setName("dummy"); return user; } @Override public int add(UserDTO dto) { log.debug("Add user: {}", dto.getName()); return 0; } }
dummy-common 模块运行后会将接口注册到 Consul, 启动后注意观察两部分:
- Consul 的日志输出和控制面板显示, 在-dev模式下, 节点注册后 Consul 日志会显示模块的名称和心跳检测记录, 面板上会显示新的 Node
- Consul 控制面板中显示的 Health Checks 是否正常, 如果不正常, 需要检查 /health 路径为什么访问失败
Dummy Admin 模块
dummy-admin 是调用接口, 并对外提供服务的模块
pom.xml 和 dummy-common 基本一样, 因为都要连接 Consul, 都要提供 Controller 方法
<?xml version="1.0" encoding="UTF-8"?> ... <name>Dummy: Admin API</name> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>com.rockbb.test</groupId> <artifactId>dummy-common-api</artifactId> <version>${project.version}</version> </dependency> </dependencies> <build> <finalName>dummy-admin</finalName> <resources> ... </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> ... </plugins> </build> </project>
在主应用入口, 除了 @SpringBootApplication 以外, 还需要增加两个注解
- @EnableDiscoveryClient(autoRegister=false) 连接到 Consul 并使用服务发现, 默认会将当前节点也注册到 Consul 作为服务. 对于纯消费节点, 不对其它节点提供接口的, 使用 autoRegister=false 可以避免将自己注册到 Consul
- @EnableFeignClients(basePackages = {"com.rockbb.test.dummy.common.api"}) 扫描对应的包, 对 @FeignClient 注解实例化接口代理
/* Attach to discovery service without registering itself */ @EnableDiscoveryClient(autoRegister=false) @EnableFeignClients(basePackages = {"com.rockbb.test.dummy.common.api"}) @SpringBootApplication public class AdminApp { public static void main(String[] args) { SpringApplication.run(AdminApp.class, args); } }
在调用方法的地方, 按普通 Service 注入和调用
@Slf4j @RestController public class IndexController { @Autowired private UserDTOService userDTOService; @GetMapping(value = "/user_get") public String doGetUser() { UserDTO user = userDTOService.get(100L); return user.getId() + ":" + user.getName(); } @GetMapping(value = "/user_add") public String doAddUser() { UserDTO user = new UserDTO(); user.setName("foobar"); int result = userDTOService.add(user); return String.valueOf(result); }
可以通过注入的 DiscoveryClient 对象, 查看对应服务的服务地址(一般不需要)
@Autowired private DiscoveryClient discoveryClient; @GetMapping("/services") public Optional<URI> serviceURL() { return discoveryClient.getInstances(CommonConstant.SERVICE_NAME) .stream() .map(ServiceInstance::getUri) .findFirst(); }
参考
- Consul和Spring Boot非常详细的配置教程 https://medium.com/javarevisited/hands-on-consul-with-spring-boot-1ebf2918165c
这篇关于Spring Cloud开发实践(六): 基于Consul和Spring Cloud 2021.0的演示项目的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-01为什么公共事业机构会偏爱 TiDB :TiDB 数据库在某省妇幼健康管理系统的应用
- 2024-04-26敏捷开发:想要快速交付就必须舍弃产品质量?
- 2024-04-26静态代码分析的这些好处,我竟然都不知道?
- 2024-04-26你在测试金字塔的哪一层?(下)
- 2024-04-26快刀斩乱麻,DevOps让代码评审也自动起来
- 2024-04-262024年最好用的10款ER图神器!
- 2024-04-2203-为啥大模型LLM还没能完全替代你?
- 2024-04-2101-大语言模型发展
- 2024-04-17基于SpringWeb MultipartFile文件上传、下载功能
- 2024-04-14个人开发者,Spring Boot 项目如何部署