什麼是Spring Boot Admin?
Spring Boot Admin 是一個管理和監控Spring Boot 應用程序的開源軟件。每個應用都認爲是一個客戶端,通過HTTP或者使用 Eureka註冊到admin server中進行展示,Spring Boot Admin UI部分使用AngularJs將數據展示在前端。
Spring Boot Admin 是一個針對spring-boot的actuator接口進行UI美化封裝的監控工具。他可以:在列表中瀏覽所有被監控spring-boot項目的基本信息,詳細的Health信息、內存信息、JVM信息、垃圾回收信息、各種配置信息(比如數據源、緩存列表和命中率)等,還可以直接修改logger的level。
Spring Boot Admin主要功能
spring boot admin爲spring boot應用提供了整合的視圖,應用的詳情視圖提供了應用本身及運行時環境(OS和JVM)運維比較關心的數據,應用的運行時信息,log輸出,metrics統計,environment和logging level實時調整,thread線程運行時狀態,trace,audit和Hystrix。 同時提供了turbine擴展插件,用於整合展示整個集羣的熔斷器信息。 在Journal模塊,可以提供整個集羣所有節點的狀態變化歷程。
Spring Boot Admin服務端搭建
一、服務端註冊到Eureka
1、引入maven依賴
<properties> <admin.version>1.5.7</admin.version> </properties> <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>${admin.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies>
2、創建啓動類
@SpringBootApplication @EnableEurekaClient @EnableAdminServer public class Monitor { private static Logger logger = LoggerFactory.getLogger(Monitor.class); public static void main(String[] args) { SpringApplication.run(Monitor.class, args); }
3、服務端註冊到Eureka配置信息
server: port: 7500 spring: application: name: ${deploy.servicename} eureka: instance: ipAddress: ${eurekaInstanceIpAddress} preferIpAddress: true name: ${deploy.servicename} metadataMap: zone: ${eurekaZone} instanceId: ${deploy.servicename}_${spring.cloud.client.ipAddress} client: serviceUrl: defaultZone: ${eurekaClientServiceUrlDefaultZone} healthcheck: enabled: true
二、服務端配置登錄
1、引入maven依賴
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-login</artifactId> <version>${admin.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2、創建http請求權限認證
@Configuration public static class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // Page with login form is served as /login.html and does a POST on /login http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll(); // The UI does a POST on /logout on logout http.logout().logoutUrl("/logout"); // The ui currently doesn't support csrf http.csrf().disable(); // Requests for the login page and the static assets are allowed http.authorizeRequests() .antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**") .permitAll(); // ... and any other request needs to be authorized http.authorizeRequests().antMatchers("/**").authenticated(); // Enable so that the clients can authenticate via HTTP basic for registering http.httpBasic(); } }
3、配置登錄用戶名和密碼
security: basic: enabled: false user: password: admin name: admin
4、登錄頁面效果圖
三、服務端集成hystrix UI展示
1、引入maven依賴
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-hystrix</artifactId> <version>${admin.version}</version> </dependency>
2、配置文件中添加endpoints節點
spring: boot: admin: routes: endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,activiti,hystrix.stream
3、效果圖展示
注:如果頁面出現loading,則可能client沒有開啓hystrix,或者client端沒有被調用
四、服務端集成turbine展示
1、引入maven依賴
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-turbine</artifactId> <version>${admin.version}</version> </dependency>
2、添加turbine相關配置信息以及turbine endpoints節點
spring: boot: admin: turbine: clusters: muse location: Muse-Turbine routes: endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,activiti,hystrix.stream,turbine.stream
3、添加turbine配置信息參數介紹
spring.boot.admin.turbine.clusters # clusters配置的內容必須和turbine服務中配置的clusters信息一致,形如turbine服務端的配置爲 turbine: aggregator: clusterConfig: muse 則spring.boot.admin.turbine.clusters填入muse
spring.boot.admin.turbine.location #註冊到Eureka中的turbine的serviceId
4、效果圖
五、郵件通知
1、添加maven依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2、在配置文件中配置郵件
spring: mail: host: xx.xx.xx port: xx username: xxxx password: xxxxx
3、配置郵件通知
boot: admin: notify: mail: to: [email protected] from: [email protected] enabled: true
配置文件正確後,當監控的服務啓動或者停止時,都會收到郵件通知
監控的服務客戶端配置
一、在spring boot admin上展示客戶端的版本和INFO信息
1、在客戶端的配置文件添加如下信息
info: name: ${deploy.servicename} description: ${service.description} version: "@project.version@"
注:其中${deploy.servicename},這個可以在pom.xml的配置,型如
<properties> <deploy.servicename>abc</deploy.servicename> </properties>
2、效果圖
二、在spring boot admin上展示客戶端的日誌信息
1、配置日誌輸出路徑
logging: path: ${logback.dir}
2、在logback.xml中,添加如下內容
<configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> </configuration>
注:這兩個配置缺一不可
3、效果圖
三、在spring boot admin上動態修改客戶端的日誌級別
1、在logback.xml文件中,配置如下內容
<configuration> <jmxConfigurator /> </configuration>
2、爲了避免因jmxConfigurator產生內存泄漏,則添加如下代碼
@WebListener public class LogContextListener implements ServletContextListener { private static Logger logger = LoggerFactory.getLogger(LogContextListener.class); @Override public void contextInitialized(ServletContextEvent sce) { } @Override public void contextDestroyed(ServletContextEvent sce) { logger.info("LoggerContext 銷燬"); LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); lc.stop(); } }
注:客戶端的啓動類上記得加上@ServletComponentScan 註解
3、效果圖
四、在spring boot admin上展示客戶端的JMX信息
1、在客戶端的pom.xml引入Jolokia包即可
<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>
2、效果圖
附錄完整的spring boot admin配置文件信息
server: port: 7500 spring: application: name: ${deploy.servicename} boot: admin: turbine: clusters: muse location: Muse-Turbine routes: endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,activiti,hystrix.stream,turbine.stream eureka: instance: ipAddress: ${eurekaInstanceIpAddress} preferIpAddress: true name: ${deploy.servicename} metadataMap: zone: ${eurekaZone} instanceId: ${deploy.servicename}_${spring.cloud.client.ipAddress} client: serviceUrl: defaultZone: ${eurekaClientServiceUrlDefaultZone} healthcheck: enabled: true logging: config: classpath:${logback_xml_file} management: security: enabled: false security: basic: enabled: false user: password: xxx name: xxxx info: name: ${deploy.servicename} description: ${service.description} version: "@project.version@"