实操 | 如何通过 SOFALookout & Prometheus 监控 SpringBoot 项目

本文涉及的产品
可观测监控 Prometheus 版,每月50GB免费额度
EMR Serverless StarRocks,5000CU*H 48000GB*H
简介: SOFA (Scalable Open Financial Architecture ) 是蚂蚁金服自主研发的金融级分布式中间件,包含了构建金融级云原生架构所需的各个组件,在蚂蚁金服内部经过了将近 10 年的实践以及发展。

SOFA (Scalable Open Financial Architecture ) 是蚂蚁金服自主研发的金融级分布式中间件,包含了构建金融级云原生架构所需的各个组件,在蚂蚁金服内部经过了将近 10 年的实践以及发展。

SOFA 目前包含了微服务研发框架,RPC 框架,服务注册中心,分布式定时任务,限流/熔断框架,动态配置推送,分布式链路追踪,Metrics 监控度量,分布式高可用消息队列,分布式事务框架,分布式数据库代理层,Service Mesh 等组件,是一套分布式架构的完整解决方案,也是在金融场景里锤炼出来的最佳实践。

本文知识点:

SOFALookout , 是一个利用多维度的 Metrics 对目标系统进行度量和监控的项目,SOFALookout 项目分为客户端部分与服务器端部分。

由于服务器端代码暂未开源,本文将手把手教你如何将 SOFALookout 的客户端采集到的 Metrics 上报给 Prometheus 进行监控。

SOFALookout :https://github.com/alipay/sofa-lookout

实操:

通过四步,实现通过 SOFALookout & Prometheus 监控 SpringBoot 项目

1. 创建个简单 SpringBoot 的 demo 项目

创建 demo 工程

可以通过 http://start.spring.io/ 创建个 demo 项目(附加个 Web 模块便于演示)。 或者也可以本地快速构建项目,在 *nix系统创建项目结构 mkdir -p demo/src/main/java/hello:

demo
└── src
    └── main
        └── java
            └── hello

创建个 pom.xml 文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.alipay.sofa</groupId>
    <artifactId>lookout-prom-integation-demo</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

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

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

创建个启动类

在 hello 的包下创建 Application.java文件

package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  1. 配置生效 Prometheus 的 Exports
    在 pom.xml 中添加依赖

增加 "lookout-sofa-boot-starter","lookout-reg-prometheus" 两个模块的依赖,版本用最新的(这里以 1.4.1 为例)

<dependency>
     <groupId>com.alipay.sofa.lookout</groupId>
     <artifactId>lookout-sofa-boot-starter</artifactId>
     <version>1.4.1</version></dependency><dependency>
     <groupId>com.alipay.sofa.lookout</groupId>
     <artifactId>lookout-reg-prometheus</artifactId>
     <version>1.4.1</version>
</dependency>

新增应用配置文件

首先创建个资源目录mkdir -p demo/src/main/resources,然后新建个 application.properties 文件(或 YAML 文件)。

echo "spring.application.name=demo" > demo/src/main/resources/application.properties
需要注意的是应用名是必须指定的!

检查启动状态

经过上面简单的配置,就可以运行 demo 了,并访问 http://localhost:9494进行确认

  demo curl http://localhost:9494/
<html><head><title>Lookout Client Exporter</title></head><body><h1>Lookout Client Exporter</h1><p><a href="/metrics">Metrics</a></p></body></html>%

默认 Jvm 的相关指标已经可以获得了。

  1. 部署 Prometheus 服务
    确认 demo 应用在 9494 端口已经正常提供服务后,就可以编辑一个 prometheus.yml 来抓取该 demo 项目信息,假设本机 IP 地址为 10.15.232.101,那么可以配置如下的 prometheus.yml:
scrape_configs:
  - job_name:       'lookout-client'
    scrape_interval: 5s
    static_configs:
      - targets: ['10.15.232.101:9494']

目标的 IP 地址可以是LAN地址,不要是 localhost,要保证 prometheus 容器内部可以访问到。

有了上面的配置文件之后,可以再到本地通过 Docker 来启动 Prometheus:

docker run -d -p 9090:9090 -v $PWD/prometheus.yml:/etc/prometheus/prometheus.yml\
  --name prom prom/prometheus:master

然后通过浏览器访问: http://localhost:9090,再通过 PromQL 查询即可查询到对应的 Metrics (比如:http://localhost:9090/graph?g0.range_input=1h&g0.expr=jvm_memory_heap_used&g0.tab=0)

image.png

