Spring Boot Admin 的使用 2


http://blog.csdn.net/kinginblue/article/details/52132113

*******************************************

 

一、前言

spring Boot Admin 用於監控基於 Spring Boot 的應用。官方文檔在這里(v1.3.4):《Spring Boot Admin Reference Guide》

實踐的過程中,感覺這個 User Guide 結構上還是說的不太明白。所以我就大概寫一遍我的實踐過程與理解。

閱讀此文前提條件是:

  • 使用過 Maven。
  • 你跑過基於 Spring Boot 的 hello world 程序。
  • 第三節需要你會點 Spring Cloud 的 Eureka Server 配置。

二、在 Spring Boot 項目中配置

這種配置中,Spring Boot Admin 作為 Server,其他 Spring Boot 應用作為 Client,Client 把自身的信息“注冊”到 Server,我們就能在 Server 上看到“注冊”的 Spring Boot 應用的狀態信息了。

2.1、Server 端

新建一個項目

2.1.1、添加依賴

pom.xml

<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server</artifactId> <version>1.3.4</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui</artifactId> <version>1.3.4</version> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2.1.2、開啟監控

添加 @EnableAdminServer 注解開啟監控

@Configuration @EnableAutoConfiguration @EnableAdminServer public class SpringBootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminApplication.class, args); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

這里未指定 Server 運行的端口,默認是 8080,如果要指定,則需要在 application.properties 文件中設置:
application.properties

server.port=8080
  • 1
  • 1

2.2、Client 端

2.2.1、添加依賴

pom.xml

<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>1.3.4</version> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

這里的 spring-boot-admin-starter-client 會自動依賴 jolokia-core,jolokia是用於 JMX-bean 管理的。

2.2.2、觸發自動配置、指明 Server 注冊地址

application.properties

spring.boot.admin.url=http://localhost:8080
  • 1
  • 1

上面 3.1.2 中 Server 端我們使用默認的 8080 端口,所以這里聲明 Server 的地址為:http://localhost:8080

2.3、開始管理

至此,啟動 Server 端和 Client 端,在瀏覽器輸入 Server 的地址:http://localhost:8080 就能看到“注冊”進來的 Spring Boot 應用信息了。

2.4、顯示應用版本

為了在 Spring Boot Admin 的應用管理列表顯示被管理應用的版本號,你需要設置 info.version,例如使用 maven filtering:
application.properties

info.version=@project.version@
  • 1
  • 1

這里設置顯示的版本號為 Maven pom.xml 中的構建版本號。

2.5、JMX-bean管理

JMX-bean 管理需要使用第三方的 jolokia ,因為 spring-boot-admin-starter-client 會自動依賴 jolokia-core,所以這里不需要顯示依賴了,第三節的基於 Eureka 注冊發現的配置中,就需要顯示地依賴:
pom.xml

<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

2.6、Loglevel 管理

當前日志級別管理僅限 Logback,通過 JMX 實現,所以需要依賴 jolokia 。同時,還需要配置 Logback 的 JMXConfigurator
logback.xml

<configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <jmxConfigurator/> </configuration>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

這個 logback.xml 放在與 application.properties 同級的目錄就可以了,如果不配置 Logback,那么 Spring Boot Admin 就無法管理應用的日志級別。

2.7、Server 端監控自己

以上的配置,基本就可以很好工作了。
但是有一個問題,我們沒有監控作為 Server 端的 Spring Boot Admin 自身。如果要監控到 Server 自己,把 Server 端也當作是 Client 一樣來配置就可以實現了:把 2.2.1、2.2.2、2.4、2.6 的步驟在 Server 端也配置一遍。

三、在 Spring Cloud 項目的 Eureka 中配置

這里示例的 Spring Cloud 項目是使用 Eureka 來做注冊/發現的,官方 Github 示例里有基於 ConsulZookeper 的配置。

配置好之后,Spring Boot Admin 就可以管理所有注冊到 Eureka Server 的應用了,包括 Spring Boot Admin 自己(因為自己也會注冊到 Eureka Server)。

3.1、一個簡單的 Eureka Server

關於 Eureka Server 這里不做詳細介紹,只列一下配置經過:
pom.xml

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

Eureka Server 啟動類

@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

application.properties

spring.application.name=eureka-server server.port=8761
  • 1
  • 2
  • 1
  • 2

在 application.properties 同級目錄下新建 bootstrap.properties 文件:
bootstrap.properties

eureka.instance.hostname=localhost eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

