一. 介紹
GitHub: https://github.com/codecentric/spring-boot-admin
官方文檔: 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)控,需要一個Spring Boot Admin Server服務,其他被監(jiān)控的服務即為一個Spring Boot Admin Client,或者是通過Spring Cloud中的服務注冊發(fā)現(xiàn)組件如:Eureak,Consul來進行監(jiān)控服務,這樣就不需要特意設置相關被監(jiān)控的服務作為一個client,只需注冊到Eureka或者Consul中即可。
注:本文是基于Spring Cloud Eureka做為服務注冊發(fā)現(xiàn)組件來實現(xiàn)對服務的監(jiān)控。提前是存在了Eureka的服務。 另:沒有使用eureka的需要在被監(jiān)控服務中引入client的jar,具體情況請參考上面的官方文檔。*
二.創(chuàng)建Spring Boot Admin Server服務
首先創(chuàng)建一個Spirng Boot項目,可以通過 http://start.spring.io/ 快速搭建。
1、添加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>
2、服務啟動類添加 @EnableAdminServer
注解開啟監(jiān)控
@Configuration@EnableAutoConfiguration@EnableAdminServerpublic class SpringBootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminApplication.class, args); }}
3、將Spring Boot Admin Server 注冊到Eureka
添加Eureka依賴
pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
服務啟動類添加 @EnableDiscoveryClient
開啟服務發(fā)現(xiàn)
@Configuration@EnableAutoConfiguration@EnableDiscoveryClient@EnableAdminServerpublic class SpringBootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminApplication.class, args); }}
添加Eureka服務注冊配置
eureka: instance: leaseRenewalIntervalInSeconds: 10 client: registryFetchIntervalSeconds: 5 serviceUrl: defaultZone: http://10.1.3.54:8761/eureka/# 關閉spring boot actuator的安全,否則敏感路徑訪問是401management: security: enabled: false
啟動項目,訪問ip:port 即進入管理的頁面
如圖:
圖中顯示的這些項目都是已經注冊到Eureka上的服務,通過將Spring Boot Admin Server注冊到Eureka上來監(jiān)控項目。
三.配置設置
1、登錄UI
Admin管理服務沒有任何的安全措施,任何人知道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>
在項目啟動類中添加配置代碼:
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@EnableAdminServerpublic 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
再次訪問會出現(xiàn)一個登陸頁面
輸入配置中配置的用戶名和密碼點擊Login登錄。進入后導航欄上也會多出一個退出按鈕。點擊后即返回到登錄頁面。
2、Client應用的版本信息
想要在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>
如圖:沒有添加的就沒有顯示。
3、Client應用JMX bean管理
要在管理界面中與JMX-beans進行交互,您必須在應用程序中包含 Jolokia 。 添加pom依賴:
<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId></dependency>
添加后在監(jiān)控的應用detial菜單里就可以看到JMX菜單
4、Client應用添加日志Log
在Client應用的properties中配置如下:
logging: path: /xxx/xxx/xxx/
指定日志輸出路徑,然后就可以顯示日志了:
5、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é)點
點擊開啟了Hystrix的Client,點擊Hystrix菜單既能看到Hystrix信息
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服務中配置的clusters一致,location需要指定注冊到Eureka中的Turbine服務名稱,例如你的Turbine服務名稱為TURBINE-SERVER,配置就為下所示。(其實Spring Boot Admin Server本身就可以當做Turbine服務,如果之前就存在了Turbine服務的話就可以直接在這里配置。若不存在Turbine服務,就在Spring Boot Admin Server添加Turbine相關依賴和配置,這樣Spring Boot Admin Server也就成了Turbine服務。)
spring.boot.admin.turbine: clusters: default location: TURBINE-SERVER
最后在配置文件中添加turbine.stream的endpoint
界面
導航欄中會多出TURBINE的Tab,點擊就能看到Turbine信息
7.郵件通知
Spring Boot Admin 同時支持郵件通知。
添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId></dependency>
添加Spring郵件配置(這里記一個小坑,開始使用的是qq郵箱,結果死活連不上qq郵箱的smtp服務器,查了下原因是騰訊那邊把公司內網ip給禁止了,結果連上4G網就沒有問題了。)
spring: mail: host: mmmail.aspire-tech.com password: xxxxx port: 25 username: xxxx
添加Spring Boot Admin郵件配置
boot: admin: notify: mail: to: [email protected] from: [email protected] enabled: true
效果如圖,監(jiān)控的服務啟動,停止都會有郵件通知。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選