Java --- springboot3之可观测性

目录

一、可观测性

二、定制健康监控的端点

三、定制metrics

四、整合Prometheus+Grafana


一、可观测性

 导入pom依赖

 
            org.springframework.boot
            spring-boot-starter-actuator
        

通过springboot配置文件配置暴露端点

#通过web暴露所有监控端点
management.endpoints.web.exposure.include=*

通过访问地址得到所有http://localhost:8080/actuator

Java --- springboot3之可观测性_第1张图片

二、定制健康监控的端点

@Component
public class MyComponent {
    public int check(){
        return 1;
    }
}
@Component
public class MyHealthIndicator extends AbstractHealthIndicator {
    @Autowired
    private MyComponent myComponent;
    /**
     * 检查健康
     * @param builder
     * @throws Exception
     */
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        //自定义检查方法
        int check = myComponent.check();
        if (check == 1){
            //存活
            builder.up()
                    .withDetail("code","200")
                    .withDetail("msg","健康的运行")
                    .build();
        }else {
            builder.down()
                    .withDetail("code","400")
                    .withDetail("msg","运行崩溃了")
                    .build();
        }
    }
}

配置文件 

management.endpoint.health.enabled=true
management.endpoint.health.show-details=always

 效果图Java --- springboot3之可观测性_第2张图片

三、定制metrics

@Component
public class MyComponent {
    Counter counter = null;
    public MyComponent(MeterRegistry meterRegistry) {
        counter = meterRegistry.counter("My.hello");
    }

    public int check(){
        return 1;
    }
    public void hello(){
        System.out.println("hello");
        counter.increment();
    }
}
@RestController
public class HelloController {
    @Autowired
    private MyComponent myComponent;
    @GetMapping("/hello")
    public void hello(){
        myComponent.hello();
    }
}

效果图

Java --- springboot3之可观测性_第3张图片

四、整合Prometheus+Grafana

导入Prometheus需要的依赖


            io.micrometer
            micrometer-registry-prometheus
        

 将自己的Java程序打包上传到服务器部署

安装服务器上传工具

安装指令:yum install lrzsz

软件传输指令:rz

 安装liunx的jdk17

下载jdk17:wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz

创建安装目录:mkdir -p /opt/java17

解压:tar -xzf jdk-17_linux-x64_bin.tar.gz -C /opt/java17/

修改文件:vim /etc/profile

修改内容:

export JAVA_HOME=/opt/java/jdk-17.0.7
export PATH=$PATH:$JAVA_HOME/bin

指行生效命令:source /etc/profile

后台运行Java服务

nohup java -jar boot3-actuator-0.0.1-SNAPSHOT.jar > output.log 2>&1 & 

修改 Prometheus配置文件

scrape_configs:
  - job_name: 'spring-boot-actuator-exporter'
    metrics_path: '/actuator/prometheus' #指定抓取的路径
    static_configs:
      - targets: ['192.168.200.1:8001']
        labels:
          nodename: 'app-demo'

测试访问:

 整合Grafana

Java --- springboot3之可观测性_第4张图片

 Java --- springboot3之可观测性_第5张图片

你可能感兴趣的:(springboot3,java,数学建模,开发语言)