前面我们在将Eureka的时候说到了替换Eureka注册中心的方案,也在上节介绍了Zookeeper替代的方案,现在我们来使用Consul来替换Eureka。
什么是Consul
Consul 是 HashiCorp 公司推出的开源产品,用于实现分布式系统的服务发现、服务隔离、服务配置,这些功能中的每一个都可以根据需要单独使用,也可以同时使用所有功能。Consul 官网目前主要推 Consul 在服务网格中的使用。
与其它分布式服务注册与发现的方案相比,Consul 的方案更“一站式”——内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value 存储、多数据中心方案,不再需要依赖其它工具。Consul 本身使用 go 语言开发,具有跨平台、运行高效等特点,也非常方便和 Docker 配合使用。
Consul 的主要特点
- Service Discovery: 服务注册与发现,Consul 的客户端可以做为一个服务注册到 Consul,也可以通过 Consul 来查找特定的服务提供者,并且根据提供的信息进行调用。
- Health Checking: Consul 客户端会定期发送一些健康检查数据和服务端进行通讯,判断客户端的状态、内存使用情况是否正常,用来监控整个集群的状态,防止服务转发到故障的服务上面。
- KV Store: Consul 还提供了一个容易使用的键值存储。这可以用来保持动态配置,协助服务协调、建立 Leader 选举,以及开发者想构造的其它一些事务。
- Secure Service Communication: Consul 可以为服务生成分布式的 TLS 证书,以建立相互的 TLS 连接。 可以使用 intentions 定义允许哪些服务进行通信。 可以使用 intentions 轻松管理服务隔离,而不是使用复杂的网络拓扑和静态防火墙规则。
- Multi Datacenter: Consul 支持开箱即用的多数据中心,这意味着用户不需要担心需要建立额外的抽象层让业务扩展到多个区域。
1.导读
本节我们需要建两个模块,Server和Client,其实也就是Producer和Consumer,通过两个模块我们来演示如何用Consul轻松的替换Eureka。并且模拟Client远程调用 Server端。
2. 新建Server项目
我们在Server端提供对外接口,供客户端项目调用
![Consul 替换Eureka的正确姿势](http://p2.ttnews.xyz/loading.gif)
2.1 maven 配置
配置里面需要引入spring-cloud-starter-consul-discovery用来配置Consul
<code><dependencies> <dependency> <groupid>org.springframework.boot/<groupid> <artifactid>spring-boot-starter-actuator/<artifactid> /<dependency> <dependency> <groupid>org.springframework.cloud/<groupid> <artifactid>spring-cloud-starter-consul-discovery/<artifactid> /<dependency> <dependency> <groupid>org.springframework.boot/<groupid> <artifactid>spring-boot-starter-web/<artifactid> /<dependency> <dependency> <groupid>com.yisu.cloud/<groupid> <artifactid>fw-cloud-feign-api/<artifactid> <version>1.0-SNAPSHOT/<version> /<dependency>/<dependencies>/<code>
2.2 Server 应用配置
配置应用的端口、应用名、以及连接Consul 所需要的的配置信息。
<code>server: port: 8861spring: application: name: fw-register-consul-server cloud: consul: host: 127.0.0.1 port: 8500 discovery: instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} #实例ID health-check-path: /actuator/health #健康检查 health-check-interval: 10s/<code>
2.3 配置对外接口
我们这里对外对外提供两个接口,一种是返回字符串类型的,一种是返回实体类型的。
<code>@RestControllerpublic class HelloWorldController{ /** * 获取字符串信息 * @return */ @GetMapping("/helloWorld") public String HelloWorld() { return "Hello World!"; } /** * 获取用户信息 * @return */ @GetMapping("/user") public User getUser() { return new User(1L,"consul","test","[email protected]","演示Consul 替换Eureka"); }}/<code>
2.4 Server 启动类
需要在启动类中加上@EnableDiscoveryClient,启动之后就可以让Consul发现
<code>@EnableDiscoveryClient@SpringBootApplicationpublic class FwRegisterConsulApplication { public static void main(String[] args) { SpringApplication.run(FwRegisterConsulApplication.class, args); }}/<code>
3. 新建Client项目
接下来新建Client 项目,用于演示Feign远程调用。对于Feign 相信已经都了解了。
![Consul 替换Eureka的正确姿势](http://p2.ttnews.xyz/loading.gif)
3.1 maven 配置
Client 的配置跟Server 端的配置相比几乎一致。均需要引入spring-cloud-starter-consul-discovery来注册Consul
<code><dependencies> <dependency> <groupid>org.springframework.boot/<groupid> <artifactid>spring-boot-starter-actuator/<artifactid> /<dependency> <dependency> <groupid>org.springframework.cloud/<groupid> <artifactid>spring-cloud-starter-consul-discovery/<artifactid> /<dependency> <dependency> <groupid>org.springframework.boot/<groupid> <artifactid>spring-boot-starter-web/<artifactid> /<dependency> <dependency> <groupid>com.yisu.cloud/<groupid> <artifactid>fw-cloud-feign-api/<artifactid> <version>1.0-SNAPSHOT/<version> /<dependency>/<dependencies>/<code>
3.2 应用配置
配置端口、应用名及连接Consul的配置信息,并且设置了健康检查。
<code>server: port: 8862spring: application: name: fw-register-consul-client cloud: consul: host: 127.0.0.1 port: 8500 discovery: instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} #实例ID health-check-path: /actuator/health #健康检查 health-check-interval: 10s main: allow-bean-definition-overriding: true #当出现相同名字的类进行注册时,准许覆盖注册/<code>
3.3 编写Feign Api
利用Feign 配置服务的远程调用,调用策略使用Feign 默认的。
<code>@FeignClient(value = "fw-register-consul-server")public interface ConsulApi { /** * 获取字符串信息 * @return */ @GetMapping("/helloWorld") String helloWorld(); /** * 获取用户信息demo * @return */ @GetMapping("/user") User getUser();}/<code>
3.4 编写Client 测试控制层
测试控制层的路由读者可以重新定义
<code>@RestControllerpublic class TestController { @Autowired private ConsulApi helloApi; /** * 获取字符串信息 * @return */ @GetMapping("/hello") public String hello() { return helloApi.helloWorld(); } /** * 获取用户信息 * @return */ @GetMapping("/user") public User user() { return helloApi.getUser(); }}/<code>
3.5 Client 启动类
加上@EnableDiscoveryClient,@EnableFeignClients用于使用服务注册和开启Feign,以上改造起来和Zookeeper 几乎一致。
<code>@EnableDiscoveryClient@SpringBootApplication@EnableFeignClientspublic class FwClientConsulApplication { public static void main(String[] args) { SpringApplication.run(FwClientConsulApplication.class, args); }}/<code>
4. 启动测试
- 启动Consul
- 启动Server
- 启动Client
可以看到服务已注册到Consul上
浏览器或Postman 输入localhost:8862/user可以看到如下结果
输入localhost:8862/hello得到
5. Consul 的调用过程
1、当 Producer启动的时候,会向 Consul发送一个 POST请求,告诉 Consul自己的 IP和 Port;
2、Consul接收到 Producer的注册后,每隔 10s(默认)会向 Producer发送一个健康检查的请求,检验 Producer是否健康;
3、当 Consumer发送 GET方式请求 /user到 Producer时,会先从 Consul中拿到一个存储服务 IP和 Port的临时表,从表中拿到 Producer的 IP和 Port后再发送 GET方式请求 /user;
4、该临时表每隔 10s 会更新,只包含有通过了健康检查的 Producer。
Spring Cloud Consul项目是针对 Consul的服务治理实现。Consul是一个分布式高可用的系统,它包含多个组件,但是作为一个整体,在微服务架构中,为我们的基础设施提供服务发现和服务配置的工具。
6. 总结
本文演示了如何使用Consul 来替换Eureka 注册中心的位置,可以看到改动点只是替换了依赖包和配置内容,代码部分并没有修改,另外Consul 和Zookeeper 一样都是CP的架构。
SpringCloud构建实战、从入门到复杂,包含eureka、zuul、gateway、feign、ribbon、hystrix、mq、turbine、nacos、elk、consul、zookeeper、rocketmq、kafka、分布式事务、jwt、SkyWalking、Zipkin、bootadmin等使用案例 https://github.com/xuyisu/fw-sping-cloud,请支持一下作者
详细教程可以点击左下角更多获取
閱讀更多 奮戰攻城獅 的文章