SpringBoot集成CAT监控

SpringBoot 集成美团点评的 CAT(Central Application Tracking)监控系统,可以帮助你实现高性能的应用监控和问题诊断。

以下是完整的集成步骤:


1. 引入依赖

pom.xml 中添加 CAT 的 Maven 依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependencies>
<!-- CAT 客户端 目前没有仓库下载,需要自行编译放到自己仓库-->
<dependency>
<groupId>com.dianping.cat</groupId>
<artifactId>cat-client</artifactId>
<version>3.0.0</version> <!-- 具体版本根据需求调整 -->
</dependency>

<!-- 如果使用 Spring Boot AOP 监控方法,可以添加 AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
</dependencies>

可以使用Maven的install:install-file命令

1
mvn install:install-file -Dfile=路径到你的jar文件 -DgroupId=你的项目组ID -DartifactId=你的项目ID -Dversion=版本号 -Dpackaging=jar

例如:

1
mvn install:install-file -Dfile=path/to/cat-client-3.0.0.jar -DgroupId=com.dianping.cat -DartifactId=cat-client -Dversion=3.0.0 -Dpackaging=jar

2. 配置 CAT

2.1 配置 CAT_HOME

创建目录并设置环境变量:

1
2
mkdir -p /data/appdatas/cat
export CAT_HOME=/data/appdatas/cat

2.2 添加 client.xml

src/main/resources 目录下创建 META-INF/app.properties

1
app.name=your-app-name

然后,在 src/main/resources/META-INF/ 目录下创建 client.xml

1
2
3
4
5
<config>
<servers>
<server ip="127.0.0.1" port="2280"/>
</servers>
</config>

说明:

  • app.name 需要和 client.xml 里面的 app.name 保持一致。
  • server ip="127.0.0.1" 表示 CAT 服务器地址(可以更改为实际的 CAT 服务器 IP)。
  • port="2280" 为 CAT 服务器的端口。

3. 启动 CAT 服务器

如果你没有搭建 CAT 服务器,可以使用 Docker 快速部署:

1
docker run -d --name cat-server -p 8080:8080 -p 2280:2280 dianping/cat

或者手动安装 CAT 服务器(通常基于 Tomcat 部署)。


4. 在代码中使用 CAT

4.1 监控 API 请求

在 Controller 层使用 CAT 监控:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@RestController
@RequestMapping("/api")
public class CatDemoController {

@GetMapping("/test")
public String test() {
Transaction t = Cat.newTransaction("URL", "/api/test");
try {
// 业务逻辑
String result = "Hello CAT!";
t.setStatus(Transaction.SUCCESS);
return result;
} catch (Exception e) {
Cat.logError(e);
t.setStatus(e);
throw e;
} finally {
t.complete();
}
}
}

4.2 监控 Service 方法

如果要在 Service 层添加监控,可以使用 AOP 或手动埋点:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Service
public class CatDemoService {

public String doSomething() {
Transaction t = Cat.newTransaction("Service", "doSomething");
try {
// 业务逻辑
String result = "Processed!";
t.setStatus(Transaction.SUCCESS);
return result;
} catch (Exception e) {
Cat.logError(e);
t.setStatus(e);
throw e;
} finally {
t.complete();
}
}
}

5. 通过 AOP 自动埋点

使用 Spring AOP 进行自动埋点,减少手动添加 Transaction 的工作量:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Aspect
@Component
public class CatTransactionAspect {

@Around("execution(* com.example.service..*(..))") // 监控 service 包下所有方法
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
String methodName = joinPoint.getSignature().toShortString();
Transaction t = Cat.newTransaction("Service", methodName);
try {
Object result = joinPoint.proceed();
t.setStatus(Transaction.SUCCESS);
return result;
} catch (Exception e) {
Cat.logError(e);
t.setStatus(e);
throw e;
} finally {
t.complete();
}
}
}

6. 监控 SQL、Redis、HTTP

CAT 还支持自动监控数据库、Redis、HTTP 请求等:

6.1 监控 SQL

1
2
3
4
5
6
7
8
9
10
11
12
public void executeSql() {
Transaction t = Cat.newTransaction("SQL", "select * from users");
try {
// 执行 SQL
t.setStatus(Transaction.SUCCESS);
} catch (Exception e) {
Cat.logError(e);
t.setStatus(e);
} finally {
t.complete();
}
}

6.2 监控 Redis

1
2
3
4
5
6
7
8
9
10
11
12
public void queryRedis(String key) {
Transaction t = Cat.newTransaction("Redis", "get:" + key);
try {
// 执行 Redis 查询
t.setStatus(Transaction.SUCCESS);
} catch (Exception e) {
Cat.logError(e);
t.setStatus(e);
} finally {
t.complete();
}
}

6.3 监控 HTTP 请求

1
2
3
4
5
6
7
8
9
10
11
12
public void callExternalAPI() {
Transaction t = Cat.newTransaction("HTTP", "http://example.com/api");
try {
// 发送 HTTP 请求
t.setStatus(Transaction.SUCCESS);
} catch (Exception e) {
Cat.logError(e);
t.setStatus(e);
} finally {
t.complete();
}
}

7. 启动项目,验证 CAT 监控

启动 Spring Boot 项目后,访问 http://localhost:8080/cat 可以进入 CAT 管理页面,查看上报的监控数据。


8. 常见问题

1. No Server Available 错误

  • 确保 client.xml 配置正确,并且 server ip 指向正确的 CAT 服务器。
  • 确保 CAT 服务器已正常运行。

2. CAT_HOME 目录未创建

  • 确保

    1
    CAT_HOME

    目录存在:

    1
    mkdir -p /data/appdatas/cat
  • 在启动 Spring Boot 之前,确保 CAT_HOME 环境变量正确设置。

3. 监控数据未上报

  • 确保 META-INF/app.properties 文件的 app.name 配置正确。
  • 确保 client.xml 指定了正确的 server ipport
  • 检查 logs/cat.log 是否有错误日志。

总结

通过上述步骤,Spring Boot 就可以成功集成美团 CAT 进行高性能监控。你可以在 Controller、Service、SQL、Redis 以及 HTTP 请求等关键位置埋点,以便实时监控应用状态,提高系统的稳定性和可观测性。

如果你的项目是微服务架构,也可以使用 CAT-HTTP-TRANSACTION 记录微服务调用链,实现全链路追踪。🚀

作者

wonderomg

发布于

2021-03-15

更新于

2026-06-28

许可协议

评论