springboot actuator监控笔记

0 环境

系统:win10
编辑器:IDEA

1 概念

监控 管理自身信息(可以自定义) 的模块

2 文件配置

1 pom的配置

监控的添加
<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
dependency>
详细配置
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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.8.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.examplegroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demoname>
    <description>Demo project for Spring Bootdescription>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>com.google.guavagroupId>
            <artifactId>guavaartifactId>
            <version>20.0version>
        dependency>
    dependencies>

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

project>

2 启动 访问地址

locahost:8080/actuator/health

3 展示更加详细 添加properties

# 展示信心详情
 management.endpoint.health.show-details=always
# 手动配置要暴露的端点(其他的端点就是封装的 不被访问)
# management.endpoints.web.exposure.include=configprops,beans
# 全部端点暴露
# 尤其是自定义端点时 为了省事 可以先这样做
# 当然也可以用上面的方式
management.endpoints.web.exposure.include=*
和上面的2一样

3 自定义actuator端点

1 概述

一种是扩展健康端点 另一种自定义端点

2 扩展健康端点

继承AbstractHealthIndicator 只需要重写即可
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 自定义端点

自定义配置 配置一下端口暴露
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> health(){
        //
//        ImmutableMap map = ImmutableMap.of("userId", "1234", "userName", "小王");
//        map.asMultimap()
//        ImmutableList> of = ImmutableList.of(map);
//        System.out.println(of.asList());
//        Map map = newHashMap();
//        map.put("userId","1234");
//        map.put("userName","user");
//        map.put("age", "24");
        Map map = ImmutableMap.of("userId", "1234", "userName", "小王");
        ArrayList> objects = Lists.newArrayList(map);

        return objects;
//        List> lists = new ArrayList<>();
//        Map map = new HashMap<>();
//        map.put("userId", "123");
//        map.put("userName", "like");
//        lists.add(map);
//        return lists;
    }
}
启动运行 localhost:8080/actuator/users

4 小结

1 使用现有的 进行配置 properties的配置
2 扩展健康端点 继承AbstractHealthIndicator类 重写方法
3 自定义端点 @Component @Endpoint(id = "xxx")

 

转载于:https://www.cnblogs.com/my-ordinary/p/11570610.html

你可能感兴趣的:(springboot actuator监控笔记)