首页  

Springboot监控端点Actuator实例     所属分类 springboot 浏览量 117
springboot2
spring-boot-starter-actuator
原生端点  用户自定义端点


应用配置类   查看应用静态信息,例如自动配置信息 加载的bean信息  配置信息 环境信息 请求映射信息
度量指标类   运行期的动态信息,堆栈 一些健康指标 metrics信息等
操作控制类   shutdown 

HealthIndicators
org.springframework.boot.actuate.health.Health

DiskSpaceHealthIndicator
DataSourceHealthIndicator
ElasticsearchHealthIndicator


自定义端点
@Endpoint @ReadOperator @WriteOperation @DeleteOperation

@Endpoint 
@ReadOperation  
@WriteOperation  
@DeleteOperation  

@WebEndpoint
@JmxEndpoint

@WebEndpoint 相当于  @RestController


默认只暴露 info和health接口

management.endpoints.web.exposure.include = health,info,metrics


management.server.port=8090
management.endpoints.web.base-path=/actuator
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
management.endpoint.shutdown.enabled=true

management.endpoints.web.exposure.exclude=beans,trace

management.endpoints.jmx.exposure.exclude   
management.endpoints.jmx.exposure.include   *

health
info         显示配置文件中以 info 开头的配置信息
metrics
mappings
httptrace    显示HTTP跟踪信息(默认最近100个HTTP请求 响应信息)
configprops 
env
beans
loggers
prometheus

jolokia   通过HTTP公开JMX bean(当Jolokia在类路径上时,不适用于WebFlux)

Using Jolokia for JMX over HTTP



自定义健康指示器和监控端点 /actuator/myendpoint /actuator/health curl post json 数据 curl -H "Content-type: application/json" -X POST -d '{"property":"myHealthStatus", "value":"FAIL2"}' http://127.0.0.1:8090/actuator/myendpoint postman post raw JSON(application/json) {"property":"myHealthStatus", "value":"FAIL"}
@Component("myHealthIndicator") public class MyHealthIndicator implements HealthIndicator{ @Override public Health health() { Object myHealthStatus = StatusUtil.get("myHealthStatus"); String myHealthStatusStr = null; if(myHealthStatus!=null) { myHealthStatusStr = myHealthStatus.toString(); }else { myHealthStatusStr = "ok"; } if("ok".equalsIgnoreCase(myHealthStatusStr )){ return Health.up().withDetail("myHealthStatus",myHealthStatusStr).build(); } return Health.down().withDetail("myHealthStatus",myHealthStatusStr).build(); } } @Component @Endpoint(id = "myendpoint") // 一定要加@Component ,不能用 myEndpoint public class MyEndPoint { @ReadOperation public Map<String,Object> read(){ Map<String,Object> status = StatusUtil.getData(); Map<String,Object> map = new HashMap<>(status); map.put("now", LocalDateTime.now().toString()); return map; } @WriteOperation public void update(String property, String value) { System.out.println(property+","+value); StatusUtil.put(property, value); } }
完整代码 https://gitee.com/dyyx/springboothello/tree/master/src/main/java/demo/myactuator

上一篇     下一篇
SpringMVC 数据验证

AspectJ简介及实例

Jackson JSON的三种处理方式

SpringIoC容器要点

JavaConfig与常见的Annotation

spring容器事件机制