经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
SpringBoot 之Actuator.
来源:cnblogs  作者:JMCui  时间:2018/10/21 20:30:38  对本文有异议

一、Actuator 介绍

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

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

二、Actuator 使用

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

    1、pom.xml

  1. <!-- 2、监控 —— Actuator插件 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-actuator</artifactId>
  5. </dependency>

    2、application.yml

  1. management:
  2. endpoints:
  3. # 暴露 EndPoint 以供访问,有jmx和web两种方式,exclude 的优先级高于 include
  4. jmx:
  5. exposure:
  6. exclude: '*'
  7. include: '*'
  8. web:
  9. exposure:
  10. # exclude: '*'
  11. include: ["health","info","beans","mappings","logfile","metrics","shutdown","env"]
  12. base-path: /actuator # 配置 Endpoint 的基础路径
  13. cors: # 配置跨域资源共享
  14. allowed-origins: http://example.com
  15. allowed-methods: GET,POST
  16. enabled-by-default: true # 修改全局 endpoint 默认设置
  17. endpoint:
  18. auditevents: # 1、显示当前引用程序的审计事件信息,默认开启
  19. enabled: true
  20. cache:
  21. time-to-live: 10s # 配置端点缓存响应的时间
  22. beans: # 2、显示一个应用中所有 Spring Beans 的完整列表,默认开启
  23. enabled: true
  24. conditions: # 3、显示配置类和自动配置类的状态及它们被应用和未被应用的原因,默认开启
  25. enabled: true
  26. configprops: # 4、显示一个所有@ConfigurationProperties的集合列表,默认开启
  27. enabled: true
  28. env: # 5、显示来自Spring的 ConfigurableEnvironment的属性,默认开启
  29. enabled: true
  30. flyway: # 6、显示数据库迁移路径,如果有的话,默认开启
  31. enabled: true
  32. health: # 7、显示健康信息,默认开启
  33. enabled: true
  34. show-details: always
  35. info: # 8、显示任意的应用信息,默认开启
  36. enabled: true
  37. liquibase: # 9、展示任何Liquibase数据库迁移路径,如果有的话,默认开启
  38. enabled: true
  39. metrics: # 10、展示当前应用的metrics信息,默认开启
  40. enabled: true
  41. mappings: # 11、显示一个所有@RequestMapping路径的集合列表,默认开启
  42. enabled: true
  43. scheduledtasks: # 12、显示应用程序中的计划任务,默认开启
  44. enabled: true
  45. sessions: # 13、允许从Spring会话支持的会话存储中检索和删除(retrieval and deletion)用户会话。使用Spring Session对反应性Web应用程序的支持时不可用。默认开启。
  46. enabled: true
  47. shutdown: # 14、允许应用以优雅的方式关闭,默认关闭
  48. enabled: true
  49. threaddump: # 15、执行一个线程dump
  50. enabled: true
  51. # web 应用时可以使用以下端点
  52. heapdump: # 16、 返回一个GZip压缩的hprof堆dump文件,默认开启
  53. enabled: true
  54. jolokia: # 17、通过HTTP暴露JMX beans(当Jolokia在类路径上时,WebFlux不可用),默认开启
  55. enabled: true
  56. logfile: # 18、返回日志文件内容(如果设置了logging.file或logging.path属性的话),支持使用HTTP Range头接收日志文件内容的部分信息,默认开启
  57. enabled: true
  58. prometheus: #19、以可以被Prometheus服务器抓取的格式显示metrics信息,默认开启
  59. enabled: true

    这大抵就是全部默认的 Endpoint 的配置了,怎么样?强大吧!之前做了一个网络监控的项目,就是能够实时查看服务器的 CPU、内存、磁盘、IO 这些(基于 sigar.jar 实现),然后现在发现 SpringBoot 就这样轻松支持了,还更强大,更简便......

    默认的 Endpoint 映射前缀是 /actuator,可以通过如上 base-path 自定义设置。

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

    3、效果

    做好了如上的配置,接下来我们只需要访问对应的 Endpoint 就可以啦,/actuator/[Endpoint ID](http://127.0.0.1:8080/actuator/health)。

三、自定义 Endpoint

    自定义 Endpoint 端点,只需要在我们的新建Bean上使用 @Endpoint 注解即可。则 Bean 中的方法就可以通过 JMX 或者 HTTP 公开。除此之外,你还可以使用 @JmxEndpoint@WebEndpoint 编写 EndPoint。但是这些 EndPoint 仅限于各自的公开方式。例如,@WebEndpoint 仅通过HTTP公开,而不通过JMX公开。

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

OperationHTTP method
@ReadOperation GET
@WriteOperation POST
@DeleteOperation DELETE

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

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

    1、Endpoint Bean

  1. @Component
  2. @Endpoint(id = "my", enableByDefault = true) //设置 id,并选择是否默认开启
  3. public class MyEndPoint {
  4. @ReadOperation
  5. public List<String> getPaths() {
  6. List<String> list = new ArrayList<>();
  7. list.add("java");
  8. list.add("c++");
  9. list.add("python");
  10. return list;
  11. }
  12. @ReadOperation
  13. public String get(@Selector String arg0) {
  14. return arg0;
  15. }
  16. @WriteOperation
  17. public String post() {
  18. return "post";
  19. }
  20. @DeleteOperation
  21. public Integer delete() {
  22. return 1;
  23. }
  24. }

    2、暴露 Endpoint

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

  1. management:
  2. endpoints:
  3. # 暴露 EndPoint 以供访问,有jmx和web两种方式,exclude 的优先级高于 include
  4. jmx:
  5. exposure:
  6. exclude: '*'
  7. include: '*'
  8. web:
  9. exposure:
  10. # exclude: '*'
  11. include: ["health","info","beans","mappings","logfile","metrics","shutdown","env","my"]

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

    3、@Selector

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

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

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

  1. <plugins>
  2. <plugin>
  3. <groupId>org.apache.maven.plugins</groupId>
  4. <artifactId>maven-compiler-plugin</artifactId>
  5. <configuration>
  6. <compilerArgs>
  7. <arg>-parameters</arg>
  8. </compilerArgs>
  9. </configuration>
  10. </plugin>
  11. <!-- 或者:
  12. <plugin>
  13. <groupId>org.apache.maven.plugins</groupId>
  14. <artifactId>maven-compiler-plugin</artifactId>
  15. <version>3.7.0</version>
  16. <configuration>
  17. <parameters>true</parameters>
  18. </configuration>
  19. </plugin>
  20. -->
  21. </plugins>

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

 

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号