一. 介紹
-
官方文檔: http://codecentric.github.io/spring-boot-admin/1.5.7/(此文檔為1.5.7版本的文檔)
The applications register with our Spring Boot Admin Client (via HTTP) or are discovered using Spring Cloud ? (e.g. Eureka, Consul).
官方文檔中介紹中提到,使用Spring Boot Admin監(jiān)控,需要一個(gè)Spring Boot Admin Server服務(wù),其他被監(jiān)控的服務(wù)即為一個(gè)Spring Boot Admin Client,或者是通過Spring Cloud中的服務(wù)注冊(cè)發(fā)現(xiàn)組件如:Eureak,Consul來進(jìn)行監(jiān)控服務(wù),這樣就不需要特意設(shè)置相關(guān)被監(jiān)控的服務(wù)作為一個(gè)client,只需注冊(cè)到Eureka或者Consul中即可。
注:本文是基于Spring Cloud Eureka做為服務(wù)注冊(cè)發(fā)現(xiàn)組件來實(shí)現(xiàn)對(duì)服務(wù)的監(jiān)控。提前是存在了Eureka的服務(wù)。另:沒有使用eureka的需要在被監(jiān)控服務(wù)中引入client的jar,具體情況請(qǐng)參考上面的官方文檔。*
二.創(chuàng)建Spring Boot Admin Server服務(wù)
首先創(chuàng)建一個(gè)Spirng Boot項(xiàng)目,可以通過 http://start.spring.io/ 快速搭建。
-
添加Spring Boot Admin Server 依賴
pom.xml
<dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>1.5.7</version> </dependency> </dependencies> -
服務(wù)啟動(dòng)類添加
@EnableAdminServer注解開啟監(jiān)控@Configuration @EnableAutoConfiguration @EnableAdminServer public class SpringBootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminApplication.class, args); } } -
將Spring Boot Admin Server 注冊(cè)到Eureka
-
添加Eureka依賴
pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>-
服務(wù)啟動(dòng)類添加
@EnableDiscoveryClient開啟服務(wù)發(fā)現(xiàn)@Configuration @EnableAutoConfiguration @EnableDiscoveryClient @EnableAdminServer public class SpringBootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminApplication.class, args); } } -
添加Eureka服務(wù)注冊(cè)配置
eureka: instance: leaseRenewalIntervalInSeconds: 10 client: registryFetchIntervalSeconds: 5 serviceUrl: defaultZone: http://10.1.3.54:8761/eureka/ # 關(guān)閉spring boot actuator的安全,否則敏感路徑訪問是401 management: security: enabled: false
-
啟動(dòng)項(xiàng)目,訪問ip:port 即進(jìn)入管理的頁面
如圖:
1526718130968.png
-
圖中顯示的這些項(xiàng)目都是已經(jīng)注冊(cè)到Eureka上的服務(wù),通過將Spring Boot Admin Server注冊(cè)到Eureka上來監(jiān)控項(xiàng)目。
三.配置設(shè)置
-
登錄UI
Admin管理服務(wù)沒有任何的安全措施,任何人知道ip和port就能訪問,這就很不安全,而Spring Boot Admin也提供了登錄頁面。需要和Spring Security 配合使用。
-
添加Spring Boot Admin UI依賴:
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-login</artifactId> <version>1.5.7</version> </dependency> -
添加Spring Security依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> -
在項(xiàng)目啟動(dòng)類中添加配置代碼:
package com.aspire.springbootadmin; import de.codecentric.boot.admin.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableAutoConfiguration @EnableDiscoveryClient @EnableAdminServer public class SpringbootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringbootAdminApplication.class, args); } @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(); } } } -
添加配置文件
security: user: name: admin password: 123123 basic: enabled: false -
再次訪問會(huì)出現(xiàn)一個(gè)登陸頁面
1526721233818.png
輸入配置中配置的用戶名和密碼點(diǎn)擊Login登錄。進(jìn)入后導(dǎo)航欄上也會(huì)多出一個(gè)退出按鈕。點(diǎn)擊后即返回到登錄頁面。
1526721401960.png -
-
Client應(yīng)用的版本信息
想要在Application列表中顯示版本,使用maven的
build-info插件,在pom.xml文件添加插件<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-info</goal> </goals> </execution> </executions> </plugin> </plugins> </build>如圖:沒有添加的就沒有顯示。

-
Client應(yīng)用JMX bean管理
要在管理界面中與JMX-beans進(jìn)行交互,您必須在應(yīng)用程序中包含Jolokia。 添加pom依賴:
<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>添加后在監(jiān)控的應(yīng)用detial菜單里就可以看到JMX菜單
1526886145536.png
-
Client應(yīng)用添加日志Log
在Client應(yīng)用的properties中配置如下:
logging: path: /xxx/xxx/xxx/指定日志輸出路徑,然后就可以顯示日志了:

-
hystrix ui支持
spring boot admin還可以集成hystrix展示。 前提是Client端需要開啟hystrix才可以。
-
在Spring Boot Admin Server 添加依賴
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-hystrix</artifactId> <version>1.4.6</version> </dependency> -
在Server的配置文件中添加endpoints節(jié)點(diǎn)
spring.boot.admin.routes.endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,activiti,hystrix.stream -
點(diǎn)擊開啟了Hystrix的Client,點(diǎn)擊Hystrix菜單既能看到Hystrix信息
1526883244216.png
-
6.Turbine UI 支持
? spring boot admin還可以集成turbine展示。
-
加入依賴:
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-turbine</artifactId> <version>1.5.7</version> </dependency> -
配置turbine的cluster和location,clusters為turbin服務(wù)中配置的clusters一致,location需要指定注冊(cè)到Eureka中的Turbine服務(wù)名稱,例如你的Turbine服務(wù)名稱為TURBINE-SERVER,配置就為下所示。(其實(shí)Spring Boot Admin Server本身就可以當(dāng)做Turbine服務(wù),如果之前就存在了Turbine服務(wù)的話就可以直接在這里配置。若不存在Turbine服務(wù),就在Spring Boot Admin Server添加Turbine相關(guān)依賴和配置,這樣Spring Boot Admin Server也就成了Turbine服務(wù)。)
spring.boot.admin.turbine: clusters: default location: TURBINE-SERVER -
最后在配置文件中添加turbine.stream的endpoint
spring.boot.admin.routes.endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,activiti,hystrix.stream,turbine.stream -
界面
導(dǎo)航欄中會(huì)多出TURBINE的Tab,點(diǎn)擊就能看到Turbine信息
1526885592504.png
7.郵件通知
Spring Boot Admin 同時(shí)支持郵件通知。
-
添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> -
添加Spring郵件配置(這里記一個(gè)小坑,開始使用的是qq郵箱,結(jié)果死活連不上qq郵箱的smtp服務(wù)器,查了下原因是騰訊那邊把公司內(nèi)網(wǎng)ip給禁止了,結(jié)果連上4G網(wǎng)就沒有問題了。)
spring: mail: host: mmmail.aspire-tech.com password: xxxxx port: 25 username: xxxx -
添加Spring Boot Admin郵件配置
boot: admin: notify: mail: to: xxxx@aspirecn.com from: chufule@aspirecn.com enabled: true -
效果如圖,監(jiān)控的服務(wù)啟動(dòng),停止都會(huì)有郵件通知。
1526895078834.png
1526895103720.png







