使用 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 应用中,实现分布式系统的监控和诊断。

相关推荐
大秦王多鱼7 小时前
Kafka SASL/SCRAM介绍
分布式·安全·kafka·apache
程序猿零零漆7 小时前
SpringCloud系列教程:微服务的未来(二十)Seata快速入门、部署TC服务、微服务集成Seata
java·spring·spring cloud·微服务
小小虫码19 小时前
项目中用的网关Gateway及SpringCloud
spring·spring cloud·gateway
拾忆,想起1 天前
如何选择Spring AOP的动态代理?JDK与CGLIB的适用场景
spring boot·后端·spring·spring cloud·微服务
Future_yzx1 天前
解析与使用 Apache HttpClient 进行网络请求和数据抓取
网络·apache
程序猿零零漆2 天前
SpringCloud系列教程:微服务的未来(十九)请求限流、线程隔离、Fallback、服务熔断
java·spring cloud·微服务
大秦王多鱼2 天前
Kafka 压缩算法详细介绍
运维·分布式·kafka·apache
TUTO_TUTO3 天前
【Wordpress网站制作】无法安装插件/主题等权限问题
linux·服务器·apache
大秦王多鱼3 天前
Kafka常见问题之 org.apache.kafka.common.errors.RecordTooLargeException
运维·分布式·kafka·apache
2的n次方_3 天前
Eureka 服务注册和服务发现的使用
spring boot·spring cloud·云原生·eureka·服务发现