Spring Boot (28) actuator與spring-boot-admin

在上一篇中,經過restful api的方式查看信息過於繁瑣,也不直觀,效率低下。當服務過多的時候看起來就過於麻煩,每一個服務都須要調用不一樣的接口來查看監控信息。web

 

SBAspring

  SBA全稱spring boot admin 是一個管理和監控spring boot 應用程序的開源項目,分爲admin-server與admin-client兩個組件,admin-server經過採集actuator端點數據,顯示在spring -boot-admin-ui上,已知的端點幾乎都有進行採集,經過spring-boot-admin能夠動態切換日誌級別、導出日誌、導出heapdump、監控各項指標 等等api

spring boot admin在對單一服務監控的同時也提供了集羣監控方案,支持經過eureka、consul、zookeeper等註冊中心的方式實現多服務監控與管理安全

 

導入依賴restful

  在pom.xml中添加對spring-boot-admin的相關依賴app

<!-- 服務端:帶UI界面 -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- 客戶端包 -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.0.0</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-actuator</artifactId>
        </dependency>
        <!-- 在管理界面中與 JMX-beans 進行交互所須要被依賴的 JAR -->
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>

若是想訪問info的信息須要以下配置maven

    <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>

 

屬性配置ide

  在application.yml中配置actuator的相關配置,其中info開頭的屬性,就是訪問info端點中顯示的相關內容,值得注意的十spring boot2.x中,默認只開放了info、health兩個端點,剩餘的須要本身經過配置management.endpoints.web.exposure.include屬性來加載,這個management.endpoints.web.base-path屬性比較重要,由於spring boot2.x後每一個端點默認的路徑是/actuator/endpointId這樣依賴spring boot admin是沒法正常採集的spring-boot

application.yml測試

spring:
  profiles:
    active: prod #指定讀取哪一個文件
  boot:
    admin:
      client:
        url: http://localhost:8088
        instance:
          prefer-ip: true

info:
  head: head
  body: body
management:
  endpoints:
    web:
      exposure:
        #加載全部的端點,默認只加載了info、health
        include: '*'
      #不配置SBA 掃描不到
      base-path: /
  endpoint:
    health:
      show-details: always
    #能夠關閉指定的端點
    shutdown:
      enabled: false

application-dev.yml 空的

application-prod.yml

server:
  port: 8088
  servlet:
    context-path: /

spring:
  boot:
    admin:
      client:
        username: david
        password: 123456
        instance:
          metadata:
            user:
              name: david
              password: 123456
  security:
    user:
      name: david
      password: 123456

 

在啓動方法中增長@EnableAdminServer註解 表明是Server端

@EnableAdminServer
@SpringBootApplication
public class BootApplication{

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

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

    @Profile("prod")
    @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{
            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();
        }
    }
}

 

測試

啓動項目 訪問 http://localhost:8088/login

相關文章
相關標籤/搜索