(3).定制Endpoint
(3.1).定制 Health 信息
在我们检测信息的时候,我们需要认为自定义的组件健康和默认的组件都健康的话,我们才认为是健康的。
- 第一种方式: 直接继承抽象类
我们只需要继承 AbstractHealthIndicator 类即可
package com.jsxs.health; import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.Status; import org.springframework.stereotype.Component; import java.util.HashMap; /** * @Author Jsxs * @Date 2023/7/27 12:32 * @PackageName:com.jsxs.health * @ClassName: MyComHealthIndicator * @Description: TODO * @Version 1.0 */ @Component public class MyComHealthIndicator extends AbstractHealthIndicator { /** * @param builder * @throws Exception 真实的检查方法 */ @Override protected void doHealthCheck(Health.Builder builder) throws Exception { HashMap<String, Object> map = new HashMap<>(); // 返回什么样的状态码,进行设置。 if (1 == 1) { // builder.up(); 和下面一样 都是表示健康的状态 builder.status(Status.UP); map.put("count", 1); map.put("ms", 100); } else { // builder.down(); builder.status(Status.OUT_OF_SERVICE); map.put("err", "链接超时"); map.put("ms", 3000); } // 返回详细信息 builder.withDetail("code", 100) .withDetails(map); } }
(3.2).定制 info 信息
第一种方式: 配置文件
双@符合之间获取的是 Pom.xml里面的信息
info: appName: boot-admin version: 2.0.1 mavenProjectName: @project.artifactId@ #使用@@可以获取maven的pom文件值 mavenProjectVersion: @project.version@
第二种方式: 编写代码
package com.jsxs.acautor.info; import org.springframework.boot.actuate.info.Info; import org.springframework.boot.actuate.info.InfoContributor; import org.springframework.stereotype.Component; import java.util.Collections; /** * @Author Jsxs * @Date 2023/7/27 13:35 * @PackageName:com.jsxs.acautor.info * @ClassName: InfoContributor * @Description: TODO * @Version 1.0 */ @Component public class ExampleInfoContributor implements InfoContributor { @Override public void contribute(Info.Builder builder) { builder.withDetail("example", Collections.singletonMap("key", "value")); } }
(3.3).定制Metrics信息
- SpringBoot支持自动适配的Metrics
- JVM metrics, report utilization of:
- Various memory and buffer pools
- Statistics related to garbage collection
- Threads utilization
- Number of classes loaded/unloaded
- CPU metrics
- File descriptor metrics
- Kafka consumer and producer metrics
- Log4j2 metrics: record the number of events logged to Log4j2 at each level
- Logback metrics: record the number of events logged to Logback at each level
- Uptime metrics: report a gauge for uptime and a fixed gauge representing the application’s absolute start time
- Tomcat metrics (server.tomcat.mbeanregistry.enabled must be set to true for all Tomcat metrics to be registered)
- Spring Integration metrics
- 增加定制Metrics
class MyService{ // 1. 设置计数的词为 Counter counter; // 2.利用有参构造的方法注入 public MyService(MeterRegistry meterRegistry){ counter = meterRegistry.counter("myservice.method.running.counter"); } // 在要统计的方法中添加如下函数 public void hello() { counter.increment(); } } //也可以使用下面的方式 @Bean MeterBinder queueSize(Queue queue) { return (registry) -> Gauge.builder("queueSize", queue::size).register(registry); }
(3.4).定制Endpoint
package com.jsxs.acautor.endoption; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; import org.springframework.stereotype.Component; import java.util.Collections; import java.util.Map; /** * @Author Jsxs * @Date 2023/7/29 10:22 * @PackageName:com.jsxs.acautor.endoption * @ClassName: DockerEndpoint * @Description: TODO * @Version 1.0 */ @Component @Endpoint(id = "container") //端点名叫什么 public class DockerEndpoint { // 端点的读操作 http://localhost:8080/actuator/container @ReadOperation public Map getDockerInfo(){ return Collections.singletonMap("info","docker started..."); } @WriteOperation private void restartDocker(){ System.out.println("docker restarted...."); } }
(4).可视化监控 SpringBootAdmin
http://docs.spring-boot-admin.com/current/getting-started.html
(4.1)SpringBootAdmin 管理端
新建SpringBootAdmin项目
1.先引入我们的依赖
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2. 在SpringBootAdmin的主类中开启监听
package com.example.springbootadmin; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableAdminServer @SpringBootApplication public class SpringBootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminApplication.class, args); } }
3.设置管理端的端口为 8888
(4.2).客户端 (被监控的一方)
1.客户端引入
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.3.1</version> </dependency>
2.客户端添加配置
spring: boot: admin: client: url: http://localhost:8888 #指向管理端的端口 # 暴漏所有的web断点 management: endpoints: enabled-by-default: true #暴露所有端点信息 web: exposure: include: '*' #以web方式暴露
3.映射域名和配置项目名
spring: boot: admin: client: url: http://localhost:8888 #指向管理端 instance: prefer-ip: true #使用ip注册进来 application: name: SpringBoot-ls-02