1.引入Mavan依赖
org.springframework.boot spring-boot-starter-actuator org.springframework.hateoas spring-hateoas
2.actuator 相关配置
修改application.yml文件
server: port: 8081spring: application: name: springboot## actuator配置management: endpoints: web: cors: # 跨域处理 allowed-origins: '*' allowed-methods: '*' allow-credentials: true base-path: /manage # 访问路径 为 http://ip:port/monitor/manage exposure: include: '*' enabled-by-default: false server: servlet: context-path: /monitor # 访问路径 为 http://ip:port/monitor port: 1234 # management 的端口 http://ip:1234 如果不想通过HTTP公开端点,则可以将管理端口设置为-1,如以下示例所示: endpoint: #暴露出的endpoint shutdown: enabled: false # shutdown 可以关闭制定的端点 除shutdown以外的所有端点均已启用 info: enabled: true health: enabled: true show-details: always #展示详情 metrics: enabled: true health: db: enabled: false #关闭默认配置的db检查 redis: enabled: false# /actuator/info 的信息的配置info: name: @project.artifactId@ encoding: @project.build.sourceEncoding@ java: source: @java.version@ target: @java.version@
3.配置完成项目
访问路径
http://ip:port/context-path/base-path/**
4.扩展actuator的监控功能
自定义endPoint
/** * @describe: 自定义端点暴漏此端点需要设置 management.endpoints.web.exposure.include=* */@Endpoint(id = "person")@Componentpublic class MyEndpoint { private final Map people = new ConcurrentHashMap<>(); MyEndpoint() { this.people.put("name1", new Person("n1")); this.people.put("name2", new Person("n2")); this.people.put("name3", new Person("3")); } @ReadOperation public List getAll() { return new ArrayList<>(this.people.values()); } @ReadOperation public Person getPerson(@Selector String name) { return this.people.get(name); } @WriteOperation public void addOrUpdatePerson(@Selector String name, @Selector String person) { this.people.put(name, new Person(person)); } class Person { private String name; Person(String name) { this.name = name; } public String getName() { return this.name; } }}
对health的扩展~实现接口方式
@Component("myHealth_interface")public class MyHealthIndicator implements HealthIndicator { @Override public Health health() { //健康检查实现 int errorCode = check(); if (errorCode != 0) { return Health.down().withDetail("Error Code", errorCode).build(); } return Health.up().build(); } /** * 健康检查实现 */ public int check(){ return 0; }}
对health的扩展~实现接口方式~实现抽象类
@Component("myHealth_abstract")public class MyHealthIndicator1 extends AbstractHealthIndicator { @Override protected void doHealthCheck(Health.Builder builder) throws Exception { builder.up().withDetail("xxx", "xxxyyy"); }}
扩展info信息
public class MyContributor implements InfoContributor { @Value("${spring.application.name}") private String applicationName; @Override public void contribute(Info.Builder builder) { builder.withDetail("Application-info", Collections.singletonMap("name", applicationName)); }}
![004e84b43e3cd8e38919b68e198fcd2e.png](https://i-blog.csdnimg.cn/blog_migrate/6f8c64cbc3d4d6ddb6df1575a9ff0a6f.jpeg)
public class MyContributor implements InfoContributor { @Value("${spring.application.name}") private String applicationName; @Override public void contribute(Info.Builder builder) { builder.withDetail("Application-info", Collections.singletonMap("name", applicationName)); }}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:
https://blog.csdn.net/houkai18792669930/article/details/108919788