Springboot2.0集成actuator健康监控

    科技2024-11-10  7

    1.引入Mavan依赖

    <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency>     <groupId>org.springframework.hateoas</groupId>     <artifactId>spring-hateoas</artifactId> </dependency>

    2.actuator 相关配置

    修改application.yml文件

    server:   port: 8081 spring:   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") @Component public class MyEndpoint {     private final Map<String, Person> 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<Person> 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));     }}

     

    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));     }}
    Processed: 0.063, SQL: 8