【微服务】Spring-Boot整合Consul (自定义服务配置及健康检查)

目的

上文提到仅使用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

转载于:https://my.oschina.net/tree/blog/1600117

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值