前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[记录点滴]Spring Boot Admin源码分析笔记

[记录点滴]Spring Boot Admin源码分析笔记

作者头像
罗西的思考
发布2020-09-07 16:42:24
1.1K0
发布2020-09-07 16:42:24
举报
文章被收录于专栏:罗西的思考罗西的思考

[记录点滴]Spring Boot Admin源码分析笔记

0x00 摘要

本文是过去使用Spring Boot Admin时候分析源码的笔记。虽然比较简单,但是也可以看出Spring Boot Admin的实现思想。

0x01 如何使用

如何使用?

在你自己application中加入 @EnableAdminServer,就可以被监控到。

@EnableAdminServer的实现

代码语言:javascript
复制
@Import(AdminServerImportSelector.class) ----- 引入Spring Boot Admin的自动配置类
@EnableZuulServer -------------------------- Spring Boot Admin做了个gateway
public @interface EnableAdminServer {
}

@Import(AdminServerImportSelector.class)

其作用是导出配置:

代码语言:javascript
复制
NotifierConfiguration, 
HazelcastStoreConfiguration, 
AdminServerCoreConfiguration, 
AdminServerWebConfiguration, 
DiscoveryClientConfiguration, 
RevereseZuulProxyConfiguration

0x02 configuration

以下是各种相关配置

  • AdminServerCoreConfiguration 生成很多基础bean
  • AdminServerWebConfiguration web相关的配置和bean,EventListener
  • DiscoveryClientConfiguration 与client发现相关的bean
  • NotifierConfiguration: NotifierListener,以及各种notify需要的配置:CompositeNotifierConfiguration,MailNotifierConfiguration。TelegramNotifierConfiguration......
  • RevereseZuulProxyConfiguration 关于zuul的配置, 比如 ApplicationHeadersFilter,SimpleHostRoutingFilter... 因为SBA默认使用@EnableZuulServer
  • HazelcastStoreConfiguration 暂时用不到,网格存储用的

0x03 几个关键类

以下是一些关键类

  • ApplicationRegistry - 主要作用是: 响应各个service client的注册,生成一个application,然后放到 ApplicationStore中。很多地方会用到,比如 RegistryController 会调用它获取application列表, 获取某一个app details。
  • ApplicationStore --- 存储application,可以从这里入手来进行mysql存储,比如自己写一个MysqlApplicationStore。
    • SimpleApplicationStore -------- 就是一个内存map
    • HazelcastApplicationStore ----- 开源的可扩展的内存数据网格
  • ApplicationIdGenerator --- 就是产生个id
  • AdminServerProperties --- 每个应用对应的属性,用来配置zuul路由

0x04 discovery

Spring Boot Admin 使用 Spring Clouds DiscoveryClient @EnableDiscoveryClient 来发现应用。 把org.springframework.cloud.client.ServiceInstance转换成Application。其中还获取了 instance.getMetadata().get(KEY_HEALTH_PATH); instance.getMetadata().get(KEY_MANAGEMENT_PATH);

ApplicationDiscoveryListener : 分别响应了以下消息来发现client

代码语言:javascript
复制
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;

如果收到了HeartbeatEvent,则在discover中,会通过 discoveryClient.getServices() 得到目前注册到eureka的服务名字列表,然后注册。

0x05 event

这是内部的各种event,发现了client之后就在内部进行消息传递,进行内部后续动作,比如updateStatus,更新application store

0x06 journal

数据结构

ApplicationEventJournal 就是用SimpleJournaledEventStore(list) 存储 ClientApplicationEvent 

Web resource

使用SseEmitter实现了推送。 所谓的Sse其实就是Server-Sent Events,即服务器推送事件,属于HTML5的一项新功能,常用于服务器主动通知客户端有相关信息的更新。其他替代方法一般有WebSocket和客户端定时轮询,前者过于复杂,后者又过于低效而笨拙。SseEmitter属于ResponseBodyEmitter的子类,可以生成text/event-stream格式的信息。 会不停地往浏览器推送最新的journal。

0x07 Model

StatusInfo, Info 这两个类都在Application中存储。

StatusUpdateApplicationListener 会在 ApplicationReadyEvent/ContextClosedEvent/ClientApplicationRegisteredEvent 各种响应中进行update。

0x08 registry

ApplicationRegistry : 主要作用就是响应各个service client的注册,生成一个application,然后放到 ApplicationStore中。

RegistryController : REST controller for controlling registration of managed applications。也用来给浏览器获取applicaiton列表,application detail。

0x09 web

AdminController : 用来注释了几个类 RegistryController, NotificationFilterController

代码语言:javascript
复制
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AdminController {
}

ApplicationOperations : Handles all rest operations invoked on a registered application. 对于info, health, 就是调用application中的对应url获取数据后转发。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • [记录点滴]Spring Boot Admin源码分析笔记
    • 0x00 摘要
      • 0x01 如何使用
        • 如何使用?
        • @EnableAdminServer的实现
        • @Import(AdminServerImportSelector.class)
      • 0x02 configuration
        • 0x03 几个关键类
          • 0x04 discovery
            • 0x05 event
              • 0x06 journal
                • 0x07 Model
                  • 0x08 registry
                    • 0x09 web
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档