游侠的博客 游侠的博客
首页
  • 论文笔记
  • 一些小知识点

    • pytorch、numpy、pandas函数简易解释
  • 《深度学习500问》
开发
技术
更多
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Ranger

一名在校研究生
首页
  • 论文笔记
  • 一些小知识点

    • pytorch、numpy、pandas函数简易解释
  • 《深度学习500问》
开发
技术
更多
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Vue

  • SpringBoot2

  • JavaWeb

  • SSM

  • SpringBoot3

    • 一、SpringBoot3快速入门
    • 二、SpringBoot核心机制了解
    • 三、SpringBoot的核心技能
    • 四、WebMvcAutoConfiguration原理
    • 五、SpringBootWeb开发-Web场景
    • 六、SpringBootWeb开发-静态资源与路径匹配
    • 七、SpringBootWeb开发-内容协商
    • 八、SpringBootWeb开发-模板引擎
    • 九、SpringBootWeb开发-国际化与错误处理
    • 十、SpringBootWeb开发-嵌入式容器
    • 十一、SpringBootWeb开发-SpringMVC所有常用特性配置
    • 十二、SpringBootWeb开发-Web新特性
    • 十三、SpringBoot-整合SSM场景
    • 十四、SpringBoot-基础特性
    • 十五、SpringBoot-核心原理
    • 十六、SpringBoot场景整合-环境准备
    • 十七、SpringBoot场景整合-Redis
    • 十八、SpringBoot场景整合-接口文档
    • 十九、SpringBoot场景整合-远程调用
    • 二十、SpringBoot场景整合-消息服务
    • 二十一、SpringBoot场景整合-Web安全
    • 二十二、SpringBoot场景整合-可观测性
      • 1. SpringBoot Actuator
        • 1. 实战
        • 1. 场景引入
        • 2. 暴露指标
        • 3. 访问数据
        • 2. Endpoint
        • 1. 常用端点
        • 2. 定制端点
        • 1. HealthEndpoint
        • 2. MetricsEndpoint
      • 2. 监控案例落地
        • 1. 安装 Prometheus + Grafana
        • 2. 导入依赖
        • 3. 配置 Prometheus 拉取数据
        • 4. 配置 Grafana 监控面板
        • 5. 效果
    • 二十三、SpringBoot场景整合-AOT
  • 技术
  • SpringBoot3
yangzhixuan
2023-06-16
目录

二十二、SpringBoot场景整合-可观测性

可观测性 Observability

对线上应用进行观测、监控、预警...

  • 健康状况【组件状态、存活状态】Health
  • 运行指标【cpu、内存、垃圾回收、吞吐量、响应成功率...】Metrics
  • 链路追踪
  • ...

# 1. SpringBoot Actuator

# 1. 实战

# 1. 场景引入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1
2
3
4

# 2. 暴露指标

management:
  endpoints:
    enabled-by-default: true #暴露所有端点信息
    web:
      exposure:
        include: '*'  #以web方式暴露所有监控端点
1
2
3
4
5
6

# 3. 访问数据

  • 访问 http://localhost:8080/actuator (opens new window);展示出所有可以用的监控端点
  • http://localhost:8080/actuator/beans
  • http://localhost:8080/actuator/configprops
  • http://localhost:8080/actuator/metrics
  • http://localhost:8080/actuator/metrics/jvm.gc.pause
  • http://localhost:8080/actuator/ (opens new window)endpointName/detailPath

# 2. Endpoint

# 1. 常用端点

ID 描述
auditevents 暴露当前应用程序的审核事件信息。需要一个AuditEventRepository组件。
beans 显示应用程序中所有Spring Bean的完整列表。
caches 暴露可用的缓存。
conditions 显示自动配置的所有条件信息,包括匹配或不匹配的原因。
configprops 显示所有@ConfigurationProperties。
env 暴露Spring的属性ConfigurableEnvironment
flyway 显示已应用的所有Flyway数据库迁移。 需要一个或多个Flyway组件。
health 显示应用程序运行状况信息。
httptrace 显示HTTP跟踪信息(默认情况下,最近100个HTTP请求-响应)。需要一个HttpTraceRepository组件。
info 显示应用程序信息。
integrationgraph 显示Spring integrationgraph 。需要依赖spring-integration-core。
loggers 显示和修改应用程序中日志的配置。
liquibase 显示已应用的所有Liquibase数据库迁移。需要一个或多个Liquibase组件。
metrics 显示当前应用程序的“指标”信息。
mappings 显示所有@RequestMapping路径列表。
scheduledtasks 显示应用程序中的计划任务。
sessions 允许从Spring Session支持的会话存储中检索和删除用户会话。需要使用Spring Session的基于Servlet的Web应用程序。
shutdown 使应用程序正常关闭。默认禁用。
startup 显示由ApplicationStartup收集的启动步骤数据。需要使用SpringApplication进行配置BufferingApplicationStartup。
threaddump 执行线程转储。
heapdump 返回hprof堆转储文件。
jolokia 通过HTTP暴露JMX bean(需要引入Jolokia,不适用于WebFlux)。需要引入依赖jolokia-core。
logfile 返回日志文件的内容(如果已设置logging.file.name或logging.file.path属性)。支持使用HTTPRange标头来检索部分日志文件的内容。
prometheus 以Prometheus服务器可以抓取的格式公开指标。需要依赖micrometer-registry-prometheus。
threaddump`、`heapdump`、`metrics
1

