什麼是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@"
複制