SpringCloud 之 consul
Table of ContentsConsul 基础概念consul与eureka比较将基于SpringBoot的微服务应用注册到Consul上成为服务提供者从consul中安全注销服务实例Consul 基础概念Consul 是一个分布式高可用的系统,它包含多个组件,但作为一个整体在微服务架构中提供服务发现和服务配置的工具。Spring Cloud Consul 项目是针对...
Table of Contents
将基于SpringBoot的微服务应用注册到Consul上成为服务提供者
Consul 基础概念
Consul 是一个分布式高可用的系统,它包含多个组件,但作为一个整体在微服务架构中提供服务发现和服务配置的工具。Spring Cloud Consul 项目是针对Consul的服务治理实现
特性:
- 服务发现
- 健康检查
- key/value 存储
- 多数据中心
Consul 工作原理
\
- 当 Producer 启动的时候,会向 Consul 发送一个 post 请求,告诉 Consul 自己的 IP 和 Port
- Consul 接收到 Producer 的注册后,每隔10s(默认)会向 Producer 发送一个健康检查的请求,检验Producer是否健康
- 当 Consumer 发送 GET 方式请求 /api/address 到 Producer 时,会先从 Consul 中拿到一个存储服务 IP 和 Port 的临时表,从表中拿到 Producer 的 IP 和 Port 后再发送 GET 方式请求 /api/address
- 该临时表每隔10s会更新,只包含有通过了健康检查的 Producer
consul与eureka比较
1,Consul 强一致性(C)
服务注册相比 Eureka 会稍慢一些。因为 Consul 的 raft 协议 要求必须过半数的节点都写入成功才认为注册成功 ;raft 协议:Raft
Leader 挂掉时,重新选举期间整个 Consul 不可用。保证了强一致性但牺牲了可用性。
Eureka 保证高可用(A)和最终一致性:
服务注册相对要快,因为不需要等注册信息复制replicate到其他节点,也不保证注册信息是否 replicate 成功
当数据出现不一致时,虽然A, B上的注册信息不完全相同,但每个Eureka节点依然能够正常对外提供服务,这会出现查询服务信息时如果请求A查不到,但请求B就能查到。如此保证了可用性但牺牲了一致性。
2,Consul实现了服务端的均衡负载功能
3,eureka 就是个 servlet 程序,跑在 servlet 容器中; Consul 则是 go 编写而成。
将基于SpringBoot的微服务应用注册到Consul上成为服务提供者
1 在pom.xml文件中 添加依赖配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
2 在配置文件 application.properites ,将consul需要的配置信息加入即可
spring.application.name=spring-cloud-consul-producer
server.port=8501
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
#注册到consul的服务名称
spring.cloud.consul.discovery.serviceName=service-producer
spring.cloud.consul.discovery.serviceName 是指注册到 Consul 的服务名称,后期客户端会根据这个名称来进行服务调用。
If you use Spring Cloud Consul Config, the above values will need to be placed in bootstrap.yml instead of application.yml.
由于consul自身提供了服务端,所以我们不需要向使用eureka的时候创建服务注册中心,直接通过下载consul的服务端程序就可以使用。
注意版本问题 不同的版本可能导致 服务无法注册到consul中 或者注册失败
目前成功的版本 实验
cousul
C:\Users\KunChi>consul --version
Consul v1.4.1
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
SpringCloud
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
SpringBoot
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
Spring Boot 版本使用的是 2.0.5.RELEASE,Spring Cloud 最新版本是 Finchley.RELEASE 依赖于 Spring Boot 2.x.
需要引入的依赖
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
spring-boot-starter-actuator 健康检查依赖于此包。
spring-cloud-starter-consul-discovery Spring Cloud Consul 的支持。
从consul中安全注销服务实例
当我们在Spring Cloud应用中使用Consul来实现服务治理时,由于Consul不会自动将不可用的服务实例注销掉(deregister),这使得在实际使用过程中,可能因为一些操作失误、环境变更等原因让Consul中存在一些无效实例信息,而这些实例在Consul中会长期存在,并处于断开状态。
在Consul中提供了 Deregister接口,附上实例的id就可以成功注销实例了
(注意是实例的id,不是服务名,即服务名+一段唯一字符串。有ACL认证的Consul需要在Header上加token,否则会报permission denied)
接着看到这个服务的实例数量……难道要一个一个请求吗?
consul.png
当然不是,配合下面这个实例列表接口,批量删除吧!
代码地址:
GitHub - lixiaoming375/consul-demo: 服务发现组件 consul
参拷文件
Eureka 虽然闭源了,但注册中心还有更多选择:Consul 使用详解_纯洁的微笑-CSDN博客
《Consul踩坑记》一、Consul移除失效服务的正确姿势 - 简书
更多推荐
所有评论(0)