# 2. 定制端点

  • 健康监控:返回存活、死亡
  • 指标监控:次数、率
# 1. HealthEndpoint
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

}

构建Health
Health build = Health.down()
                .withDetail("msg", "error service") //详细信息,可以放多个
                .withDetail("code", "500")
                .withException(new RuntimeException())
                .build();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
management:
    health:
      enabled: true
      show-details: always #总是显示详细信息。可显示每个模块的状态信息
1
2
3
4
@Component
public class MyComHealthIndicator extends AbstractHealthIndicator {

    /**
     * 真实的检查方法
     * @param builder
     * @throws Exception
     */
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        //mongodb。  获取连接进行测试
        Map<String,Object> map = new HashMap<>();
        // 检查完成
        if(1 == 2){
//            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);

    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 2. MetricsEndpoint
class MyService{
    Counter counter;
    public MyService(MeterRegistry meterRegistry){
         counter = meterRegistry.counter("myservice.method.running.counter");
    }

    public void hello() {
        counter.increment();
    }
}

1
2
3
4
5
6
7
8
9
10
11

# 2. 监控案例落地

基于 Prometheus + Grafana

时序数据库:数据是按照时间维度存储在数据库中的

Grafana提供一个可视化界面

image

# 1. 安装 Prometheus + Grafana

#安装prometheus:时序数据库
docker run -p 9090:9090 -d \
-v pc:/etc/prometheus \
prom/prometheus

#安装grafana;默认账号密码 admin:admin
docker run -d --name=grafana -p 3000:3000 grafana/grafana
1
2
3
4
5
6
7

# 2. 导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.10.6</version>
</dependency>
1
2
3
4
5
6
7
8
9
management:
  endpoints:
    web:
      exposure: #暴露所有监控的端点
        include: '*'
1
2
3
4
5

访问: http://localhost:8001/actuator/prometheus 验证,返回 prometheus 格式的所有指标

部署Java应用

#安装上传工具
#输入rz就能上传本地文件到数据库

yum install lrzsz

#安装openjdk
# 下载openjdk
wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz

mkdir -p /opt/java
tar -xzf jdk-17_linux-x64_bin.tar.gz -C /opt/java/
sudo vi /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-14-actuator-0.0.1-SNAPSHOT.jar > output.log 2>&1 &

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

确认可以访问到: http://8.130.32.70:9999/actuator/prometheus

# 3. 配置 Prometheus 拉取数据

## 修改 prometheus.yml 配置文件
scrape_configs:
  - job_name: 'spring-boot-actuator-exporter'
    metrics_path: '/actuator/prometheus' #指定抓取的路径
    static_configs:
      - targets: ['192.168.200.1:8001']
        labels:
          nodename: 'app-demo'
1
2
3
4
5
6
7
8

# 4. 配置 Grafana 监控面板

  • 添加数据源(Prometheus)
  • 添加面板。可去 dashboard 市场找一个自己喜欢的面板,也可以自己开发面板;Dashboards | Grafana Labs (opens new window)

# 5. 效果

image

编辑 (opens new window)
上次更新: 2024/05/30, 07:49:34
二十一、SpringBoot场景整合-Web安全
二十三、SpringBoot场景整合-AOT

← 二十一、SpringBoot场景整合-Web安全 二十三、SpringBoot场景整合-AOT→

最近更新
01
tensor比较大小函数
05-30
02
Large Language Models can Deliver Accurate and Interpretable Time Series Anomaly Detection
05-27
03
半监督学习经典方法 Π-model、Mean Teacher
04-10
更多文章>
Theme by Vdoing | Copyright © 2023-2024 Ranger | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式