SpringBoot Zipkin Dubbo Rpc Http日志鏈路追蹤全流程
1.準備作業
1)安裝啟動Zipkin
2)安裝啟動Zookeeper
3)專案架構
本專案主要將 采用 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日志鏈路追蹤全流程
2.本地部署Zipkin服務器
下載jar包后,直接到jar包目錄,運行 java -jar java -jar zipkin-server-2.12.9-exec.jar
當你看到 Started ZipkinServer in 2.652 seconds (JVM running for 3.204) 表明運行成功
1)運行Jar

2)啟動Zipkin服務器
注意 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) 說明啟動成功

3)查看Zpikin界面
瀏覽器輸入 http://127.0.0.1:9411 你可以看到一下界面

3.本地部署Zookeeper服務器并啟動
測驗zk命令正常,zk埠為2181,注冊地址為 zookeeper://127.0.0.1:2181

4.配置專案A user
先說下專案結構 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 一個模塊
1)構建專案A user
在頁面 https://start.spring.io/ 中構建專案 user,直接新建一個Springboot專案
插件勾選了 web、Lombok

構建完成 ,解壓zip包,然后 用IDEA匯入專案
2)匯入user專案
打開IDEA 匯入專案,然后 查看目錄結構

3)配置user微服務 application.properties
配置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配置
4)引入user微服務 Log4j2組態檔
在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 配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Configuration后面的status,這個用于設定log4j2自身內部的資訊輸出,可以不設定,
當設定成trace時,可以看到log4j2內部各種詳細輸出
-->
<!--monitorInterval:Log4j能夠自動檢測修改配置 檔案和重新配置本身,設定間隔秒數-->
<configuration monitorInterval="5">
<!--日志級別以及優先級排序:
OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL
-->
<!--變數配置-->
<Properties>
<!--
格式化輸出:
%d表示日期,
%thread表示執行緒名,
%-5level:級別從左顯示5個字符寬度
%msg:日志訊息,%n是換行符
-->
<!--
%logger{36} 表示 Logger 名字最長36個字符
-->
<property name="LOG_PATTERN" value="[%d{yyy-MM-dd HH:mm:ss:SSS}] [%X{traceId},%X{spanId}] %highlight{%-5level}[%thread] %style{%logger{36}}{cyan} : %msg%n" />
<!-- 定義日志存盤的路徑,不要配置相對路徑 -->
<property name="PROJECT_NAME" value="user" />
<property name="FILE_PATH" value="E:\myworkspace\log\${PROJECT_NAME}" />
</Properties>
<appenders>
<console name="Console" target="SYSTEM_OUT">
<!--輸出日志的格式-->
<PatternLayout pattern="${LOG_PATTERN}" disableAnsi="false" noConsoleNoAnsi="false"/>
<!--控制臺只輸出level及其以上級別的資訊(onMatch),其他的直接拒絕(onMismatch)-->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</console>
<!--檔案會列印出所有資訊,這個log每次運行程式會自動清空,由append屬性決定,適合臨時測驗用-->
<File name="FileLog" fileName="${FILE_PATH}/test.log" append="false">
<PatternLayout pattern="${LOG_PATTERN}"/>
</File>
<!--
這個會列印出所有的info及以下級別的資訊,每次大小超過size,
則這size大小的日志會自動存入按年份-月份建立的檔案夾下面并進行壓縮,
作為存檔
-->
<RollingFile name="RollingFileInfo" fileName="${FILE_PATH}/info.log" filePattern="${FILE_PATH}/${PROJECT_NAME}-INFO-%d{yyyy-MM-dd}_%i.log.gz">
<!--控制臺只輸出level及以上級別的資訊(onMatch),其他的直接拒絕(onMismatch)-->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<!--interval屬性用來指定多久滾動一次,默認是1 hour-->
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="20MB"/>
</Policies>
<!-- DefaultRolloverStrategy屬性如不設定,則默認為最多同一檔案夾下7個檔案開始覆寫-->
<DefaultRolloverStrategy max="15"/>
</RollingFile>
<!-- 這個會列印出所有的warn及以下級別的資訊,每次大小超過size,則這size大小的日志會自動存入按年份-月份建立的檔案夾下面并進行壓縮,作為存檔-->
<RollingFile name="RollingFileWarn" fileName="${FILE_PATH}/warn.log" filePattern="${FILE_PATH}/${PROJECT_NAME}-WARN-%d{yyyy-MM-dd}_%i.log.gz">
<!--控制臺只輸出level及以上級別的資訊(onMatch),其他的直接拒絕(onMismatch)-->
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<!--interval屬性用來指定多久滾動一次,默認是1 hour-->
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="20MB"/>
</Policies>
<!-- DefaultRolloverStrategy屬性如不設定,則默認為最多同一檔案夾下7個檔案開始覆寫-->
<DefaultRolloverStrategy max="15"/>
</RollingFile>
<!-- 這個會列印出所有的error及以下級別的資訊,每次大小超過size,則這size大小的日志會自動存入按年份-月份建立的檔案夾下面并進行壓縮,作為存檔-->
<RollingFile name="RollingFileError" fileName="${FILE_PATH}/error.log" filePattern="${FILE_PATH}/${PROJECT_NAME}-ERROR-%d{yyyy-MM-dd}_%i.log.gz">
<!--控制臺只輸出level及以上級別的資訊(onMatch),其他的直接拒絕(onMismatch)-->
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<!--interval屬性用來指定多久滾動一次,默認是1 hour-->
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="20MB"/>
</Policies>
<!-- DefaultRolloverStrategy屬性如不設定,則默認為最多同一檔案夾下7個檔案開始覆寫-->
<DefaultRolloverStrategy max="15"/>
</RollingFile>
</appenders>
<!--Logger節點用來單獨指定日志的形式,比如要為指定包下的class指定不同的日志級別等,-->
<!--然后定義loggers,只有定義了logger并引入的appender,appender才會生效-->
<loggers>
<!--過濾掉spring和mybatis的一些無用的DEBUG資訊-->
<logger name="org.mybatis" level="info" additivity="false">
<AppenderRef ref="Console"/>
</logger>
<!--監控系統資訊-->
<!--若是additivity設為false,則 子Logger 只會在自己的appender里輸出,而不會在 父Logger 的appender里輸出,-->
<Logger name="org.springframework" level="info" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<root level="info">
<appender-ref ref="Console"/>
<appender-ref ref="FileLog"/>
<appender-ref ref="RollingFileInfo"/>
<appender-ref ref="RollingFileWarn"/>
<appender-ref ref="RollingFileError"/>
</root>
</loggers>
</configuration>
5)修改user微服務 Pom檔案
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如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jzj</groupId>
<artifactId>user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>user</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<dubbo.version>2.7.7</dubbo.version>
<curator.version>4.0.1</curator.version>
<brave-bom.version>5.13.3</brave-bom.version>
<zipkin-reporter-bom.version>2.16.3</zipkin-reporter-bom.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions><!-- 去掉springboot log默認配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入log4j2依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!--Log4j2 異步日志Jar-->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--添加Dubbo及Zookeeper、zkclient依賴 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-dependencies-zookeeper -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- zookeeper客戶端注冊中心依賴-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>
<!-- SpringBoot 2.7.x Dubbo 版本 配置Zipkin-->
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-dubbo</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-spring-beans</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-context-slf4j</artifactId>
</dependency>
<!-- tracing -->
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-sender-okhttp3</artifactId>
</dependency>
<!-- tracing & mvc-->
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-spring-webmvc</artifactId>
</dependency>
<!-- tracing & http-->
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-httpclient</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-bom</artifactId>
<version>${brave-bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-bom</artifactId>
<version>${zipkin-reporter-bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
6)引入專案測驗類Controller
在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;
}
}
6)引入專案配置類
在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<Span> 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);
}
}
7)啟動專案、測驗Http請求
啟動專案,訪問埠 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

8)查看Log日志及Zipkin界面
Console端Log日志

Zipkin界面

到此為止,我們配置好了A 用戶User 微服務,并且它的 Http 是可以追蹤的,要想實作Dubbo Rpc呼叫,我們還要配置好B order微服務
下一章我們開始 Dubbo RPC B 訂單微服務 client、web服務 的鏈路追蹤專案
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/258693.html
標籤:其他
下一篇:大資料中篇03_Flume