客户端更多内置的 Metrics 指标 (https://github.com/alipay/sofa-lookout/wiki/client-ext-metrics)

  1. 通过 Lookout SDK 新增业务埋点
    下面我们演示如何统计某个 web 服务被请求的次数,首页在 demo 应用中新增个 RestController 代码如下:
...
import com.alipay.lookout.api.*;
@Autowired
private Registry registry;
@GetMapping("/echo/{words}")

public String echo(@PathVariable String words) {
    Id id = registry.createId("http_requests_total")
            .withTag("host", NetworkUtil.getLocalAddress().getHostName());
    registry.counter(id).inc();
    return words;
}

重启应用,并访问:

  demo curl http://localhost:8080/echo/hello
hello%   

每访问一次,请求计数器自增一次。然后我们可以在 Prometheus 控制台进行查看(时间跨度可以选择短一点,比如 1~5 分钟)。

image.png

总结:

以上内容演示了 Count 型 Metrics 的使用,更多使用说明和 Metrics 类型可以参考 SOFALookout (https://github.com/alipay/sofa-lookout) 的 WIKI 文档。

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
目录
相关文章
|
22天前
|
前端开发 JavaScript Java
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
本文介绍了一个结合SpringBoot和Python的实用功能,旨在监控iPhone 15的库存状态并通过邮件提醒用户。系统采用SpringBoot监听苹果官网API,解析JSON数据判断是否有货,并展示最近的库存记录。此外,还能自动触发Selenium+Python脚本实现自动化购买。文中详细介绍了技术栈、接口分析、邮件配置及自动化脚本的设置方法。该项目不仅适用于熟悉后端开发的人员,也适合回顾Layui和Jquery等前端技术。
39 0
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
|
1月前
|
Prometheus 监控 Cloud Native
【监控】prometheus传统环境监控告警常用配置
【监控】prometheus传统环境监控告警常用配置
【监控】prometheus传统环境监控告警常用配置
|
5天前
|
Prometheus 监控 Cloud Native
docker安装prometheus+Granfan并监控容器
【9月更文挑战第14天】本文介绍了在Docker中安装Prometheus与Grafana并监控容器的步骤,包括创建配置文件、运行Prometheus与Grafana容器,以及在Grafana中配置数据源和创建监控仪表盘,展示了如何通过Prometheus抓取数据并利用Grafana展示容器的CPU使用率等关键指标。
消息中间件 缓存 监控
36 0
|
23天前
|
存储 Prometheus 监控
Grafana 与 Prometheus 集成:打造高效监控系统
【8月更文第29天】在现代软件开发和运维领域,监控系统已成为不可或缺的一部分。Prometheus 和 Grafana 作为两个非常流行且互补的开源工具,可以协同工作来构建强大的实时监控解决方案。Prometheus 负责收集和存储时间序列数据,而 Grafana 则提供直观的数据可视化功能。本文将详细介绍如何集成这两个工具,构建一个高效、灵活的监控系统。
100 1
|
23天前
|
Prometheus 监控 Cloud Native
Spring Boot 性能护航!Prometheus、Grafana、ELK 组合拳,点燃数字化时代应用稳定之火
【8月更文挑战第29天】在现代软件开发中,保证应用性能与稳定至关重要。Spring Boot 作为流行的 Java 框架,结合 Prometheus、Grafana 和 ELK 可显著提升监控与分析能力。Prometheus 负责收集时间序列数据,Grafana 将数据可视化,而 ELK (Elasticsearch、Logstash、Kibana)则管理并分析应用日志。通过具体实例演示了如何在 Spring Boot 应用中集成这些工具:配置 Prometheus 获取度量信息、Grafana 显示结果及 ELK 分析日志,从而帮助开发者快速定位问题,确保应用稳定高效运行。
37 1
|
26天前
|
Prometheus 监控 Cloud Native
使用Prometheus搞定微服务监控
使用Prometheus搞定微服务监控
使用Prometheus搞定微服务监控
|
1月前
|
Prometheus Kubernetes 监控
Kubernetes(K8S) 监控 Prometheus + Grafana
Kubernetes(K8S) 监控 Prometheus + Grafana
123 2
|
1月前
|
Prometheus 监控 Cloud Native
在 HBase 集群中,Prometheus 通常监控哪些类型的性能指标?
在 HBase 集群中,Prometheus 通常监控哪些类型的性能指标?
|
21天前
|
Java Spring 监控
Spring Boot Actuator:守护你的应用心跳,让监控变得触手可及!
【8月更文挑战第31天】Spring Boot Actuator 是 Spring Boot 框架的核心模块之一,提供了生产就绪的特性,用于监控和管理 Spring Boot 应用程序。通过 Actuator,开发者可以轻松访问应用内部状态、执行健康检查、收集度量指标等。启用 Actuator 需在 `pom.xml` 中添加 `spring-boot-starter-actuator` 依赖,并通过配置文件调整端点暴露和安全性。Actuator 还支持与外部监控工具(如 Prometheus)集成,实现全面的应用性能监控。正确配置 Actuator 可显著提升应用的稳定性和安全性。
42 0