springboot actuator监控笔记
0 环境
系统:win10
编辑器:IDEA
1 概念
监控 管理自身信息(可以自定义) 的模块
2 文件配置
1 pom的配置
监控的添加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </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.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 启动 访问地址
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<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; } }
启动运行 localhost:8080/actuator/users
4 小结
1 使用现有的 进行配置 properties的配置 2 扩展健康端点 继承AbstractHealthIndicator类 重写方法 3 自定义端点 @Component @Endpoint(id = "xxx")
作者:以罗伊
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。