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

0x00 摘要

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

0x01 如何使用

如何使用?

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

@EnableAdminServer的实现

@Import(AdminServerImportSelector.class) ----- 引入Spring Boot Admin的自动配置类
@EnableZuulServer -------------------------- Spring Boot Admin做了个gateway
public @interface EnableAdminServer {
}

@Import(AdminServerImportSelector.class)

其作用是导出配置:

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

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

@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AdminController {
}

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

[记录点滴]Spring Boot Admin源码分析笔记的更多相关文章

  1. Spring Boot缓存源码分析

    前言 项目里面要增加一个应用缓存,原本想着要怎么怎么来整合ehcache和springboot,做好准备配置这个配置那个,结果只需要做三件事: pom依赖 写好一个ehcache的配置文件 在boot ...

  2. spring boot 入口源码分析

    public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); / ...

  3. spring boot admin 源码包的编译

    https://github.com/codecentric/spring-boot-admin 下载地址: 编译要求: Build Requirements: Node.js v8.x (LTS) ...

  4. Spring的IOC源码分析

    Spring IOC 容器源码分析 Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不 ...

  5. Spring IOC 容器源码分析 - 循环依赖的解决办法

    1. 简介 本文,我们来看一下 Spring 是如何解决循环依赖问题的.在本篇文章中,我会首先向大家介绍一下什么是循环依赖.然后,进入源码分析阶段.为了更好的说明 Spring 解决循环依赖的办法,我 ...

  6. Spring IOC 容器源码分析 - 创建单例 bean 的过程

    1. 简介 在上一篇文章中,我比较详细的分析了获取 bean 的方法,也就是getBean(String)的实现逻辑.对于已实例化好的单例 bean,getBean(String) 方法并不会再一次去 ...

  7. Spring IOC 容器源码分析 - 获取单例 bean

    1. 简介 为了写 Spring IOC 容器源码分析系列的文章,我特地写了一篇 Spring IOC 容器的导读文章.在导读一文中,我介绍了 Spring 的一些特性以及阅读 Spring 源码的一 ...

  8. spring boot @Value源码解析

    Spring boot 的@Value只能用于bean中,在bean的实例化时,会给@Value的属性赋值:如下面的例子: @SpringBootApplication @Slf4j public c ...

  9. Spring Developer Tools 源码分析:三、重启自动配置'

    接上文 Spring Developer Tools 源码分析:二.类路径监控,接下来看看前面提到的这些类是如何配置,如何启动的. spring-boot-devtools 使用了 Spring Bo ...

  10. Spring Developer Tools 源码分析:二、类路径监控

    在 Spring Developer Tools 源码分析一中介绍了 devtools 提供的文件监控实现,在第二部分中,我们将会使用第一部分提供的目录监控功能,实现对开发环境中 classpath ...

随机推荐

  1. 常见Java面试题 – 第三部分:重载(overloading)与重写(overriding)

    ImportNew注: 本文是ImportNew编译整理的Java面试题系列文章之一.你可以从这里查看全部的Java面试系列. 这篇文章介绍的常见面试题是关于重载(overloading)方法和重写( ...

  2. golang之gRPC

    相关链接: grpc: https://grpc.io/docs/languages/go/quickstart/ protobuf: https://protobuf.dev/programming ...

  3. k8s之常用命令

    Minikube # 启动集群服务 minikube start # 查看集群服务状态 minikube status# 登录集群minikube ssh # 关闭,暂停,恢复,删除集群服务 mini ...

  4. DBeaver 不错大家都来用 DBeaver 吧

    支持 windows linux 支持 pg 等 n 多数据库

  5. qemu/kvm

    <domain type="kvm"> <name>win7</name> <uuid>e31c1621-b3c3-42ed-be3 ...

  6. Qt/C++音视频开发73-高效滤镜/文字水印/图形水印/图片水印/yolo运算后的结果显示到画面中

    一.前言 视频监控系统发展到今天,越来越智能,比如这些年流行的人脸识别.物体识别.烟感识别等,都是需要拿到图片数据去做运算处理,然后将结果显示到视频画面中,或者还有要求将结果保存到视频录像文件中,以便 ...

  7. Qt/C++编写跨平台的推流工具(支持win/linux/mac/嵌入式linux/安卓等)

    一.前言 跨平台的推流工具当属OBS最牛逼,功能也是最强大的,唯一的遗憾就是多路推流需要用到插件,而且CPU占用比较高,默认OBS的规则是将对应画布中的视频画面和设定的音频一起重新编码再推流,意味着肯 ...

  8. Qt编写地图综合应用56-实时动态轨迹

    一.前言 实时动态轨迹经历过很多个版本的迭代,此功能最初是一个客户定制的,主要是需要在地图上动态显示GPS的运动轨迹,有个应用场景就是一个带有监控的车子,实时在运动中,后台可以接收到经纬度信息,需要绘 ...

  9. Qt开源作品15-视频监控画面

    一.前言 视频监控系统在整个安防领域,已经做到了烂大街的程序,全国起码几百家公司做过类似的系统,当然这一方面的需求量也是非常旺盛的,各种定制化的需求越来越多,尤其是这几年借着人脸识别的东风,发展更加迅 ...

  10. Spring+MyBatis企业应用实战(第二版)2018-电子书+源码+SQL脚本

    Spring+MyBatis企业应用实战(第二版)2018学习资料: 电子书: 链接:https://pan.baidu.com/s/1yAdlA5F_HuZMbO4who5jVw 提取码:58yz ...