[記錄點滴]Spring Boot Admin原始碼分析筆記
[記錄點滴]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獲取資料後轉發。