此文件作用與 application.properties 幾乎樣,只是但是作用在 application context 啟動時期。原話是:like application.properties but for the bootstrap phase of an application context

以上配置表明,我們的 Eureka Server 運行在 8761 端口。服務注冊地址是:http://localhost:8761/eureka/

3.2、Server 端

官方示例:spring-boot-admin-sample-eureka

3.2.1、添加 spring-cloud-starter-eureka 依賴

pom.xml

<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server</artifactId> <version>1.3.4</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui</artifactId> <version>1.3.4</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3.2.2、添加 @EnableDiscoveryClient 開啟發現

@Configuration @EnableAutoConfiguration @EnableDiscoveryClient @EnableAdminServer public class SpringBootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminApplication.class, args); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3.2.3、指明去哪注冊

application.properties

eureka.instance.client.serviceUrl.defaultZone: http://localhost:8761/eureka/
  • 1
  • 1

也就是我們在 3.1 中配置的 Eureka Server 服務地址。

這個配置我測試時並不成功,改為 eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/ 才可以,不知為何。

3.2.4、官方未說明的

3.2.1 ~ 3.2.3 的配置,會把 Server 注冊到 Eureka Server,也就是說 Spring Boot Admin 也可以管理自身,但現在的 Server 配置還不全面(比如自身還缺的配置有:版本信息、 JMX 管理和 Loglevel 管理)。加上以下配置:
application.properties

info.version=@project.version@
  • 1
  • 1

pom.xml

<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

logback.xml

<configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <jmxConfigurator/> </configuration>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

3.3、Client 端

Client 端的配置主要是把自己注冊到 Eureka Server 中就可以被 Spring Boot Admin 管理了,免去了手工配置 Spring Boot Admin 服務地址的操作(即 2.2.2 節操作)。

3.3.1、依賴

pom.xml

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意要添加 spring-boot-starter-actuator 依賴,因為獲取應用信息是通過 actuator 中的相關 endpoints 獲取的。

之所以 Server 端不需要添加此依賴,是因為 spring-boot-admin-server 依賴於 spring-boot-admin-starter-client ,而 spring-boot-admin-starter-client 依賴於 spring-boot-starter-actuator

3.3.2、啟動類

