Skip to content

Commit 52b4a2d

Browse files
author
YunaiV
committed
增加 zipkin 对 dubbo 的链路追踪
1 parent 24ce2e0 commit 52b4a2d

File tree

8 files changed

+250
-3
lines changed

8 files changed

+250
-3
lines changed

lab-40/lab-40-zipkin-dubbo/lab-40-zipkin-dubbo-consumer/pom.xml

+37
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@
5050
<version>2.13.0</version>
5151
</dependency>
5252

53+
<!-- Brave 核心库 -->
54+
<!-- The below are needed to report traces to http://localhost:9411/api/v2/spans -->
55+
<dependency>
56+
<groupId>io.zipkin.brave</groupId>
57+
<artifactId>brave</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>io.zipkin.reporter2</groupId>
61+
<artifactId>zipkin-sender-okhttp3</artifactId>
62+
</dependency>
63+
64+
<!-- Adds the MVC class and method names to server spans -->
65+
<!-- Brave 对 Spring MVC 的支持 -->
66+
<dependency>
67+
<groupId>io.zipkin.brave</groupId>
68+
<artifactId>brave-instrumentation-spring-webmvc</artifactId>
69+
</dependency>
70+
71+
<!-- Brave 针对 Dubbo 的插件,实现链路追踪 -->
72+
<dependency>
73+
<groupId>io.zipkin.brave</groupId>
74+
<artifactId>brave-instrumentation-dubbo</artifactId>
75+
<version>5.10.1</version>
76+
</dependency>
5377
</dependencies>
5478

