Skip to content

Commit b2397fc

Browse files
author
YunaiV
committed
增加 spring cloud sleuth 链路追踪
1 parent c3e8aa3 commit b2397fc

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-0
lines changed

labx-13/labx-13-sc-sleuth-opentracing/src/main/resources/application.yml

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ spring:
1111
# Spring Cloud Sleuth 针对 Web 组件的配置项,例如说 SpringMVC
1212
web:
1313
enabled: true # 是否开启,默认为 true
14+
# Spring Cloud Sleuth 针对 OpenTracing 组件的配置项
15+
opentracing:
16+
enabled: true # 是否开启,默认为 true
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labx-13</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>labx-13-sc-sleuth-sampler</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
19+
</properties>
20+
21+
<!--
22+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
23+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
24+
-->
25+
<dependencyManagement>
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-parent</artifactId>
30+
<version>${spring.boot.version}</version>
31+
<type>pom</type>
32+
<scope>import</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.cloud</groupId>
36+
<artifactId>spring-cloud-dependencies</artifactId>
37+
<version>${spring.cloud.version}</version>
38+
<type>pom</type>
39+
<scope>import</scope>
40+
</dependency>
41+
</dependencies>
42+
</dependencyManagement>
43+
44+
<dependencies>
45+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-web</artifactId>
49+
</dependency>
50+
51+
<!-- 引入 Spring Cloud Sleuth + Zipkin 相关依赖,实现对它们的自动配置,从而实现链路追踪 -->
52+
<dependency>
53+
<groupId>org.springframework.cloud</groupId>
54+
<artifactId>spring-cloud-starter-zipkin</artifactId>
55+
</dependency>
56+
</dependencies>
57+
58+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springcloud.labx13.springmvcdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class UserServiceApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(UserServiceApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.iocoder.springcloud.labx13.springmvcdemo.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestParam;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
@RequestMapping("/user")
10+
public class UserController {
11+
12+
@GetMapping("/get")
13+
public String get(@RequestParam("id") Integer id) {
14+
return "user:" + id;
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
spring:
2+
application:
3+
name: user-service # 服务名
4+
5+
# Zipkin 配置项,对应 ZipkinProperties 类
6+
zipkin:
7+
base-url: http://127.0.0.1:9411 # Zipkin 服务的地址
8+
9+
# Spring Cloud Sleuth 配置项
10+
sleuth:
11+
# Spring Cloud Sleuth 针对 Web 组件的配置项,例如说 SpringMVC
12+
web:
13+
enabled: true # 是否开启,默认为 true
14+
# Spring Cloud Sleuth 针对抽样收集的配置项
15+
sampler:
16+
probability: 0.1 # 采样百分比,默认为 1.0 全部采样。
17+
rate: # 限流采样,即每秒可收集链路的数量,默认为 10。

labx-13/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
<module>labx-13-sc-sleuth-logback</module>
3232
<module>labx-13-sc-sleuth-opentracing</module>
33+
<module>labx-13-sc-sleuth-sampler</module>
3334
</modules>
3435

3536

0 commit comments

Comments
 (0)