Consul
Consul
“Consul is a distributed, highly available, and data center aware solution to connect and confifigure applications across dynamic, distributed infrastructure.” - Reference
关键特性
- 服务发现
- 健康检查
- KV 存储
- 多数据中心支持
- 安全的服务间通信(以加密的方式完成应用间的交互)
使用 Consul 提供服务发现能力
- HTTP API (以HTTP的方式做服务的注册于发现,类似Eurak)
- DNS( xxx.service.consul )
- 与 Nginx 联动,比如 ngx_http_consul_backend_module
通过 Docker 启动 Consul
docker pull consul
docker run --name consul -d -p 8500:8500 -p 8600:8600/udp consul
Provider 端
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- 用户Consul 的健康监测 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.properties
spring.application.name=waiter-service
application.properties
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
info.app.author=DigitalSonic
info.app.encoding=@project.build.sourceEncoding@
server.port=0
#consul相关配置
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.prefer-ip-address=true
测试
//localhost:8500
//dns测试 (解析域名)
dig @127.0.0.1 -p 8600 waiter-service.service.consul
Customer 端
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
bootstrap.properties
spring.application.name=customer-service
application.properties
server.port=0
management.endpoint.health.show-details=always
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.prefer-ip-address=true