SpringBoot 之Actuator.

1、Actuator 介绍

    Actuator 是 SpringBoot 项目中一个很是强大一个功能,有助于对应用程序进行监视和管理,经过 restful api 请求来监管、审计、收集应用的运行状况。java

    Actuator 的核心是端点 Endpoint,它用来监视应用程序及交互,spring-boot-actuator 中已经内置了很是多的 Endpoint(health、info、beans、metrics、httptrace、shutdown等等),同时也容许咱们本身扩展本身的 Endpoints。每一个 Endpoint 均可以启用和禁用。要远程访问 Endpoint,还必须经过 JMX 或 HTTP 进行暴露,大部分应用选择HTTP,Endpoint 的ID默认映射到一个带 /actuator 前缀的URL。例如,health 端点默认映射到 /actuator/healthpython

2、Actuator 使用

    启用 Actuator 最简单方式是添加 spring-boot-starter-actuator ‘Starter’依赖。 c++

    一、pom.xml

<!-- 二、监控 —— Actuator插件 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

    二、application.yml

management:
  endpoints:
    # 暴露 EndPoint 以供访问,有jmx和web两种方式,exclude 的优先级高于 include
 jmx:
      exposure:
        exclude: '*'
        include: '*' web:
      exposure:
      # exclude: '*'
        include: ["health","info","beans","mappings","logfile","metrics","shutdown","env"]
      base-path: /actuator  # 配置 Endpoint 的基础路径
      cors: # 配置跨域资源共享
        allowed-origins: http://example.com
        allowed-methods: GET,POST
    enabled-by-default: true # 修改全局 endpoint 默认设置
 endpoint:
    auditevents: # 一、显示当前引用程序的审计事件信息,默认开启
      enabled: true
      cache:
        time-to-live: 10s # 配置端点缓存响应的时间
    beans: # 二、显示一个应用中全部 Spring Beans 的完整列表,默认开启
      enabled: true
    conditions: # 三、显示配置类和自动配置类的状态及它们被应用和未被应用的缘由,默认开启
      enabled: true
    configprops: # 四、显示一个全部@ConfigurationProperties的集合列表,默认开启
      enabled: true
    env: # 五、显示来自Spring的 ConfigurableEnvironment的属性,默认开启
      enabled: true
    flyway: # 六、显示数据库迁移路径,若是有的话,默认开启
      enabled: true
    health: # 七、显示健康信息,默认开启
      enabled: true
      show-details: always
    info: # 八、显示任意的应用信息,默认开启
      enabled: true
    liquibase: # 九、展现任何Liquibase数据库迁移路径,若是有的话,默认开启
      enabled: true
    metrics: # 十、展现当前应用的metrics信息,默认开启
      enabled: true
    mappings: # 十一、显示一个全部@RequestMapping路径的集合列表,默认开启
      enabled: true
    scheduledtasks: # 十二、显示应用程序中的计划任务,默认开启
      enabled: true
    sessions: # 1三、容许从Spring会话支持的会话存储中检索和删除(retrieval and deletion)用户会话。使用Spring Session对反应性Web应用程序的支持时不可用。默认开启。
      enabled: true
    shutdown: # 1四、容许应用以优雅的方式关闭,默认关闭
      enabled: true
    threaddump: # 1五、执行一个线程dump
      enabled: true
    # web 应用时可使用如下端点
    heapdump: # 1六、    返回一个GZip压缩的hprof堆dump文件,默认开启
      enabled: true
    jolokia: # 1七、经过HTTP暴露JMX beans(当Jolokia在类路径上时,WebFlux不可用),默认开启
      enabled: true
    logfile: # 1八、返回日志文件内容(若是设置了logging.file或logging.path属性的话),支持使用HTTP Range头接收日志文件内容的部分信息,默认开启
      enabled: true
    prometheus: #1九、以能够被Prometheus服务器抓取的格式显示metrics信息,默认开启
      enabled: true

    这大抵就是所有默认的 Endpoint 的配置了,怎么样?强大吧!以前作了一个网络监控的项目,就是可以实时查看服务器的 CPU、内存、磁盘、IO 这些(基于 sigar.jar 实现),而后如今发现 SpringBoot 就这样轻松支持了,还更强大,更简便......git

    默认的 Endpoint 映射前缀是 /actuator,能够经过如上 base-path 自定义设置。github

    每一个 Endpoint 均可以配置开启或者禁用。可是仅仅开启 Endpoint 是不够的,还须要经过 jmx 或者 web 暴露他们,经过 exclude 和 include 属性配置。web

    三、效果

    作好了如上的配置,接下来咱们只须要访问对应的 Endpoint 就能够啦,/actuator/[Endpoint ID](http://127.0.0.1:8080/actuator/health)。spring

3、自定义 Endpoint

    自定义 Endpoint 端点,只须要在咱们的新建Bean上使用 @Endpoint 注解便可。则 Bean 中的方法就能够经过 JMX 或者 HTTP 公开。除此以外,你还可使用 @JmxEndpoint@WebEndpoint 编写 EndPoint。可是这些 EndPoint 仅限于各自的公开方式。例如,@WebEndpoint 仅经过HTTP公开,而不经过JMX公开。数据库

    那么是否是类中全部的方法都支持对外公开呢?很明显不是的。这里又要提到三个注解,只有加三个注解的方法才支持对外公开,而且每一个注解都有支持它的 HTTP method。以下:apache

Operation HTTP method
@ReadOperation GET
@WriteOperation POST
@DeleteOperation DELETE

    Endpoint 上的操做经过参数接收输入。 当经过网络公开时,这些参数的值取自URL的查询参数和JSON请求主体。 经过JMX公开时,参数将映射到MBean操做的参数。参数默认是必需的,能够经过使用 @Nullable 注释使其成为可选的。api

    能够经过使用 @Selector 注释操做方法的一个或多个参数来进一步定制路径。@Selector 会将路径上的参数做为变量传递给操做方法。这个注解有点诡异,且听我徐徐道来~~

    一、Endpoint Bean

@Component
@Endpoint(id = "my", enableByDefault = true) //设置 id,并选择是否默认开启
public class MyEndPoint {

    @ReadOperation
    public List<String> getPaths() {
        List<String> list = new ArrayList<>();
        list.add("java");
        list.add("c++");
        list.add("python");
        return list;
    }

    @ReadOperation
    public String get(@Selector String arg0) {
        return arg0;
    }
@WriteOperation
public String post() { return "post"; } @DeleteOperation public Integer delete() { return 1; } }

    二、暴露 Endpoint

设置好了上面的 Endpoint Bean,还不能真正的访问到它们,须要在 application.yml 中将它们暴露出来:

management:
  endpoints:
    # 暴露 EndPoint 以供访问,有jmx和web两种方式,exclude 的优先级高于 include
 jmx:
      exposure:
        exclude: '*'
        include: '*' web:
      exposure:
      # exclude: '*'
        include: ["health","info","beans","mappings","logfile","metrics","shutdown","env","my"]

作好这些配置后,你就能访问到 Endpoint 了(http://127.0.0.1:8080/actuator/my)

    三、@Selector

注意到没有,上面的 Endpoint 有一个 @Selector 参数的方法,而且参数名是 arg0,这个参数名是有学问滴......

原来我给的参数名是 path,原来我设想我能够访问 /actuator/my/[任意字符] 的路径,可是会报 400 参数不匹配错误。可是嘞,/actuator/my/[任意字符]?path=[任意字符] 是正常访问的,真是奇了怪了!

原来,为了使 @Selector 正常工做,必须使用嵌入的参数名称编译 Endpoint(-parameters),以下。或者将参数名改成 arg0 就能达到目的。这个是 stackoverflow 上的一个解释~

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <compilerArgs>
                <arg>-parameters</arg>
            </compilerArgs>
        </configuration>
    </plugin>
    <!-- 或者:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
            <parameters>true</parameters>
        </configuration>
    </plugin>
    -->
</plugins>

tips: -parameters 的方式我没有验证经过呀~~汗

 

演示源代码:https://github.com/JMCuixy/Thymeleaf

相关文章
相关标签/搜索