目录

Life in Flow

知不知,尚矣;不知知,病矣。
不知不知,殆矣。

X

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

1docker pull consul
2docker run --name consul -d -p 8500:8500 -p 8600:8600/udp consul

Provider 端
依赖

1		<dependency>
2			<groupId>org.springframework.cloud</groupId>
3			<artifactId>spring-cloud-starter-consul-discovery</artifactId>
4		</dependency>
5		<!-- 用户Consul 的健康监测 -->
6		<dependency>
7			<groupId>org.springframework.boot</groupId>
8			<artifactId>spring-boot-starter-actuator</artifactId>
9		</dependency>

bootstrap.properties

1spring.application.name=waiter-service

application.properties

 1spring.jpa.hibernate.ddl-auto=none
 2spring.jpa.properties.hibernate.show_sql=true
 3spring.jpa.properties.hibernate.format_sql=true
 4
 5management.endpoints.web.exposure.include=*
 6management.endpoint.health.show-details=always
 7
 8info.app.author=DigitalSonic
 9info.app.encoding=@project.build.sourceEncoding@
10
11server.port=0
12
13#consul相关配置
14spring.cloud.consul.host=localhost
15spring.cloud.consul.port=8500
16spring.cloud.consul.discovery.prefer-ip-address=true

测试

1//localhost:8500
2
3
4//dns测试 (解析域名)
5dig @127.0.0.1 -p 8600 waiter-service.service.consul

Customer 端
依赖

1		<dependency>
2			<groupId>org.springframework.cloud</groupId>
3			<artifactId>spring-cloud-starter-consul-discovery</artifactId>
4		</dependency>

bootstrap.properties

1spring.application.name=customer-service

application.properties

1server.port=0
2
3management.endpoint.health.show-details=always
4
5spring.cloud.consul.host=localhost
6spring.cloud.consul.port=8500
7spring.cloud.consul.discovery.prefer-ip-address=true

作者:Soulboy