• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Y-wee
博客园    首页    新随笔    联系   管理     

Spring Boot Admin 注册原理简单说明

Spring Boot Admin 注册原理简单说明

​ Spring Boot Admin 底层用了 spring-boot-starter-actuator 插件,相当于对 spring-boot-starter-actuator 插件的更进一步封装

如何注册

​ Client 只有注册到 Server 的注册列表中才会被监控到,Client 注册包括启动时注册和启动后注册

​ 为什么启动时进行注册了,启动后还要注册呢

​ 因为 Client 在启动时进行注册被 Server 监控只能说明 Client 在启动时刻服务处于健康状态,但是不能保证 Client 一直是健康的;所以,为了清楚的知道 Client 的服务状态,Client 每隔一段时间需要向 Server 发送一次注册请求,告诉 Server 自己的服务是健康的,还处于工作状态;这期间如果 Server 没有及时收到 Client 发送的注册请求,则认为 Client 服务不是健康的,应用处于离线状态

启动时注册

​ Client 端启动时会触发 RegistrationApplicationListener 监听器中的注册事件,该事件会调用 startRegisterTask() 方法开始注册

​ 在 startRegisterTask() 方法中实际上是调用了 registrator.register() 方法,registrator 是一个已经注入到监听器的注册器 bean

​ registrator.register() 方法中又调用一个重载方法 register(Application application, String adminUrl, boolean firstAttempt),该方法最终会通过 http 协议发送一个 post 请求;请求路径为 adminUrl,该路径实际上就是我们在 Client 中配置的 spring.boot.admin.url 的值并拼接上 "/instances";而 application 中包含的是 Client 中的服务相关信息

​ 由此可知,在 Client 向 Server 注册的过程中,实际上就是通过 Post 请求访问 Server 中的 /instances 接口并将 Client 服务相关信息作为参数传递

​ Server 接收到 Client 发送的请求,将 Client 传递的参数转换成 Registration,并将 Registration 作为新的参数调用 register(Registration registration) 方法,该方法会根据 healthUrl 生成一个 InstantanceId 并返回

​ 将 InstantanceId 里面的 id 拼接在 /instances/{id} 路径上得到一个新的 uri,并将该 uri 放入 headers 响应头返回

​ Client 从响应体中获取该 id,如果不为空则返回 true->注册成功

​ 总结:

启动后注册

​ Client 启动后每隔一段时间会向 Server 发送注册请求,请求过程跟启动时注册一样,默认间隔时间为 10s:


该时间可通过 spring.boot.admin.client.period 配置:

​ 参考文档:

​ https://www.jianshu.com/p/7ca83e87f041

​ https://blog.csdn.net/chainhou/article/details/112687002

记得快乐
posted @ 2022-11-15 17:40  Y-wee  阅读(819)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3