Skip to content

Commit 611c798

Browse files
author
YunaiV
committed
增加 spring cloud sleuth 链路追踪
1 parent 73bef7c commit 611c798

File tree

10 files changed

+223
-2
lines changed

10 files changed

+223
-2
lines changed

labx-12/labx-12-sc-config-user-application-nacos/src/main/resources/bootstrap.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ spring:
77
config:
88
name: ${spring.application.name} # 读取的配置文件的名字,默认为 ${spring.application.name}
99
discovery:
10-
enabled: true
11-
service-id: demo-config-server
10+
enabled: true # 是否使用注册发现,获取配置中心的地址,默认为 false
11+
service-id: demo-config-server # 配置中心的服务名
1212

1313
# Spring Cloud Nacos Discovery 相关配置项
1414
nacos:
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-springmvc</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+
<!-- TODO -->
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,13 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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-zipkin-server-demo-in-memory</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+
<sleuth.version>2.2.2.RELEASE</sleuth.version>
20+
</properties>
21+
22+
<!--
23+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
24+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
25+
-->
26+
<dependencyManagement>
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-parent</artifactId>
31+
<version>${spring.boot.version}</version>
32+
<type>pom</type>
33+
<scope>import</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-dependencies</artifactId>
38+
<version>${spring.cloud.version}</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
<!-- <dependency>-->
43+
<!-- <groupId>org.springframework.cloud</groupId>-->
44+
<!-- <artifactId>spring-cloud-starter-sleuth</artifactId>-->
45+
<!-- <version>${sleuth.version}</version>-->
46+
<!-- <type>pom</type>-->
47+
<!-- <scope>import</scope>-->
48+
<!-- </dependency>-->
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<dependencies>
53+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-web</artifactId>
57+
</dependency>
58+
59+
<!-- Zipkin 服务器 -->
60+
<dependency>
61+
<groupId>io.zipkin.java</groupId>
62+
<artifactId>zipkin-server</artifactId>
63+
<version>2.12.9</version>
64+
</dependency>
65+
<!-- Zipkin UI -->
66+
<dependency>
67+
<groupId>io.zipkin.java</groupId>
68+
<artifactId>zipkin-autoconfigure-ui</artifactId>
69+
<version>2.12.9</version>
70+
</dependency>
71+
72+
<dependency>
73+
<groupId>org.springframework.cloud</groupId>
74+
<artifactId>spring-cloud-starter-sleuth</artifactId>
75+
<version>2.2.2.RELEASE</version>
76+
</dependency>
77+
78+
</dependencies>
79+
80+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.iocoder.springcloud.labx13.sleuthzipkinserverdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import zipkin2.server.internal.EnableZipkinServer;
6+
7+
@SpringBootApplication
8+
@EnableZipkinServer
9+
public class SleuthZipkinServerApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SleuthZipkinServerApplication.class, args);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
server:
2+
port: 9411
3+
4+

labx-13/pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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>labs-parent</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</artifactId>
13+
<packaging>pom</packaging>
14+
<modules>
15+
<!-- <module>labx-13-sc-sleuth-zipkin-server-demo-in-memory</module>-->
16+
<module>labx-13-sc-sleuth-springmvc</module>
17+
</modules>
18+
19+
20+
</project>

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<module>labx-10</module>
7474
<module>labx-11</module>
7575
<module>labx-12</module>
76+
<module>labx-13</module>
7677

7778
<!-- Dubbo 示例 -->
7879
</modules>

0 commit comments

Comments
 (0)