Spring Boot Admin:微服務應用監控

  • 2019 年 10 月 31 日
  • 筆記

Spring Boot Admin 可以對SpringBoot應用的各項指標進行監控,可以作為微服務架構中的監控中心來使用,本文將對其用法進行詳細介紹。

Spring Boot Admin 簡介

SpringBoot應用可以通過Actuator來暴露應用運行過程中的各項指標,Spring Boot Admin通過這些指標來監控SpringBoot應用,然後通過圖形化介面呈現出來。Spring Boot Admin不僅可以監控單體應用,還可以和Spring Cloud的註冊中心相結合來監控微服務應用。

Spring Boot Admin 可以提供應用的以下監控資訊:

  • 監控應用運行過程中的概覽資訊;
  • 度量指標資訊,比如JVM、Tomcat及進程資訊;
  • 環境變數資訊,比如系統屬性、系統環境變數以及應用配置資訊;
  • 查看所有創建的Bean資訊;
  • 查看應用中的所有配置資訊;
  • 查看應用運行日誌資訊;
  • 查看JVM資訊;
  • 查看可以訪問的Web端點;
  • 查看HTTP跟蹤資訊。

創建admin-server模組

這裡我們創建一個admin-server模組來作為監控中心演示其功能。

  • 在pom.xml中添加相關依賴:
<dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>  </dependency>  <dependency>      <groupId>de.codecentric</groupId>      <artifactId>spring-boot-admin-starter-server</artifactId>  </dependency>
  • 在application.yml中進行配置:
spring:    application:      name: admin-server  server:    port: 9301
  • 在啟動類上添加@EnableAdminServer來啟用admin-server功能:
@EnableAdminServer  @SpringBootApplication  public class AdminServerApplication {        public static void main(String[] args) {          SpringApplication.run(AdminServerApplication.class, args);      }    }

創建admin-client模組

這裡我們創建一個admin-client模組作為客戶端註冊到admin-server。

  • 在pom.xml中添加相關依賴:
<dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>  </dependency>  <dependency>      <groupId>de.codecentric</groupId>      <artifactId>spring-boot-admin-starter-client</artifactId>  </dependency>
  • 在application.yml中進行配置:
spring:    application:      name: admin-client    boot:      admin:        client:          url: http://localhost:9301 #配置admin-server地址  server:    port: 9305  management:    endpoints:      web:        exposure:          include: '*'    endpoint:      health:        show-details: always  logging:    file: admin-client.log #添加開啟admin的日誌監控
  • 啟動admin-server和admin-client服務。

監控資訊演示

  • 訪問如下地址打開Spring Boot Admin的主頁:http://localhost:9301
  • 點擊wallboard按鈕,選擇admin-client查看監控資訊;
  • 監控資訊概覽;
  • 度量指標資訊,比如JVM、Tomcat及進程資訊;
  • 環境變數資訊,比如系統屬性、系統環境變數以及應用配置資訊;
  • 查看所有創建的Bean資訊;
  • 查看應用中的所有配置資訊;
  • 查看日誌資訊,需要添加以下配置才能開啟;
logging:    file: admin-client.log #添加開啟admin的日誌監控
  • 查看JVM資訊;
  • 查看可以訪問的Web端點;
  • 查看HTTP跟蹤資訊;

結合註冊中心使用

Spring Boot Admin結合Spring Cloud 註冊中心使用,只需將admin-server和註冊中心整合即可,admin-server 會自動從註冊中心獲取服務列表,然後挨個獲取監控資訊。這裡以Eureka註冊中心為例來介紹下該功能。

修改admin-server

  • 在pom.xml中添加相關依賴:
<dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  </dependency>
  • 在application-eureka.yml中進行配置,只需添加註冊中心配置即可:
spring:    application:      name: admin-server  server:    port: 9301  eureka:    client:      register-with-eureka: true      fetch-registry: true      service-url:        defaultZone: http://localhost:8001/eureka/
  • 在啟動類上添加@EnableDiscoveryClient來啟用服務註冊功能:
@EnableDiscoveryClient  @EnableAdminServer  @SpringBootApplication  public class AdminServerApplication {        public static void main(String[] args) {          SpringApplication.run(AdminServerApplication.class, args);      }    }

