Spring Boot Admin:微服務應用監控

SpringBoot實戰電商項目mall(20k+star)地址:github.com/macrozheng/…java

摘要

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

Spring Boot Admin 簡介

SpringBoot應用能夠經過Actuator來暴露應用運行過程當中的各項指標,Spring Boot Admin經過這些指標來監控SpringBoot應用,而後經過圖形化界面呈現出來。Spring Boot Admin不只能夠監控單體應用,還能夠和Spring Cloud的註冊中心相結合來監控微服務應用。github

Spring Boot Admin 能夠提供應用的如下監控信息:web

  • 監控應用運行過程當中的概覽信息;
  • 度量指標信息,好比JVM、Tomcat及進程信息;
  • 環境變量信息,好比系統屬性、系統環境變量以及應用配置信息;
  • 查看全部建立的Bean信息;
  • 查看應用中的全部配置信息;
  • 查看應用運行日誌信息;
  • 查看JVM信息;
  • 查看能夠訪問的Web端點;
  • 查看HTTP跟蹤信息。

建立admin-server模塊

這裏咱們建立一個admin-server模塊來做爲監控中心演示其功能。spring

  • 在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。cookie

  • 在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服務。

監控信息演示

  • 點擊wallboard按鈕,選擇admin-client查看監控信息;架構

  • 監控信息概覽;app

  • 度量指標信息,好比JVM、Tomcat及進程信息;

  • 環境變量信息,好比系統屬性、系統環境變量以及應用配置信息;

  • 查看全部建立的Bean信息;

  • 查看應用中的全部配置信息;

  • 查看日誌信息,須要添加如下配置才能開啓;
logging:
 file: admin-client.log #添加開啓admin的日誌監控
複製代碼

  • 查看JVM信息;

  • 查看能夠訪問的Web端點;

  • 查看HTTP跟蹤信息;

結合註冊中心使用

Spring Boot Admin結合Spring Cloud 註冊中心使用,只需將admin-server和註冊中心整合便可,admin-server 會自動從註冊中心獲取服務列表,而後挨個獲取監控信息。這裏以Eureka註冊中心爲例來介紹下該功能。ide

修改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;spring-boot

  • 查看註冊中心發現服務均已註冊:http://localhost:8001/

添加登陸認證

咱們能夠經過給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監控中心服務
複製代碼

項目源碼地址

github.com/macrozheng/…

公衆號

mall項目全套學習教程連載中,關注公衆號第一時間獲取。

公衆號圖片
相關文章
相關標籤/搜索