目的
上文提到仅使用discovery包自带的注册功能进行服务注册,但是由于监控的是 /health,使用actuator实现自由度不够,并且有些低级异常可能不完全影响服务运行,但状态依然为DOWN,导致过于监测过于严格。
可用方案
解决这个问题有两种方案:
- 自己实现health类似的接口,并且在spring.cloud.consul.discovery.health-check-path配置为自己实现的接口;
- 通过consul配置文件的方式进行注册,并且把spring.cloud.consul.discovery.register设置为false不自动进行注册;
本文说明第二种方式。
实施
配置环境同上一篇文章: 定义consul的服务配置,配置如下(为了简单就做HTTP监测,实际上可以使用脚本进行健康监测更为灵活):
{
"services": [
{
"name": "spring-boot-consul-service",
"port": 17003,
"checks": [
{
"interval": "10s",
"http": "http://192.168.0.185:17003/service/hi",
"timeout": "1s"
}
],
"enable_tag_override": false,
"address": "192.168.0.185",
"id": "spring-boot-consul-service"
}
]
}
启动consul和spring-boot-consul-service服务后,可以看到服务已经正常注册上去
然后调用接口( http://192.168.0.184:8500/v1/catalog/service/spring-boot-consul-service ) 查看服务状态,显示如下,可以看到三个服务都已经注册了
[
{
"ID": "e88608f3-93d0-cfac-e0fe-8ffc2c1cdef0",
"Node": "agent-4",
"Address": "192.168.0.184",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "192.168.0.184",
"wan": "192.168.0.184"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceID": "spring-boot-consul-service",
"ServiceName": "spring-boot-consul-service",
"ServiceTags": [ ],
"ServiceAddress": "192.168.0.184",
"ServicePort": 17003,
"ServiceEnableTagOverride": false,
"CreateIndex": 881,
"ModifyIndex": 881
},
{
"ID": "d6337397-b7e2-8cc7-11ef-ff36d9c1a65e",
"Node": "agent-5",
"Address": "192.168.0.185",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "192.168.0.185",
"wan": "192.168.0.185"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceID": "spring-boot-consul-service",
"ServiceName": "spring-boot-consul-service",
"ServiceTags": [ ],
"ServiceAddress": "192.168.0.185",
"ServicePort": 17003,
"ServiceEnableTagOverride": false,
"CreateIndex": 885,
"ModifyIndex": 885
},
{
"ID": "b40a3dc6-b735-d733-33f5-5369368870e6",
"Node": "agent-6",
"Address": "192.168.0.186",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "192.168.0.186",
"wan": "192.168.0.186"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceID": "spring-boot-consul-service",
"ServiceName": "spring-boot-consul-service",
"ServiceTags": [ ],
"ServiceAddress": "192.168.0.186",
"ServicePort": 17003,
"ServiceEnableTagOverride": false,
"CreateIndex": 887,
"ModifyIndex": 887
}
]
本地启动spring-boot-consul-client服务,访问service接口,结果如下: http://127.0.0.1:17004/service/add?a=1&b=2
/add, host:192.168.0.108, service_id:application-17004; result : 3; port:17004-------------/add, host:192.168.0.184, service_id:application-17003; result : 3; port:17003
/add, host:192.168.0.108, service_id:application-17004; result : 3; port:17004-------------/add, host:192.168.0.185, service_id:application-17003; result : 3; port:17003
/add, host:192.168.0.108, service_id:application-17004; result : 3; port:17004-------------/add, host:192.168.0.186, service_id:application-17003; result : 3; port:17003
项目地址:
https://github.com/treeyh/java-demo/tree/master/spring-boot/spring-boot-consul https://gitee.com/treeyh/java-demo/tree/master/spring-boot/spring-boot-consul
本文地址:
https://my.oschina.net/tree/blog/1600117