修改admin-client

  • 在pom.xml中添加相關依賴:
<dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  </dependency>
  • 在application-eureka.yml中進行配置,刪除原來的admin-server地址配置,添加註冊中心配置即可:
spring:    application:      name: admin-client  server:    port: 9305  management:    endpoints:      web:        exposure:          include: '*'    endpoint:      health:        show-details: always  logging:    file: admin-client.log #添加開啟admin的日誌監控  eureka:    client:      register-with-eureka: true      fetch-registry: true      service-url:        defaultZone: http://localhost:8001/eureka/
  • 在啟動類上添加@EnableDiscoveryClient來啟用服務註冊功能:
@EnableDiscoveryClient  @SpringBootApplication  public class AdminClientApplication {        public static void main(String[] args) {          SpringApplication.run(AdminClientApplication.class, args);      }    }

功能演示

  • 啟動eureka-server,使用application-eureka.yml配置啟動admin-server,admin-client;
  • 查看註冊中心發現服務均已註冊:http://localhost:8001/
  • 查看Spring Boot Admin 主頁發現可以看到服務資訊:http://localhost:9301

添加登錄認證

我們可以通過給admin-server添加Spring Security支援來獲得登錄認證功能。

創建admin-security-server模組

  • 在pom.xml中添加相關依賴:
<dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  </dependency>  <dependency>      <groupId>de.codecentric</groupId>      <artifactId>spring-boot-admin-starter-server</artifactId>      <version>2.1.5</version>  </dependency>  <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-security</artifactId>  </dependency>  <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>  </dependency>
  • 在application.yml中進行配置,配置登錄用戶名和密碼,忽略admin-security-server的監控資訊:
spring:    application:      name: admin-security-server    security: # 配置登錄用戶名和密碼      user:        name: macro        password: 123456    boot:  # 不顯示admin-security-server的監控資訊      admin:        discovery:          ignored-services: ${spring.application.name}  server:    port: 9301  eureka:    client:      register-with-eureka: true      fetch-registry: true      service-url:        defaultZone: http://localhost:8001/eureka/
  • 對SpringSecurity進行配置,以便admin-client可以註冊:
/**   * Created by macro on 2019/9/30.   */  @Configuration  public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {      private final String adminContextPath;        public SecuritySecureConfig(AdminServerProperties adminServerProperties) {          this.adminContextPath = adminServerProperties.getContextPath();      }        @Override      protected void configure(HttpSecurity http) throws Exception {          SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();          successHandler.setTargetUrlParameter("redirectTo");          successHandler.setDefaultTargetUrl(adminContextPath + "/");            http.authorizeRequests()                  //1.配置所有靜態資源和登錄頁可以公開訪問                  .antMatchers(adminContextPath + "/assets/**").permitAll()                  .antMatchers(adminContextPath + "/login").permitAll()                  .anyRequest().authenticated()                  .and()                  //2.配置登錄和登出路徑                  .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()                  .logout().logoutUrl(adminContextPath + "/logout").and()                  //3.開啟http basic支援,admin-client註冊時需要使用                  .httpBasic().and()                  .csrf()                  //4.開啟基於cookie的csrf保護                  .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())                  //5.忽略這些路徑的csrf保護以便admin-client註冊                  .ignoringAntMatchers(                          adminContextPath + "/instances",                          adminContextPath + "/actuator/**"                  );      }  }
  • 修改啟動類,開啟AdminServer及註冊發現功能:
@EnableDiscoveryClient  @EnableAdminServer  @SpringBootApplication  public class AdminSecurityServerApplication {        public static void main(String[] args) {          SpringApplication.run(AdminSecurityServerApplication.class, args);      }    }
  • 啟動eureka-server,admin-security-server,訪問Spring Boot Admin 主頁發現需要登錄才能訪問:http://localhost:9301

使用到的模組

springcloud-learning  ├── eureka-server -- eureka註冊中心  ├── admin-server -- admin監控中心服務  ├── admin-client -- admin監控中心監控的應用服務  └── admin-security-server -- 帶登錄認證的admin監控中心服務

項目源碼地址

https://github.com/macrozheng/springcloud-learning