springboot actuator monitoring notes

0 Environment

System: win10 
Editor: IDEA

1 concept

Self-monitoring and management information (can be customized) modules

2 file configuration

1 pom configuration

Adding monitoring
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Detailed Configuration
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2 Start access address

locahost:8080/actuator/health

3 shows a more detailed add properties

# Show confidence Details 
 management.endpoint.health.show -details = Always 
# manually configured to be exposed endpoints (other endpoint is packaged not be accessed) 
# management.endpoints.web.exposure.include = configprops, Beans 
# all endpoints exposure 
# especially custom endpoint can first do so in order to save 
# course, you can use the above way 
management.endpoints.web.exposure.include = *
And 2 above the same

3 endpoints custom actuator

1 Overview

One is the expansion of health endpoint to another endpoint Custom

2 Extended health endpoints

Inheritance AbstractHealthIndicator only need to rewrite
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;

// 添加组件扫面注解
@Component
public class UserHealIndicator extends AbstractHealthIndicator {

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        builder.up().withDetail("status", true);

    }
}

3 custom endpoint

Custom Configuration Ports exposure
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;

@Component
@Endpoint(id = "users")
public class UserEndpoint {
    @ReadOperation
    public List<Map<String, Object>> health(){
        //
//        ImmutableMap<String,Object> map = ImmutableMap.of("userId", "1234", "userName", "小王");
//        map.asMultimap()
//        ImmutableList<ImmutableMap<String, Object>> of = ImmutableList.of(map);
//        System.out.println(of.asList());
//        Map<String, Object> map = newHashMap();
//        map.put("userId","1234");
//        map.put("userName","user");
//        map.put("age", "24");
        Map<String, Object> map = ImmutableMap.of("userId", "1234", "userName", "小王");
        ArrayList<Map<String, Object>> objects = Lists.newArrayList(map);

        return objects;
//        List<Map<String, Object>> lists = new ArrayList<>();
//        Map<String, Object> map = new HashMap<>();
//        map.put("userId", "123");
//        map.put("userName", "like");
//        lists.add(map);
//        return lists;
    }
}
Start running localhost: 8080 / actuator / users

4 Summary

1 using a conventional configuration properties of the configuration
 2 extended health AbstractHealthIndicator class overrides methods inherited end
 3 custom endpoint @Component @Endpoint (id = "xxx" )

 

Guess you like

Origin www.cnblogs.com/my-ordinary/p/11570610.html