为什么80%的码农都做不了架构师?>>>   

目的

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

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

  1. 微服务Spring Boot 整合 Redis 实现 好友关注

    文章目录 ⛅引言 一.Redis 实现好友关注 -- 关注与取消关注 二.Redis 实现好友关注 -- 共同关注功能 ⛵小结 ⛅引言 本博文参考 黑马 程序员B站 Redis课程系列 在点评项目中, ...

  2. 猿创征文 | 微服务 Spring Boot 整合Redis 实战开发解决高并发数据缓存

    文章目录 一.什么是 缓存? ⛅为什么用缓存? ⚡如何使用缓存 二.实现一个商家缓存 ⌛环境搭建 ♨️核心源码 ✅测试接口 三.采用 微服务 Spring Boot 注解开启缓存 ✂️@CacheEn ...

  3. spring boot整合SpringSecurity-03 自定义报错信息

    spring boot整合SpringSecurity 目录 spring boot整合SpringSecurity-01入门 spring boot整合SpringSecurity-02 基于Ser ...

  4. Spring Boot 整合携程Apollo 配置中心

    作者:AaronSimon blog.csdn.net/AaronSimon/article/details/83657612 Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理 ...

  5. Spring Boot整合Redis——自定义RedisSerializer

    引言 spring boot简单引入redis依赖,并使用RedisTemplate进行对象存储时,需要使存储对象实现Serializable接口,这样才能够成功将对象进行序列化. RedisTemp ...

  6. spring boot web开发------自定义springmvc配置

    1.如何自定义springmvc配置 旧的springmvc配置文件 <?xml version="1.0" encoding="UTF-8"?> ...

  7. Spring boot整合nacos注册中心/配置中心报错:java.lang.IllegalArgumentException: no server available

    1.问题描述 我是近期在使用Springboot整合nacos,由于springboot和springcloud都是用最新版本,啪的一下,很快啊,就出现问题了,于是自己把版本降下来了,年轻人不讲武德降 ...

  8. Spring Boot 整合MyBatis(23)

    Spring Boot 整合MyBatis Spring Boot 整合 Druid 引入依赖 配置 application.yml pring Boot 整合 tk.mybatis 引入依赖 配置 ...

  9. 【SpringBoot学习】28、Spring Boot 整合 TKMybatis 通用 Mapper

    文章目录 Spring Boot 整合 TKMybatis 自定义父类接口 新增接口 新增之后回显 Id 更新 删除 单查询 查询列表 计数 条件构造器 技术分享区 Spring Boot 整合 TK ...

  10. ElasticSearch教程与实战:从搭建服务到Spring Boot整合

    目录 写在前面 Elasticsearch是什么?可以解决什么问题? 关于Elasticsearch版本的选择 Elasticsearch的几个基本概念 索引(index) 类型(type) 文档(d ...

最新文章

  1. Linux下安装PCRE
  2. /etc/fstab 文件解释
  3. VS2012 无法打开文件“kernel32.lib”问题的解决办法
  4. ArcGIS Server9.2学习开发(4)——使用Toc控件
  5. Asp.NetCore轻松学-部署到 Linux 进行托管
  6. uos命令_ubuntu、debian、uos的命令
  7. 使用gp部署bginfo在客户端显示信息
  8. 需要注意的一些Mysql语句
  9. python模块大全doc_Python doc8包_程序模块 - PyPI - Python中文网
  10. sofia-sip-ua >= 1.12.12… configure: error: no usable sofia-sip; please install sofia-sip-ua devel pa
  11. matlab——diff函数
  12. ORBSLAM3 VIO初始化
  13. php获取客户端IP和服务器端IP
  14. X509 PKCS7 PKCS12
  15. 【LeetCode-1413】1431. 拥有最多糖果的孩子
  16. 高通modem侧新增AT命令
  17. linux中编辑jar包的内容
  18. 极速office(Word)如何在表格里面插入行或者列
  19. 三国杀移动版虎牢关模式神吕布技能是什么?
  20. 用Python制作动态饼图

热门文章

  1. mysql双机热备的实现
  2. php源码安全加密之PHP混淆算法.
  3. mysql-5.6 升级 5.7
  4. Nutch 二次开发之parse正文内容
  5. 最常用的css选择器及兼容性 +几个好用却不多见的 nth-child等
  6. 提权巧用RAR.EXE
  7. iphone 下拉刷新控件 EGORefreshTableHeaderView 手动显示更新
  8. VDI序曲二十一 APP-V 4.6 SP1服务器端部署
  9. Windows下搭建PHP开发环境
  10. 20189317 《网络攻防技术》 第二周作业