@SpringBootApplication @EnableEurekaClient public class ClientEurekaSampleApplication { public static void main(String[] args) { SpringApplication.run(ClientEurekaSampleApplication.class, args); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

添加 @EnableDiscoveryClient@EnableEurekaClient 注解到啟動類上,將自己注冊到 Erueka Server。

3.3.3、指明去哪注冊

bootstrap.properties

eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/
  • 1
  • 1

3.3.4、其他項配置

application.properties

info.version=@project.version@
  • 1
  • 1

logback.xml

<configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <jmxConfigurator/> </configuration>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

pom.xml

<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

四、通知

官方提供好幾種通知方式,這里貼一下郵件通知的配置,其他 PagerdutyHipchatSlackReminder 的通知配置請參見官方文檔。

使用 spring-boot-starter-mail 依賴配置 JavaMailSender

pom.xml

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

application.properties

spring.mail.host=smtp.example.com spring.boot.admin.notify.mail.to=admin@example.com
  • 1
  • 2
  • 1
  • 2

表格:郵件配置選項

Property name Description Default value 中文說明
spring.boot.admin.notify.mail.enabled Enable mail notifications true 默認啟用
spring.boot.admin.notify.mail.ignore-changes Comma-delimited list of status changes to be ignored. Format: “:”. Wildcards allowed. “UNKNOWN:UP” 需要忽略的狀態改變通知,逗號分隔
spring.boot.admin.notify.mail.to Comma-delimited list of mail recipients “root@localhost” 接收通知的郵箱地址,逗號分隔
spring.boot.admin.notify.mail.cc Comma-delimited list of carbon-copy recipients   抄送
spring.boot.admin.notify.mail.from Mail sender   發送人
spring.boot.admin.notify.mail.subject Mail subject. SpEL-expressions are supported “#{application.name} (#{application.id}) is #{to.status}” 主題
spring.boot.admin.notify.mail.text Mail body. SpEL-expressions are supported “#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}” 內容

五、附:Spring Boot Admin Server 配置說明

表格:Spring Boot Admin Server 配置選項

Property name Description Default value 中文說明
spring.boot.admin.context-path The context-path prefixes the path where the Admin Server’s statics assets and API should be served. Relative to the Dispatcher-Servlet.   Admin Server 保留的靜態訪問和API的前綴(當你在業務應用中使用而不是單獨使用時就很有必要了)
spring.boot.admin.monitor.period Time interval in ms to update the status of applications with expired status-informations. 10.000 更新應用信息的頻率,單位毫秒
spring.boot.admin.monitor.status-lifetime Lifetime of application statuses in ms. The applications /health-endpoint will not be queried until the lifetime has expired. 10.000 被監控的應用信息的過期時間,單位毫秒

5.1、Spring Cloud 對自動發現的支持

來自被發現的應用的狀態信息是經過 ServiceInstanceConverter 轉換過的,自動配置時,使用了 Spring Boot Admin 自帶的 Eureka 轉換實現。你也可以實現相關接口並並添加到上下文以替換默認的。

表格:注冊發現配置選項

Property name Description Default value 中文說明
spring.boot.admin.discovery.enabled Enables the DiscoveryClient-support for the admin server. true 默認開啟
spring.boot.admin.discovery.converter.management-context-path Will be appended to the service-url of the discovered service when the managment-url is converted by the DefaultServiceInstanceConverter.    
spring.boot.admin.discovery.converter.health-endpoint Will be appended to the management-url of the discovered service when the health-url is converted by the DefaultServiceInstanceConverter. “health”  
spring.boot.admin.discovery.ignored-services This services will be ignored when using discovery and not registered as application.    

六、附:Spring Boot Admin Client 配置說明

Spring Boot Admin Client 注冊到 Spring Boot Admin Server,Client 定期地發送 Http Post 到 admin 提供自己的應用信息。如果需要管理 loglevels 或 JMX-beans ,則要在依賴中添加 Jolokia ,使得 JMX-beans 也可以通過 http 訪問。

表格:Spring Boot Admin Client配置選項

Property name Description Default value 中文說明
spring.boot.admin.client.enabled Enables the Spring Boot Admin Client. true 默認開啟
spring.boot.admin.url List of URLs of the Spring Boot Admin server to register at. This triggers the AutoConfiguration. Mandatory.   admin server 的地址列表,此設置會觸發自動配置,必須
spring.boot.admin.api-path Http-path of registration endpoint at your admin server. “api/applications” 注冊到 admin server 端點的 Http-path
spring.boot.admin.username spring.boot.admin.password Username and password for http-basic authentication. If set the registration uses http-basic-authentication when registering at the admin server.   注冊到 admin server 的賬號密碼
spring.boot.admin.period Interval for repeating the registration (in ms). 10.000 重試注冊的間隔時間
spring.boot.admin.auto-registration If set to true the periodic task to register the application is automatically scheduled after the application is ready. true 應用啟動后自動執行周期性的注冊任務
spring.boot.admin.auto-deregistration Switch to enable auto-deregistration at Spring Boot Admin server when context is closed. false 當應用關閉時,自動取消注冊
spring.boot.admin.client.health-url Client-health-url to register with. Can be overridden in case the reachable URL is different (e.g. Docker). Must be unique in registry. Guessed based on management-url and endpoints.health.id.  
spring.boot.admin.client.management-url Client-management-url to register with. Can be overridden in case the reachable url is different (e.g. Docker). Guessed based on service-url, server.servlet-path, management.port and management.context-path.  
spring.boot.admin.client.service-url Client-service-url to register with. Can be overridden in case the reachable url is different (e.g. Docker). Guessed based on hostname, server.port and server.context-path.  
spring.boot.admin.client.name Name to register with. ${spring.application.name} if set, “spring-boot-application” otherwise. 注冊時的名字
spring.boot.admin.client.prefer-ip Use the ip-address rather then the hostname in the guessed urls. If server.address / management.address is set, it get used. Otherwise the IP address returned from InetAddress.getLocalHost() gets used. false  

七、問答

這部分我也啰嗦一下翻譯出來。

    • 我可以把 spring-boot-admin 添加到我的業務應用中嗎?
      答:可以,但不應該這么做。你可以設置 spring.boot.admin.context-path 來改變 admin server 保留的 UI 和 REST-API 的訪問,取決於你的應用復雜性,你可能會陷入困境。另一方面,當你的應用掛掉后,你的監控也一起掛掉,那么要監控有什么用呢?

    • 該怎么自定義 UI ?
      答:修改 UI 你僅可以復制並修改 spring-boot-admin-ui,並添加你自己的模塊到 classpath 中。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM