本项目主要将 采用 SpringBoot 2.4.2 + Zipkin + Dubbo 2.7.x 实现
在这里说一下坑,我为什么不直接用sleuth 来配置,想看 sleuth实现Http调用的链路追踪 可以看下我的文章 SpringBoot SpringCloud Sleuth Zipkin实现 链路追踪(一)
不能实现Dubbo的Rpc调用链追踪,因为sleuth要引入新的包,比较坑的是 一个包 brave-instrumentation-dubbo、brave-instrumentation-dubbo-rpc 这个包很坑,大家都知道 dubbo在 2.6.x之前 是由 com.alibaba 来维护的,groupId都是 com.alibaba的
从dubbo 2.7.x 版本后,由apache来维护了,所以 groupId 变成了 org.apache.dubbo, 2.6.x和 2.7.x 区别还是蛮大的
官方给的issud是:
brave-instrumentation-dubbo-rpc only support for 2.6.x.
The newest version of Dubbo is 2.7.x.The package has changed.The new package is start with org.apache.dubbo.
所以说 你用的Dubbo 如果是 2.6.x的, 用 sleuth没问题,引入 brave-instrumentation-dubbo-rpc
如果你是用Dubbo 2.7.x 要引入 brave-instrumentation-dubbo 版本来实现
实际上我引入了sleuth和brave-instrumentation-dubbo 依旧不会简单的实现打印TraceId,rpc provider和consumer串联(也有可能是哪里搞错了,后期跟踪源码定位下问题)
所以 今天我写了这个文章,来实现SpringBoot Zipkin Dubbo Rpc Http日志链路追踪全流程
下载jar包后,直接到jar包目录,运行 java -jar java -jar zipkin-server-2.12.9-exec.jar
当你看到 Started ZipkinServer in 2.652 seconds (JVM running for 3.204) 表明运行成功
注意 http://127.0.0.1:9411 是Zipkin服务器界面的入口
看到 Serving HTTP at /0:0:0:0:0:0:0:0:9411 - http://127.0.0.1:9411/
Started ZipkinServer in 2.602 seconds (JVM running for 3.149) 说明启动成功
浏览器输入 http://127.0.0.1:9411 你可以看到一下界面
测试zk命令正常,zk端口为2181,注册地址为 zookeeper://127.0.0.1:2181
先说下项目结构 Dubbo通信同过接口访问 比如A->B A调用B的dubbo接口。
一般来说 都是B提供一个 client 的jar包, A项目引用 这个 jar包, 然后 通过调用Jar包中暴露的接口,此接口的实现一般在B服务中,这样才能实现 A项目通过Rpc 调用B项目的接口,等于A把B服务中接口的方法,当作本地方法来使用
所以项目结构 B项目有一个 client Jar,和 一个web应用 , 例如 订单服务B, order 和order-client 两个模块
A项目是一个web 应用,例如用户服务A,user 一个模块
在页面 https://start.spring.io/ 中构建项目 user,直接新建一个Springboot项目
插件勾选了 web、Lombok
构建完成 ,解压zip包,然后 用IDEA导入项目
配置application.properties ,log4j2需要指明日志配置的xml文件 如 resources下log4j下log4j2-dev.xml文件
配置user 微服务启动端口 9528
配置dubbo协议相关
配置zipkin配置相关
#项目配置
server.port=9528
spring.application.name=user
#项目配置
#Dubbo 配置
dubbo.protocol.name=dubbo
dubbo.application.name=userApplication
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.consumer.check=false
dubbo.provider.filter=tracing
dubbo.consumer.filter=tracing
#log日志配置
logging.config=classpath:log4j/log4j2-dev.xml
#log日志配置end
#zipkin配置
zipkin.base.url=http://127.0.0.1:9411/api/v2/spans
zipkin.enable=true
#zipkin配置
在resources下新建log4j文件夹,然后新建 log4j2-dev.xml文件 修改自己的项目名 $PROJECT_NAME 及 项目日志存储路径 $FILE_PATH
要想log4j2打印TraceId、SpanId,必要听别人说要配置 [%X{X-B3-TraceId},%X{X-B3-SpanId}] 没卵用,重要事情说三遍
这个版本的只有按照这个配置才是可以生效的[%X{traceId},%X{spanId}]
这个版本的只有按照这个配置才是可以生效的[%X{traceId},%X{spanId}]
这个版本的只有按照这个配置才是可以生效的[%X{traceId},%X{spanId}]
Log4j2-dev.xml 配置如下:
Pom文件需要修改成引用Log4j2的日志jar包,springboot默认依赖logback,现在要修改pom文件,去掉logback包,添加log4j2 jar包作为日志输出依赖
主要操作为 :
1.去除web包下的logging依赖
2.添加log4j2jar包依赖
3.添加dubbo及zookeeper依赖
4.添加 brave 相关客户端依赖, brave的依赖版本靠 dependencyManagement 中的brave-bom 5.13.3、zipkin-reporter-bom 2.16.3版本来控制
Pom如下
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.2
com.jzj
user
0.0.1-SNAPSHOT
user
Demo project for Spring Boot
1.8
2.7.7
4.0.1
5.13.3
2.16.3
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
com.lmax
disruptor
3.3.6
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.apache.dubbo
dubbo-spring-boot-starter
${dubbo.version}
org.apache.dubbo
dubbo-dependencies-zookeeper
${dubbo.version}
log4j
log4j
slf4j-log4j12
org.slf4j
org.apache.curator
curator-recipes
${curator.version}
org.apache.curator
curator-framework
${curator.version}
io.zipkin.brave
brave-instrumentation-dubbo
io.zipkin.brave
brave-spring-beans
io.zipkin.brave
brave-context-slf4j
io.zipkin.reporter2
zipkin-sender-okhttp3
io.zipkin.brave
brave-instrumentation-spring-webmvc
io.zipkin.brave
brave-instrumentation-httpclient
io.zipkin.brave
brave-bom
${brave-bom.version}
pom
import
io.zipkin.reporter2
zipkin-reporter-bom
${zipkin-reporter-bom.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
在com.jzj.user目录下新建 controller包,包中新建测试类SiteController, 保证测试http端口
http://localhost:9528/temp/ping
http://localhost:9528/temp/log
http://localhost:9528/temp/http
package com.jzj.user.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
@ResponseBody
@RestController
@RequestMapping("temp")
public class SiteController {
private static Logger log = LoggerFactory.getLogger(SiteController.class);
@Autowired
private RestTemplate restTemplate;
@RequestMapping("ping")
public Object ping() {
log.info("进入ping");
return "pong user";
}
@RequestMapping("log")
public Object log() {
log.info("this is info log");
log.error("this is error log");
log.debug("this is debug log");
log.warn("this is warn log");
log.trace("this is trace log");
return "123";
}
@RequestMapping("http")
public Object httpQuery() {
String roomUrl = "http://localhost:9528/temp/ping";
URI ping = URI.create(roomUrl);
String pingResult = restTemplate.getForObject(ping, String.class);
return pingResult;
}
}
在com.jzj.user包下新建autoconfig包
在autoconfig包下新建rest包,新建类RestTemplateConfig 用于使用RestTemplate封装http请求
package com.jzj.user.autoconfig.rest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* RestTemplate配置类
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//单位为ms
factory.setConnectTimeout(5000);//单位为ms
return factory;
}
}
在autoconfig包下新建trace包,新建类TracingConfig用于使用链路追踪配置
package com.jzj.user.autoconfig.trace;
import brave.CurrentSpanCustomizer;
import brave.SpanCustomizer;
import brave.Tracing;
import brave.context.slf4j.MDCScopeDecorator;
import brave.http.HttpTracing;
import brave.propagation.B3Propagation;
import brave.propagation.ExtraFieldPropagation;
import brave.propagation.ThreadLocalCurrentTraceContext;
import brave.rpc.RpcTracing;
import brave.sampler.Sampler;
import brave.servlet.TracingFilter;
import brave.spring.webmvc.SpanCustomizingAsyncHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import zipkin2.Span;
import zipkin2.codec.Encoding;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.Sender;
import zipkin2.reporter.okhttp3.OkHttpSender;
import javax.servlet.Filter;
/**
* @author jzjie
*/
@Configuration
@Import(SpanCustomizingAsyncHandlerInterceptor.class)
public class TracingConfig extends WebMvcConfigurerAdapter {
@Bean
@ConditionalOnProperty(
value = {"zipkin.enable"},
matchIfMissing = false)
Sender sender(@Value("${zipkin.base.url}") String url) {
return OkHttpSender.newBuilder()
.encoding(Encoding.PROTO3)
.endpoint(url)
.build();
}
/**
* Configuration for how to buffer spans into messages for Zipkin
*/
@Bean
@ConditionalOnBean(Sender.class)
AsyncReporter spanReporter(Sender sender) {
AsyncReporter.Builder builder = AsyncReporter.builder(sender);
builder.queuedMaxSpans(50000);
builder.queuedMaxBytes(104857600);
return builder.build();
}
/**
* Controls aspects of tracing such as the service name that shows up in the UI
*/
@Bean
Tracing tracing(@Value("${dubbo.application.name}") String applicationName, @Value("${zipkin.enable:false}") Boolean enable, @Autowired(required = false) AsyncReporter spanReporter) {
Tracing.Builder builder = Tracing.newBuilder()
.localServiceName(applicationName)
.propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name"))
.currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
// puts trace IDs into logs
.addScopeDecorator(MDCScopeDecorator.create())
.build()
);
if (enable) {
builder.spanReporter(spanReporter);
builder.sampler(Sampler.ALWAYS_SAMPLE);
} else {
builder.sampler(Sampler.NEVER_SAMPLE);
}
return builder.build();
}
@Bean
SpanCustomizer spanCustomizer(Tracing tracing) {
return CurrentSpanCustomizer.create(tracing);
}
/**
* Decides how to name and tag spans. By default they are named the same as the http method
*/
@Bean
HttpTracing httpTracing(Tracing tracing) {
return HttpTracing.create(tracing);
}
@Bean
RpcTracing rpcTracing(Tracing tracing) {
return RpcTracing.create(tracing);
}
/**
* Creates server spans for http requests
*/
@Bean
Filter tracingFilter(HttpTracing httpTracing) {
return TracingFilter.create(httpTracing);
}
@Autowired
SpanCustomizingAsyncHandlerInterceptor webMvcTracingCustomizer;
/**
* Decorates server spans with application-defined web tags
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(webMvcTracingCustomizer);
}
}
启动项目,访问端口 9528 controller端口
http://127.0.0.1:9528/temp/ping
http://127.0.0.1:9528/temp/log
http://127.0.0.1:9528/temp/http
Zipkin界面
到此为止,我们配置好了A 用户User 微服务,并且它的 Http 是可以追踪的,要想实现Dubbo Rpc调用,我们还要配置好B order微服务
下一章我们开始 Dubbo RPC B 订单微服务 client、web服务 的链路追踪项目