SpringBoot 2 Actuator Endpoint


創建Maven 項目:

POM 中加入 actuator 依賴,來獲取應用程序的實時運行數據。

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

POM全文如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.study</groupId>
  <artifactId>springboot-monitor</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.1.1.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

創建啟動類Application

package com.actuator.simple;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

默認情況下,只有 如下 3 個 actuator 端口暴露出來:

 

 

 

 

 

 

 

 在application.yml 中,開啟 shutdown 端點,並暴露 shutdown 、health 和 info端口:

server:
  port: 8090
management:
  endpoint: 
    shutdown:
      enabled: true
  endpoints:
    web:
      exposure:
        include: shutdown,health,info
                            

 

 

 調用 shutdown 端點:

 在yaml 中配置暴露所有端點:

server:
  port: 8090
management:
  endpoint: 
    shutdown:
      enabled: true
  endpoints:
    web:
      exposure:
        include: "*"
                            

 

在pom.xml 中加入如下security依賴:

               <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

並配置保護Actuator Endpoint,開啟HttpBasic, 並在application.yml 中創建role為ACTUATOR 的 User。

package com.actuator.simple;

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter{

    /* 
     * need to add http.csrf().disable(); to call actuator/shutdown successfully
     * https://stackoverflow.com/questions/50689032/spring-boot-actuator-shutdown-endpoint-forbidden
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    //protect all Actuator endpoints,such as actuator/health, actuator/info
    http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests().anyRequest().hasRole("ACTUATOR").and().httpBasic().and().csrf().disable();
    
    }
} 

 注意此處我們關閉了 CSRF protection,以使 POST /actuator/shutdown 方法得以執行,否則,調用

 POST  /actuator/shutdown 會得到403 Forbidden Error。

Cross Site Request Forgery Protection 
Since SpringBoot relies on Spring security's defaults, CSRF protection is turned by default. This means that the actuator endpoints that require a POST( SHUTDOWN and LOGGERS endpoints) PUT or DELETE will get a 403 forbidden error when the default security configuration is in use.

[Note]
It's recommended that disabling CSRF protection completely ONLY IF you are creating a service that is used by non-browser clients.

 

management.endpoint.health.show-details 的值默認是never,即不顯示 health 端點的 details 信息。

當將  management.endpoint.health.show-details  設為 when_authorized 或  always  時, 可看到health 端點的 details 信息。

此時,application.yml 如下:

server:
  port: 8090
management:
  endpoint: 
    health:
#     show-details: never(default) / when-authorized / always
      show-details: when-authorized 
    shutdown:
      enabled: true
  endpoints:
    web:
      exposure:
        include: shutdown,health,info
spring:
  security:
    user:
      name: xluffy
      password: 123
      roles: ACTUATOR
                
                            

調用  /actuator/health ,並提供application.yml 中設定的用戶名 、密碼,此時可看到health 端點的 details 信息

 

 SpringBoot會根據classpath 中的依賴自動配置HealthIndicator: 例,如果在 pom.xml 中加入了如下Redis 依賴,並啟動Redis Server,此時的 health 端點信息如下:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

 

 此處,有3 個 HealthIndicator,分別是 HealthIndicator 、 DiskSpaceHealthIndicator 、RedisHealthIndicator

對於HealthIndicator 如果如下繼承該類,

package com.actuator.simple;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class CustomHealth implements HealthIndicator {

    @Override
    public Health health() {
    
    return Health.outOfService().withDetail("msg", "服務中斷...").build();
    }

}

則 Health 端點的信息如下:

 

當在application.yml 中加入

info:
  author:
    name: xluffy

以及配置build-info goal 時,

             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                  <execution>
                    <goals>
                      <goal>build-info</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>

可以看到如下信息:

 

git-commit-id-plugin 可以將Git 信息暴露到 actuator/info 端口,前提是需要build project(maven clean  package)
Add this to plugins section of pom.xml. maven will generate this file during build ./target/classes/git.properties.
Spring will read contents of this file and include it in the response of /actuator/info
此時的application.yml 是:
server:
  port: 8090
management:
  info:
    git:
#      mode: full
      mode: simple
  endpoint: 
    health:
#     show-details: never(default) / when-authorized / always
      show-details: when-authorized 
    shutdown:
      enabled: true
  endpoints:
    web:
      exposure:
        include: shutdown,health,info
spring:
  security:
    user:
      name: xluffy
      password: 123
      roles: ACTUATOR
info:
  author:
    name: xluffy
              
                
                            

 

<plugin> 
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
</plugin>

 


免責聲明!

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



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