79+
<dependencyManagement>
80+
<!-- Brave Bom 文件 -->
81+
<dependencies>
82+
<dependency>
83+
<groupId>io.zipkin.brave</groupId>
84+
<artifactId>brave-bom</artifactId>
85+
<version>5.9.1</version>
86+
<type>pom</type>
87+
<scope>import</scope>
88+
</dependency>
89+
</dependencies>
90+
</dependencyManagement>
91+
5592
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.iocoder.springboot.lab40.zpkindemo.consumerdemo.config;
2+
3+
import brave.spring.webmvc.SpanCustomizingAsyncHandlerInterceptor;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Import;
7+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
9+
10+
@Configuration
11+
@Import(SpanCustomizingAsyncHandlerInterceptor.class)
12+
public class SpringMvcConfiguration implements WebMvcConfigurer {
13+
14+
@Autowired
15+
public SpanCustomizingAsyncHandlerInterceptor webMvcTracingCustomizer;
16+
17+
/**
18+
* Decorates server spans with application-defined web tags
19+
*/
20+
@Override
21+
public void addInterceptors(InterceptorRegistry registry) {
22+
registry.addInterceptor(webMvcTracingCustomizer);
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package cn.iocoder.springboot.lab40.zpkindemo.consumerdemo.config;
2+
3+
import brave.CurrentSpanCustomizer;
4+
import brave.SpanCustomizer;
5+
import brave.Tracing;
6+
import brave.http.HttpTracing;
7+
import brave.servlet.TracingFilter;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import zipkin2.Span;
12+
import zipkin2.reporter.AsyncReporter;
13+
import zipkin2.reporter.Sender;
14+
import zipkin2.reporter.okhttp3.OkHttpSender;
15+
16+
import javax.servlet.Filter;
17+
18+
@Configuration
19+
public class ZipkinConfiguration {
20+
21+
// ==================== 通用配置 ====================
22+
23+
/**
24+
* Configuration for how to send spans to Zipkin
25+
*/
26+
@Bean
27+
public Sender sender() {
28+
return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans");
29+
}
30+
31+
/**
32+
* Configuration for how to buffer spans into messages for Zipkin
33+
*/
34+
@Bean
35+
public AsyncReporter<Span> spanReporter() {
36+
return AsyncReporter.create(sender());
37+
}
38+
39+
/**
40+
* Controls aspects of tracing such as the service name that shows up in the UI
41+
*/
42+
@Bean
43+
public Tracing tracing(@Value("${spring.application.name}") String serviceName) {
44+
return Tracing.newBuilder()
45+
.localServiceName(serviceName)
46+
// .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
47+
// .addScopeDecorator(MDCScopeDecorator.create()) // puts trace IDs into logs
48+
// .build()
49+
// )
50+
.spanReporter(spanReporter()).build();
51+
}
52+
53+
/**
54+
* Allows someone to add tags to a span if a trace is in progress
55+
*/
56+
@Bean
57+
public SpanCustomizer spanCustomizer(Tracing tracing) {
58+
return CurrentSpanCustomizer.create(tracing);
59+
}
60+
61+
// ==================== HTTP 相关 ====================
62+
63+
/**
64+
* Decides how to name and tag spans. By default they are named the same as the http method
65+
*/
66+
@Bean
67+
public HttpTracing httpTracing(Tracing tracing) {
68+
return HttpTracing.create(tracing);
69+
}
70+
71+
/**
72+
* Creates server spans for http requests
73+
*/
74+
@Bean
75+
public Filter tracingFilter(HttpTracing httpTracing) {
76+
return TracingFilter.create(httpTracing);
77+
}
78+
79+
// ==================== SpringMVC 相关 ====================
80+
// @see SpringMvcConfiguration 类上的,@Import(SpanCustomizingAsyncHandlerInterceptor.class)
81+
82+
83+
84+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
spring:
2+
application:
3+
name: user-service-consumer
4+
15
# dubbo 配置项,对应 DubboConfigurationProperties 配置类
26
dubbo:
37
# Dubbo 应用配置
48
application:
5-
name: user-service-consumer # 应用名
9+
name: ${spring.application.name} # 应用名
610
# Dubbo 注册中心配置
711
registry:
812
address: zookeeper://127.0.0.1:2181 # 注册中心地址。个鞥多注册中心,可见 http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html 文档。
13+
# Dubbo 服务提供者的配置,对应 ConsumerConfig 类
14+
consumer:
15+
filter: tracing

lab-40/lab-40-zipkin-dubbo/lab-40-zipkin-dubbo-provider/pom.xml

+30
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,36 @@
5050
<version>2.13.0</version>
5151
</dependency>
5252

53+
<!-- Brave 核心库 -->
54+
<!-- The below are needed to report traces to http://localhost:9411/api/v2/spans -->
55+
<dependency>
56+
<groupId>io.zipkin.brave</groupId>
57+
<artifactId>brave</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>io.zipkin.reporter2</groupId>
61+
<artifactId>zipkin-sender-okhttp3</artifactId>
62+
</dependency>
63+
64+
<!-- Brave 针对 Dubbo 的插件,实现链路追踪 -->
65+
<dependency>
66+
<groupId>io.zipkin.brave</groupId>
67+
<artifactId>brave-instrumentation-dubbo</artifactId>
68+
<version>5.10.1</version>
69+
</dependency>
5370
</dependencies>
5471

72+
<dependencyManagement>
73+
<!-- Brave Bom 文件 -->
74+
<dependencies>
75+
<dependency>
76+
<groupId>io.zipkin.brave</groupId>
77+
<artifactId>brave-bom</artifactId>
78+
<version>5.9.1</version>
79+
<type>pom</type>
80+
<scope>import</scope>
81+
</dependency>
82+
</dependencies>
83+
</dependencyManagement>
84+
5585
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cn.iocoder.springboot.lab40.zipkindemo.providerdemo.config;
2+
3+
import brave.CurrentSpanCustomizer;
4+
import brave.SpanCustomizer;
5+
import brave.Tracing;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import zipkin2.Span;
10+
import zipkin2.reporter.AsyncReporter;
11+
import zipkin2.reporter.Sender;
12+
import zipkin2.reporter.okhttp3.OkHttpSender;
13+
14+
@Configuration
15+
public class ZipkinConfiguration {
16+
17+
// ==================== 通用配置 ====================
18+
19+
/**
20+
* Configuration for how to send spans to Zipkin
21+
*/
22+
@Bean
23+
public Sender sender() {
24+
return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans");
25+
}
26+
27+
/**
28+
* Configuration for how to buffer spans into messages for Zipkin
29+
*/
30+
@Bean
31+
public AsyncReporter<Span> spanReporter() {
32+
return AsyncReporter.create(sender());
33+
}
34+
35+
/**
36+
* Controls aspects of tracing such as the service name that shows up in the UI
37+
*/
38+
@Bean
39+
public Tracing tracing(@Value("${spring.application.name}") String serviceName) {
40+
return Tracing.newBuilder()
41+
.localServiceName(serviceName)
42+
// .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
43+
// .addScopeDecorator(MDCScopeDecorator.create()) // puts trace IDs into logs
44+
// .build()
45+
// )
46+
.spanReporter(spanReporter()).build();
47+
}
48+
49+
/**
50+
* Allows someone to add tags to a span if a trace is in progress
51+
*/
52+
@Bean
53+
public SpanCustomizer spanCustomizer(Tracing tracing) {
54+
return CurrentSpanCustomizer.create(tracing);
55+
}
56+
57+
}

lab-40/lab-40-zipkin-dubbo/lab-40-zipkin-dubbo-provider/src/main/resources/application.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
spring:
2+
application:
3+
name: user-service-provider
4+
15
# dubbo 配置项,对应 DubboConfigurationProperties 配置类
26
dubbo:
37
# Dubbo 应用配置
48
application:
5-
name: user-service-provider # 应用名
9+
name: ${spring.application.name} # 应用名
610
# Dubbo 注册中心配
711
registry:
812
address: zookeeper://127.0.0.1:2181 # 注册中心地址。个鞥多注册中心,可见 http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html 文档。
@@ -13,3 +17,6 @@ dubbo:
1317
# 配置扫描 Dubbo 自定义的 @Service 注解,暴露成 Dubbo 服务提供者
1418
scan:
1519
base-packages: cn.iocoder.springboot.lab40.zipkindemo.providerdemo.service
20+
# Dubbo 服务提供者的配置,对应 ProviderConfig 类
21+
provider:
22+
filter: tracing

labx-13/labx-13-sc-sleuth-dubbo/labx-13-sc-sleuth-dubbo-consumer/src/main/resources/application.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dubbo:
1616
# Dubbo 服务注册中心配置,对应 RegistryConfig 类
1717
registry:
1818
address: spring-cloud://127.0.0.1:8848 # 指定 Dubbo 服务注册中心的地址
19-
# Dubbo 服务提供者的配置,对应 ProviderConfig
19+
# Dubbo 服务提供者的配置,对应 ConsumerConfig
2020
consumer:
2121
filter: tracing
2222
# Spring Cloud Alibaba Dubbo 专属配置项,对应 DubboCloudProperties 类

0 commit comments

Comments
 (0)