乐趣区

Springboot-Actuator

Springboot Actuator

1. 启用 springboot actuator

<dependency>
        <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
                </dependency>
            
            ### 2. endpoint
            
            endpoint 可以帮助你监控应用的信息, 例如,health endpoint 提供基本的应用程序运行状况信息
            
            为了远程调用 endpoint, 你必须通过 http 或者 jmx 去暴露该 endpoint
            
            默认情况下, 你可以使用 http 去访问 /actuator/endpoint-id 来获取该 endpoint 所提供的信息
            
            例如: 你可以访问 /actuator/health 来获取应用的基本进行状况
            
            #### 内置的 endpoint
            
            | ID                 | Description                                                  |
            | :----------------- | :----------------------------------------------------------- |
            | `auditevents`      | Exposes audit events information for the current application. Requires an `AuditEventRepository` bean. |
            | `beans`            | Displays a complete list of all the Spring beans in your application. |
            | `caches`           | Exposes available caches.                                    |
            | `conditions`       | Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match. |
            | `configprops`      | Displays a collated list of all `@ConfigurationProperties`.  |
            | `env`              | Exposes properties from Spring’s `ConfigurableEnvironment`.  |
            | `flyway`           | Shows any Flyway database migrations that have been applied. Requires one or more `Flyway` beans. |
            | `health`           | Shows application health information.                        |
            | `httptrace`        | Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges). Requires an `HttpTraceRepository` bean. |
            | `info`             | Displays arbitrary application info.                         |
            | `integrationgraph` | Shows the Spring Integration graph. Requires a dependency on `spring-integration-core`. |
            | `loggers`          | Shows and modifies the configuration of loggers in the application. |
            | `liquibase`        | Shows any Liquibase database migrations that have been applied. Requires one or more `Liquibase` beans. |
            | `metrics`          | Shows‘metrics’information for the current application.     |
            | `mappings`         | Displays a collated list of all `@RequestMapping` paths.     |
            | `scheduledtasks`   | Displays the scheduled tasks in your application.            |
            | `sessions`         | Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Requires a Servlet-based web application using Spring Session. |
            | `shutdown`         | Lets the application be gracefully shutdown. Disabled by default. |
            | `threaddump`       | Performs a thread dump.                                      |
            
            如果你的应用是 web application (Spring MVC, Spring WebFlux, or Jersey), 以下 endpoint 也是可用的:
            
            | ID           | Description                                                  |
            | :----------- | :----------------------------------------------------------- |
            | `heapdump`   | Returns an `hprof` heap dump file.                           |
            | `jolokia`    | Exposes JMX beans over HTTP (when Jolokia is on the classpath, not available for WebFlux). Requires a dependency on `jolokia-core`. |
            | `logfile`    | Returns the contents of the logfile (if `logging.file.name` or `logging.file.path` properties have been set). Supports the use of the HTTP `Range` header to retrieve part of the log file’s content. |
            | `prometheus` | Exposes metrics in a format that can be scraped by a Prometheus server. Requires a dependency on `micrometer-registry-prometheus`. |
            
            #### 2.1 启用 endpoints
            
            默认的, 除了 shutdown 以外的 endpoint 都是默认启用的, 使用如下配置关闭默认启用的 endpoints, 然后为单独的 endpoint 配置是否启用
            
            ```yml
            management:
              endpoints:
                  enable-by-default: false
                    endpoint:
                        info:
                              enabled: true
                              ```
                              
                              `enabled` 选项控制着该 endpoint 是否被创建, 他的相关 bean 是否存在上下文中.
                              
                              #### 2.2 暴露 endpoints
                              
                              一些 endpoints 将会暴露敏感的信息, 下表显示了默认情况下内置的 endpoints 的暴露情况
                              
                              | ID                 | JMX  | Web  |
                              | :----------------- | :--- | :--- |
                              | `auditevents`      | Yes  | No   |
                              | `beans`            | Yes  | No   |
                              | `caches`           | Yes  | No   |
                              | `conditions`       | Yes  | No   |
                              | `configprops`      | Yes  | No   |
                              | `env`              | Yes  | No   |
                              | `flyway`           | Yes  | No   |
                              | `health`           | Yes  | Yes  |
                              | `heapdump`         | N/A  | No   |
                              | `httptrace`        | Yes  | No   |
                              | `info`             | Yes  | Yes  |
                              | `integrationgraph` | Yes  | No   |
                              | `jolokia`          | N/A  | No   |
                              | `logfile`          | N/A  | No   |
                              | `loggers`          | Yes  | No   |
                              | `liquibase`        | Yes  | No   |
                              | `metrics`          | Yes  | No   |
                              | `mappings`         | Yes  | No   |
                              | `prometheus`       | N/A  | No   |
                              | `scheduledtasks`   | Yes  | No   |
                              | `sessions`         | Yes  | No   |
                              | `shutdown`         | Yes  | No   |
                              | `threaddump`       | Yes  | No   |
                              
                              你可以使用如下配置去选择 endpoint 是否暴露 http 端口或者 jmx:
                              
                              | Property                                    | Default        |
                              | :------------------------------------------ | :------------- |
                              | `management.endpoints.jmx.exposure.exclude` |                |
                              | `management.endpoints.jmx.exposure.include` | `*`            |
                              | `management.endpoints.web.exposure.exclude` |                |
                              | `management.endpoints.web.exposure.include` | `info, health` |
                              
                              `include` 表示需要暴露的 endpoint, exclude 表示不用暴露的, exclude 选项优先于 include 选项, exclude 和 include 都是 list, * 表示所有 endpoint, 在 yml 中 \* 表示特殊用法, 使用 "\*" 代替
                              
                              for example:
                              
                              ```yml
                              # 只暴露 health, info 的 jxm
                              management:
                                endpoints:
                                   jmx:
                                        exposure:
                                               include: info, health
                                               # 暴露所有除了 info, health 的 web 端口
                                                   web:
                                                         exposure:
                                                                 include: "*"
                                                                         exclude: info, health
                                                                         ```
退出移动版