Spring Boot Admin Reference Guide - 監控服務配置

什麼是Spring Boot Admin

Spring Boot Admin 是管理和監控你的SpringBoot應用程序的社區項目,做者codecentric。應用程序經過Spring Boot Admin Client進行註冊,HTTP方式或者使用springcloud服務發現(例如:Eureka,Consul)。Spring Boot Admin UI只是Spring Boot Actuator endpoints上的一個AngularJs應用程序,提供圖形化界面的展現。git

版本說明

SpringBoot 2.0.1.RELEASE
SpringBootAdmin 2.0.0-SNAPSHOT
SpringBoot2.0版本以後配置文件會有部分調整,本文不適用於 2.0以前的版本

項目集成之SpringBoot項目

Admin服務端配置

1. pom.xml中引入Maven依賴github

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
PS: spring-boot-admin-starter-server包含 spring-boot-admin-serverspring-boot-admin-server-ui兩個依賴

2. 開啓@EnableAdminServer註解支持web

@SpringBootApplication
@EnableAdminServer
public class AdminServerSpringStrap {
    public static void main(String[] args) {
        SpringApplication.run(AdminServerSpringStrap.class, args);
    }
}
PS:若是使用SpringBootAutoConfiguration方式自動引入註解配置須要添加 @AutoConfigureBefore({AdminServerAutoConfiguration.class}),不然 @EnableAdminServer註解無效

至此Admin服務端已經配置完畢。spring

Admin客戶端配置

1. pom.xml中引入Maven依賴安全

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>

2. application.yml配置app

---
spring:
  profiles: client
  application:
      name: client
  boot:
    admin:
      client:
        url: "http://localhost:8080/"    // Admin服務端地址,根據實際狀況更改
server:
  port: 8081
management:
  endpoints:
    web:
      exposure:
        include: "*"    // SpringBootActuator監控暴露全部接口
PS: SpringBoot2.0以後大部分endpoints不會被暴露,上面咱們暴露了全部endpoints,如應用到生產環境,考慮到安全問題,對於 ActuatorEndpoints請根據須要進行配置

至此Admin客戶端已經配置完畢。ide

運行概覽圖

  • Admin首頁

Admin UI 起始頁面

  • Admin 客戶端詳細監控信息頁面

Admin UI 客戶端詳細監控信息展現頁

添加SpringSecurity進行權限控制

pom.xml添加Maven依賴spring-boot

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
添加到須要進行權限控制的項目中

一. 僅Admin客戶端進行權限控制

1. Admin客戶端application.yml配置fetch

---
spring:
  profiles: client
  application:
      name: client
  boot:
    admin:
      client:
        url: "http://localhost:8080"
        instance:
          metadata:
            user.name: ${spring.security.user.name}
            user.password: ${spring.security.user.password}
  security:
    user:
      name: client
      password: client
server:
  port: 8081
management:
  endpoints:
    web:
      exposure:
        include: "*"
PS:
security下的屬性配置 httpBasic的用戶名密碼
instance.metadata下的屬性配置向 Admin服務端傳輸本客戶端的用戶名和密碼,這樣Admin服務端請求的時候會攜帶已傳遞的用戶名密碼,若是此處的密碼和 security下的不一致,則服務端將沒有權限訪問客戶端的監控

配置完成後,啓動客戶端,服務端請求客戶端監控接口的時候會帶上客戶端傳遞的憑證,至此完畢。ui

二. Admin服務端和客戶端都要進行權限控制

1. Admin服務端增長SpringSecurity的權限配置

啓動器添加 @EnableWebSecurity註解
@Configuration
public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");

        http.authorizeRequests()
            .antMatchers(adminContextPath + "/assets/**").permitAll()
            .antMatchers(adminContextPath + "/login").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
        .logout().logoutUrl(adminContextPath + "/logout").and()
        .httpBasic().and()
        .csrf().disable();
        // @formatter:on
    }
}

2. Admin服務端application.yml配置

---
spring:
  profiles: admin
  application:
    name: admin
  security:
    user:
      name: admin
      password: admin
server:
  port: 8080
management:
  endpoints:
    web:
      exposure:
        include: "*"

3. Admin客戶端application.yml配置

---
spring:
  profiles: client
  application:
      name: client
  security:
    user:
      name: client
      password: client
  boot:
    admin:
      client:
        url: "http://localhost:8080"
        instance:
          metadata:
            user.name: ${spring.security.user.name}
            user.password: ${spring.security.user.password}
        username: admin
        password: admin
server:
  port: 8081
management:
  endpoints:
    web:
      exposure:
        include: "*"
PS:客戶端增長了 spring.boot.admin.client.[username,password]配置,此配置是傳遞 Admin服務端的憑證信息,不然不能註冊到Admin服務端。其餘配置同上一節 僅Admin客戶端進行權限控制一致。

若是須要Admin服務端本身註冊本身,則只要將Admin服務端的application.yml與客戶端一致便可。

至此SpringSecurity權限控制配置完畢。

項目集成之SpringCloud項目(Eureka)

此文只說明 Spring-Boot-Admin集成 Eureka的配置,至於Eureka的集成請關注個人後續文章。

1. Maven依賴說明
Spring-Boot-Admin的相關依賴請參考上述說明

2. Admin服務端配置

spring:
  application:
    name: server-admin
  security:
    user:
      name: admin
      password: admin
server:
  port: 9888
eureka:
  client:
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
    registry-fetch-interval-seconds: 5
  instance:
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}
    lease-renewal-interval-in-seconds: 10
    health-check-url-path: /actuator/health
management:
  endpoints:
    web:
      exposure:
        include: "*"
配置基本同 SpringBoot項目一致,不一樣的是將Admin服務端註冊到 Eureka註冊中心,此配置包含權限控制,根據須要進行刪減。

3. Admin客戶端配置

spring:
  application:
    name: service-hi
  security:
    user:
      name: client
      password: client
server:
  port: 8762

eureka:
  client:
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
    registry-fetch-interval-seconds: 5
  instance:
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}
    lease-renewal-interval-in-seconds: 10
    health-check-url-path: /actuator/health
management:
  endpoints:
    web:
      exposure:
        include: "*"
配置基本同 SpringBoot項目一致,不一樣的是將Admin客戶端註冊到 Eureka註冊中心,此配置包含權限控制,根據須要進行刪減。若是客戶端和服務端都註冊到 同一個註冊中心,則不須要在客戶端指定服務端的地址,而且不須要在客戶端配置服務端的憑證信息。

至此Spring-Boot-Admin集成到Eureka完畢。

附加說明

  • 項目做者codecentric的GitHub:spring-boot-admin
  • 做者codecentric寫的項目指導:Spring Boot Admin Reference Guide
  • admin項目Maven倉庫

    <repository>
        <id>sonatype-nexus-snapshots</id>
        <name>Sonatype Nexus Snapshots</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
相關文章
相關標籤/搜索