1. 程式人生 > >zipkin搭建springcloud鏈路追蹤服務注意事項

zipkin搭建springcloud鏈路追蹤服務注意事項

1、不需要在方法中列印日誌,會自動構建起跟蹤機制;

2、spring.application.name 不能包含下劃線,因為在eureka中java.net.URI不能區分下劃線,否則在進行服務呼叫的時候報錯(host name may not be null);

3、日誌中[demo-trace1,63c082f6715c8979,63c082f6715c8979,true],true代表上傳成功;

4、呼叫方法不能以/trace命名(http://localhost:9101/trace),否則日誌不能上傳到日誌伺服器上,應該是與系統自帶的方法有衝突(可以用http://localhost:9101/trace0,http://localhost:9101/a/trace等方式);

5、在測試時,請將spring.sleuth.sampler.probability設定為1(預設為0.1),否則要呼叫10次才會上傳成功1次;

6、只需要引入spring-cloud-starter-zipkin包;

7、關於 Zipkin 的服務端,在使用 Spring Boot 2.x 版本後,官方就不推薦自行定製編譯了,反而是直接提供了編譯好的 jar 包來給我們使用;

注意:在學習搭建鏈路追蹤服務時,本人採用的是通過建立zipkin工程,自行定製的方式進行,需要注意的是在這種情況下,案例中所有案例工程(包括閘道器,service端)的搭建都需要在低版本中進行如:

springboot 1.5.9、

spring-cloud.version Dalston.SR1、

spring-cloud-starter-zipkin 1.2.1.RELEASE、

zipkin-server 1.27.0、

zipkin-autoconfigure-ui 1.27.0 等,否則即使能正常開啟工程,鏈路記錄也無法上傳zipkinserver。因為關於 Zipkin 的服務端,在Spring Boot 2.x 版本後,官方就不推薦自行定製編譯了,反而是直接提供了編譯好的 jar 包來給我們使用;

所以官方提供了一鍵指令碼:
curl -sSL https://zipkin.io/quickstart.sh | bash -s
java -jar zipkin.jar
如果用 Docker 的話,直接 docker run -d -p 9411:9411 openzipkin/zipkin
如果非要自己建服務端,只需要在pom中引入以下包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>2.8.3</version>
</dependency>

然後在啟動類上加上@zipkin.server.internal.EnableZipkinServer

或者 @zipkin.server.EnableZipkinServer(後一個已被棄用,暫時不清楚兩個的區別);

 

8、自建zipkin服務啟動後,訪問localhost:9411,後臺可能會報錯:
java.lang.IllegalArgumentException: Prometheus requires that all meters with the same name have the same set of tag keys. There is already an existing meter containing tag keys [method, status, uri]. The meter you are attempting to register has keys [exception, method, status, uri].
可以不用關注這個問題,伺服器一樣能收集到日誌,也可以有兩種處理方式:
a、將management.metrics.web.server.auto-time-requests=false設定為false,預設為true;
b、重寫DefaultWebMvcTagsProvider或者實現介面WebMvcTagsProvider,參照DefaultWebMvcTagsProvider的寫法,只需要把DefaultWebMvcTagsProvider下getTages()方法的WebMvcTags.exception(exception)去除掉。
public class DemoTagsProvider implements WebMvcTagsProvider{
static Logger logger = LoggerFactory.getLogger(DemoTagsProvider.class);
@Override
public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler,
Throwable exception) {
return Tags.of(WebMvcTags.method(request), WebMvcTags.uri(request, response),
WebMvcTags.status(response));
}
@Override
public Iterable<Tag> getLongRequestTags(HttpServletRequest request, Object handler) {
return Tags.of(WebMvcTags.method(request), WebMvcTags.uri(request, null));
}
}
然後再啟動類中注入bean :
@Bean
public DemoTagsProvider demoTagsProvider(){
return new DemoTagsProvider();

 

}

文中部分內容借鑑於:https://blog.csdn.net/duanqing_song/article/details/80422301