当前位置:   article > 正文

Springboot入门之集成Actuator_springboot集成actuator

springboot集成actuator

1.Actuator是什么?

Actuator是spring boot下的一个模块,提供http (或JMX)端点来实现对应用程序的监视和管理、收集运行状况等功能。

2.添加相关依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>

3.启动测试

http://localhost:8080/actuator

默认开放3个endpoint, /actuator /actuator/health /actuator/health/{*path}

4.开放endpoint 修改application.yml

  1. management:
  2. server:
  3. port: 12345 # 端口默认=server.port 增加安全性
  4. endpoints:
  5. web:
  6. exposure:
  7. include: "*" # 开放节点 * 开放所有 beans,.....
  8. # exclude: beans # 禁止开放节点 * 禁止开放所有 beans,.....
  9. base-path: /control # 自定义根路径 增加安全性
  10. endpoint:
  11. shutdown:
  12. enabled: true # 打开shutdown端点 POST请求
  13. health:
  14. show-details: always # 获得健康检查中所有指标的详细信息
  15. beans:
  16. cache:
  17. time-to-live: 100s # 不带参数的端点请求会自动进行缓存
  18. #health:
  19. #defaults:
  20. #enabled: false # 禁用所有自动健康检查
  21. #mongo:
  22. #enabled: false # 禁用制定组件(mongo)自动健康检查
  1. {
  2. "_links": {
  3. "self": {
  4. "href": "http://localhost/actuator",
  5. "templated": false
  6. },
  7. "beans": {
  8. "href": "http://localhost/actuator/beans",
  9. "templated": false
  10. },
  11. "caches-cache": {
  12. "href": "http://localhost/actuator/caches/{cache}",
  13. "templated": true
  14. },
  15. "caches": {
  16. "href": "http://localhost/actuator/caches",
  17. "templated": false
  18. },
  19. "health": {
  20. "href": "http://localhost/actuator/health",
  21. "templated": false
  22. },
  23. "health-path": {
  24. "href": "http://localhost/actuator/health/{*path}",
  25. "templated": true
  26. },
  27. "info": {
  28. "href": "http://localhost/actuator/info",
  29. "templated": false
  30. },
  31. "conditions": {
  32. "href": "http://localhost/actuator/conditions",
  33. "templated": false
  34. },
  35. "configprops": {
  36. "href": "http://localhost/actuator/configprops",
  37. "templated": false
  38. },
  39. "configprops-prefix": {
  40. "href": "http://localhost/actuator/configprops/{prefix}",
  41. "templated": true
  42. },
  43. "env": {
  44. "href": "http://localhost/actuator/env",
  45. "templated": false
  46. },
  47. "env-toMatch": {
  48. "href": "http://localhost/actuator/env/{toMatch}",
  49. "templated": true
  50. },
  51. "loggers": {
  52. "href": "http://localhost/actuator/loggers",
  53. "templated": false
  54. },
  55. "loggers-name": {
  56. "href": "http://localhost/actuator/loggers/{name}",
  57. "templated": true
  58. },
  59. "heapdump": {
  60. "href": "http://localhost/actuator/heapdump",
  61. "templated": false
  62. },
  63. "threaddump": {
  64. "href": "http://localhost/actuator/threaddump",
  65. "templated": false
  66. },
  67. "metrics-requiredMetricName": {
  68. "href": "http://localhost/actuator/metrics/{requiredMetricName}",
  69. "templated": true
  70. },
  71. "metrics": {
  72. "href": "http://localhost/actuator/metrics",
  73. "templated": false
  74. },
  75. "scheduledtasks": {
  76. "href": "http://localhost/actuator/scheduledtasks",
  77. "templated": false
  78. },
  79. "mappings": {
  80. "href": "http://localhost/actuator/mappings",
  81. "templated": false
  82. }
  83. }
  84. }

5.endpoint 介绍

5.1)beans:注册到Sprng容器中的Bean对象集合
5.2)caches:缓存信息
5.3)health:应用的健康状态
5.4)info:应用的基本信息,需要手工配置
5.5)conditions:自动配置生效的条件
5.6)configprops:获取所有的配置属性
5.7)auditevents:显示应用暴露的审计事件 
5.8)metrics:应用多样的度量信息                          查看某项的度量信息/metric/jvm.buffer.count
5.9)loggers:日志配置                                            loggers/root
5.10)httptrace:HTTP足迹,显示最近100个HTTP request/repsponse
5.11)env:当前的环境特性
5.12)flyway:显示数据库迁移路径的详细信息
5.13)shutdown:关闭应用                                        唯一POST请求的节点
5.14)mappings:所有的@RequestMapping路径
5.15)scheduledtask:应用中的调度任务
5.16)threaddump:线程信息
5.17)heapdump:JVM堆dump

6.端点保护

6.1 添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>

6.2 添加配置

  1. spring:
  2. security:
  3. user:
  4. name: admin
  5. password: admin
  6. roles: admin

6.3 配置拦截地址

  1. package com.example.hellospringboot.config;
  2. import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
  3. import org.springframework.boot.actuate.context.ShutdownEndpoint;
  4. import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  7. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  8. @Configuration
  9. public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {
  10. @Override
  11. protected void configure(HttpSecurity http) throws Exception {
  12. http.authorizeRequests()
  13. .requestMatchers(EndpointRequest.to(ShutdownEndpoint.class))
  14. .hasRole("admin")
  15. .requestMatchers(EndpointRequest.toAnyEndpoint())
  16. .permitAll()
  17. .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
  18. .permitAll()
  19. .antMatchers("/control*")
  20. .permitAll()
  21. .antMatchers("/control**")
  22. .authenticated()
  23. .and()
  24. .httpBasic();
  25. }
  26. }

Springboot入门之简单启动_Ocean@上源码的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/856526
推荐阅读
相关标签
  

闽ICP备14008679号