Spring Cloud 之 SpringBoot Admin監控搭建(十三)


Spring Boot 有一個非常好用的監控和管理的源軟件,這個軟件就是 Spring Boot Admin。該軟件能夠將 Actuator 中的信息進行界面化的展示,也可以監控所有 Spring Boot 應用的健康狀況,提供實時警報功能。

主要的功能點有:

  • 顯示應用程序的監控狀態
  • 應用程序上下線監控
  • 查看 JVM,線程信息
  • 可視化的查看日志以及下載日志文件
  • 動態切換日志級別
  • Http 請求信息跟蹤
  • 其他功能點……


github源碼地址:https://github.com/codecentric/spring-boot-admin

 

Spring Boot Admin 是由服務端和客戶端組成,在 Spring Boot 項目中,Spring Boot Admin 作為 Server 端,其他的要被監控的應用作為 Client 端。

1、新建一個名稱為spring-boot-admin的模塊

 

2、build.gradle依賴

1 dependencies {
2     compile 'de.codecentric:spring-boot-admin-starter-server:2.0.1';
3     compile("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
4     compile("org.springframework.boot:spring-boot-starter-security")
5     compile("org.jolokia:jolokia-core")
6 }

 

3、創建啟動類

/**
 * @author Leo
 */
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@EnableEurekaClient
public class SpringBootAdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminApplication.class, args);
    }

    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().permitAll().and().csrf().disable();
        }
    }
}

 

4、bootstrap.yml配置

 1 spring:
 2   application:
 3     name: spring-boot-admin
 4 
 5 server:
 6   port: 9999
 7 
 8 eureka:
 9   client:
10     registryFetchIntervalSeconds: 5
11     serviceUrl:
12       defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
13 
14 management:
15   endpoints:
16     web:
17       exposure:
18         include: "*"
19   endpoint:
20     health:
21       show-details: ALWAYS

 

5、啟動

服務啟動后訪問:http://localhost:9999/,打開Spring Boot Admin監控平台。從下圖可以看到現在有6個應用,8個實例,狀態為全部在線。

 

 6、security配置

通常情況下,因為安全問題,我們需要給Spring Boot Admin平台設置一個登錄賬號和密碼,怎么做呢

6.1 修改配置文件

 1 spring:
 2   application:
 3     name: spring-boot-admin
 4   security:
 5     user:
 6       name: "admin"
 7       password: "123456"
 8 server:
 9   port: 9999
10 
11 eureka:
12   client:
13     registryFetchIntervalSeconds: 5
14     serviceUrl:
15       defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
16 
17 management:
18   endpoints:
19     web:
20       exposure:
21         include: "*"
22   endpoint:
23     health:
24       show-details: ALWAYS

6.2修改啟動類

 1 /**
 2  * @author Leo
 3  */
 4 @Configuration
 5 @EnableAutoConfiguration
 6 @EnableAdminServer
 7 @EnableEurekaClient
 8 public class SpringBootAdminApplication {
 9 
10     public static void main(String[] args) {
11         SpringApplication.run(SpringBootAdminApplication.class, args);
12     }
13 
14     @Configuration
15     public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
16         private final String adminContextPath;
17 
18         public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
19             this.adminContextPath = adminServerProperties.getContextPath();
20         }
21 
22         @Override
23         protected void configure(HttpSecurity http) throws Exception {
24             SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
25             successHandler.setTargetUrlParameter("redirectTo");
26 
27             http.authorizeRequests()
28                     .antMatchers(adminContextPath + "/assets/**").permitAll()
29                     .antMatchers(adminContextPath + "/login").permitAll()
30                     .anyRequest().authenticated()
31                     .and()
32                     .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
33                     .logout().logoutUrl(adminContextPath + "/logout").and()
34                     .httpBasic().and()
35                     .csrf().disable();
36         }
37     }
38 }

6.3重啟服務,訪問http://localhost:9999/,和之前不同的是多了一個登錄頁面,輸入admin/123456登錄即可。

 

 6.4 登錄后我們發現Spring Boot Admin本身處於下線狀態,如下圖

 

 6.5 修改配置

 1 spring:
 2   application:
 3     name: spring-boot-admin
 4   security:
 5     user:
 6       name: "admin"
 7       password: "123456"
 8 server:
 9   port: 9999
10 
11 eureka:
12   client:
13     registryFetchIntervalSeconds: 5
14     serviceUrl:
15       defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
16   instance:
17     metadata-map:
18       user.name: "admin"
19       user.password: "123456"
20 
21 management:
22   endpoints:
23     web:
24       exposure:
25         include: "*"
26   endpoint:
27     health:
28       show-details: ALWAYS

6.6 重啟Admin后再次登錄

 

 

服務全部正常。

 

再上幾個監控頁面

 

 

 

 

 

 

大家可以自己研究一下。


免責聲明!

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



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