使用 Apache SkyWalking 进行 Spring Cloud 应用的分布式追踪与监控:完整教程

使用 Apache SkyWalking 进行 Spring Cloud 应用的分布式追踪与监控:完整教程

SkyWalking 是一款开源的分布式追踪、性能监控和诊断平台,适用于微服务、云原生和容器化应用。它能够在分布式系统中收集和分析各个服务之间的调用关系和性能数据,从而帮助开发者定位性能瓶颈和故障。Spring Cloud 是一组用于构建分布式系统的工具和框架,而将 SkyWalking 集成到 Spring Cloud 应用中可以帮助你实现对整个系统的监控和诊断。

准备工作

确保你已经安装并配置了以下工具和服务:

  • Java 8 或更高版本
  • Apache Maven
  • SkyWalking 后端服务,包括 SkyWalking OAP 和 SkyWalking UI

创建 Spring Cloud 项目

如果你还没有 Spring Cloud 项目,可以使用 Spring Initializr 创建一个新的 Spring Cloud 项目。确保选择 Spring Web 和 Spring Cloud 相关的依赖项。

你可以通过PowerShell(管理员)用以下命令来生成一个新的 Spring Cloud 项目:

powershell 复制代码
# 定义 Spring Initializr 的 URL 和输出文件名
$url = "https://start.spring.io/starter.zip"
$outputFile = "demo.zip"

# 定义 POST 请求的数据
$body = @{
    dependencies = "web,cloud-starter"
    groupId = "com.example"
    artifactId = "demo"
    packageName = "com.example.demo"
    name = "demo"
    description = "Demo project for Spring Boot"
    type = "maven-project"
	javaVersion = "11"
    bootVersion = "3.3.0"
}

# 将 POST 数据编码为 application/x-www-form-urlencoded 格式
$formFields = $body.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }
$formBody = [string]::Join("&", $formFields)

# 发送 POST 请求并下载文件
Invoke-WebRequest -Uri $url -Method POST -Body $formBody -ContentType "application/x-www-form-urlencoded" -OutFile $outputFile

# 检查是否成功下载
if (!(Test-Path $outputFile)) {
    Write-Host "Error: Failed to download $outputFile"
    exit 1
}

# 解压缩文件到当前目录
Expand-Archive -Path $outputFile -DestinationPath "demo"

# 删除压缩包
Remove-Item $outputFile

Write-Host "Project downloaded and extracted successfully."

添加 SkyWalking 依赖

在 pom.xml 文件中添加 SkyWalking 的依赖:

xml 复制代码
<dependencies>
    <!-- Other dependencies -->
    <dependency>
        <groupId>org.apache.skywalking</groupId>
        <artifactId>apm-agent-core</artifactId>
        <version>8.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.skywalking</groupId>
        <artifactId>apm-agent-spring-boot-starter</artifactId>
        <version>8.9.0</version>
    </dependency>
</dependencies>

配置 SkyWalking Agent

在 src/main/resources 目录下创建 skywalking-agent.config 配置文件,添加以下配置:

bash 复制代码
SkyWalking agent configurations
agent.service_name=${spring.application.name}
collector.backend_service=127.0.0.1:11800
logging.level=DEBUG

确保 collector.backend_service 指向你的 SkyWalking Collector 服务的地址和端口。

配置 Spring Cloud 应用

在 src/main/resources 目录下找到或创建 application.properties 文件,添加以下配置:

bash 复制代码
spring.application.name=my-spring-cloud-app
server.port=8080

准备 SkyWalking Agent

下载 SkyWalking Agent,并将其解压到一个目录。你可以从 SkyWalking 官方下载页面 获取最新版本。

解压后,你会得到一个 skywalking-agent 目录,其中包含 skywalking-agent.jar 和 config 目录。

启动 Spring Cloud 应用

在启动 Spring Cloud 应用时,使用 SkyWalking Agent 进行启动。你需要在启动命令中添加 JVM 参数来加载 SkyWalking Agent。

首先,确保 skywalking-agent.config 文件位于 config 目录中。

然后,通过以下命令启动你的 Spring Cloud 应用:

powershell 复制代码
java -javaagent:/path/to/skywalking-agent/skywalking-agent.jar -jar target/my-spring-cloud-app-0.0.1-SNAPSHOT.jar

验证集成

启动你的 Spring Cloud 应用后,访问 SkyWalking UI 界面(通常是 http://:/,默认端口为 8080)。你应该能够看到你的 Spring Cloud 应用的服务拓扑图和性能指标。

示例项目结构

下面是一个示例项目结构:

css 复制代码
my-spring-cloud-app
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── MySpringCloudApp.java
│   │   └── resources
│   │       ├── application.properties
│   │       └── skywalking-agent.config
├── pom.xml
└── skywalking-agent
    ├── skywalking-agent.jar
    └── config
        └── skywalking-agent.config

配置 SkyWalking OAP 和 UI

确保你的 SkyWalking OAP 和 UI 已经正确配置并运行。你可以参考 SkyWalking 官方文档来进行配置。

使用案例

1. 服务性能监控

通过 SkyWalking,可以监控服务的响应时间、错误率和吞吐量等性能指标,帮助开发者优化服务性能。

2. 故障定位

通过分布式追踪和调用链分析,可以快速定位故障发生的节点和原因,缩短故障修复时间。

3. 架构优化

通过服务拓扑图,可以直观地了解系统的架构,识别系统瓶颈和潜在的优化点。

参考资源

通过上述步骤,你可以将 SkyWalking 集成到你的 Spring Cloud 应用中,实现分布式系统的监控和诊断。

相关推荐
述雾学java8 分钟前
Spring Cloud Feign 整合 Sentinel 实现服务降级与熔断保护
java·spring cloud·sentinel
何苏三月3 小时前
SpringCloud系列 - Sentinel 服务保护(四)
spring·spring cloud·sentinel
麦兜*14 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
老三牛擦1 天前
熟练掌握RabbitMQ和Kafka的使用及相关应用场景。异步通知与解耦,流量削峰,配合本地消息表实现事务的最终一致性并解决消息可靠、顺序消费和错误重试等问题
skywalking
Fireworkitte2 天前
Apache POI 详解 - Java 操作 Excel/Word/PPT
java·apache·excel
蚂蚁数据AntData2 天前
从性能优化赛到社区Committer,走进赵宇捷在Apache Fory的成长之路
大数据·开源·apache·数据库架构
小湘西2 天前
Apache HttpClient 的请求模型和 I/O 类型
java·http·apache
白仑色2 天前
Spring Cloud 微服务(统一网关设计)
spring cloud·微服务·服务治理·统一配置管理·分布式配置中心
述雾学java2 天前
Spring Cloud 服务追踪实战:使用 Zipkin 构建分布式链路追踪
分布式·spring·spring cloud·zipkin
老三牛擦2 天前
熟悉多线程与并发编程,理解各类锁机制,熟悉JUC并发多线程及线程池,熟练异步编排编码,熟悉Redisson在分布式场景下各类锁的应用场景和并发控制原理。
skywalking