天天看點

Spring Cloud(Finchley.RELEASE版本)微服務學習實踐:6.1全鍊路追蹤-Sleuth

環境:

jdk1.8;spring boot2.0.3;spring cloud(Finchley.RELEASE版本);Maven3.3

摘要說明:

全鍊路追蹤:一個微服務系統,服務之間的調用往往是很複雜的,一個消費者往往需要調用多個服務提供者才能得到結果,即幾乎每一個前端請求都會形成一條複雜的分布式服務調用鍊路,在每條鍊路中任何一個依賴服務出現延遲過高或錯誤的時候都有可能引起請求最後的失敗。這時候對于每個請求全鍊路調用的跟蹤就變得越來越重要,通過實作對請求調用的跟蹤可以幫助我們快速的發現錯誤根源以及監控分析每條請求鍊路上的性能瓶頸等好處。

Spring Cloud Sleuth:Spring Cloud Sleuth為Spring Cloud實施分布式跟蹤解決方案,大量借用Dapper,Zipkin和HTrace。對于大多數使用者來說,Sleuth應該是不可見的,并且所有與外部系統的互動都應該自動進行檢測。您可以簡單地在日志中捕獲資料,或者将資料發送到遠端收集器服務。更多參考:官網

步驟:

1.建立sleuthRibbon(sleuth服務消費者)和sleuthEurekaDiscovery(服務提供至)子項目

首先模仿ribbon和eurekaDiscovery複制sleuthRibbon(sleuth服務消費者)和sleuthEurekaDiscovery(服務提供至)子項目;其中application.properties分别為:

sleuthEurekaDiscovery:

#配置服務名稱及端口
spring.application.name=sleuth-eureka-client
server.port=2101

#将服務注冊到注冊中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

#——————————————添加健康檢查配置——————————————
# 該配置訓示eureka用戶端需要向eureka伺服器發送心跳的頻率  (Spring Cloud預設該配置是 30s)
eureka.instance.lease-renewal-interval-in-seconds=1

# 該配置訓示eureka伺服器在接收到最後一個心跳之後等待的時間,然後才能從清單中删除此執行個體 (Spring Cloud預設該配置是 90s)
eureka.instance.lease-expiration-duration-in-seconds=10
           

sleuthRibbon:

#配置服務及端口
spring.application.name=sleuth-ribbon-consumer
server.port=3101
#注冊到注冊中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

#從eureka伺服器系統資料庫中擷取注冊資訊的時間間隔(s),預設為30秒
eureka.client.registry-fetch-interval-seconds=5
           

2.開發一個服務調用及日志列印

我們在上述的兩個項目中開發下述兩個接口:

sleuthEurekaDiscovery(TestLogController):

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestLogController {
	protected final static Logger logger = LoggerFactory.getLogger(TestLogController.class);

	@GetMapping("/infoLog-1")
	public String infoLog() {
		logger.info("------infoLog-1------");
		return "infoLog";
	}
}
           

sleuthRibbon(TestLogController):

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestLogController {
	protected final static Logger logger = LoggerFactory.getLogger(TestLogController.class);
	@Autowired
	RestTemplate restTemplate;

	@GetMapping("/infoLog-2")
	public String infoLog() {
		logger.info("------infoLog-2------");
		return restTemplate.getForEntity("http://SLEUTH-EUREKA-CLIENT/infoLog-1", String.class).getBody();
	}
}
           

先後啟動

服務注冊中心(eurekaServer):eureka-server(1001)

服務提供者(sleuthEurekaDiscovery):sleuth-eureka-client(2101)

服務消費者(sleuthRibbon):sleuth-ribbon-consumer(3101)

通路http://localhost:3101/infoLog-2;先後分别看到日志

2018-07-02 11:25:31.593  INFO 11376 --- [nio-3101-exec-5] c.t.s.s.controller.TestLogController     : ------infoLog-2------
           
2018-07-02 11:25:31.596  INFO 13444 --- [nio-2101-exec-6] c.t.s.s.controller.TestLogController     : ------infoLog-1------
           

3.添加Spring Cloud Sleuth

我們分别在上述兩個子項目的pom.xml中添加Spring Cloud Sleuth的依賴:

再重複上面的操作檢視日志,會出現[sleuth-ribbon-consumer,1681af8931e6f1b7,1681af8931e6f1b7,false]分别表示:應用名稱,traceId,spanId,表示是否要将該資訊輸出到Zipkin等服務中來收集和展示;

其中spanId表示一個工作單元,如一個HTTP請求

下面兩個鍊路的日志前一個的spanId和下一個的traceId一緻達到一個全鍊路;

2018-07-02 11:35:45.773  INFO [sleuth-ribbon-consumer,1681af8931e6f1b7,1681af8931e6f1b7,false] 8168 --- [nio-3101-exec-1] c.t.s.s.controller.TestLogController     : ------infoLog-2------
           
2018-07-02 11:35:46.057  INFO [sleuth-eureka-client,1681af8931e6f1b7,df4315b11efce95d,false] 9080 --- [io-2101-exec-10] c.t.s.s.controller.TestLogController     : ------infoLog-1------
           

将日志級别設定為DEBUG(logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG)看到的日志更多;

2018-07-02 13:59:22.063 DEBUG [sleuth-ribbon-consumer,a62adda9e1031c9f,a62adda9e1031c9f,false] 8508 --- [nio-3101-exec-5] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-07-02 13:59:22.065 DEBUG [sleuth-ribbon-consumer,a62adda9e1031c9f,a62adda9e1031c9f,false] 8508 --- [nio-3101-exec-5] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2018-07-02 13:59:22.175 DEBUG [sleuth-ribbon-consumer,61006e3b87325039,61006e3b87325039,false] 8508 --- [nio-3101-exec-5] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/favicon.ico]
2018-07-02 13:59:22.184 DEBUG [sleuth-ribbon-consumer,61006e3b87325039,61006e3b87325039,false] 8508 --- [nio-3101-exec-5] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/favicon.ico] is: -1
2018-07-02 13:59:22.427 DEBUG [sleuth-ribbon-consumer,61006e3b87325039,61006e3b87325039,false] 8508 --- [nio-3101-exec-5] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-07-02 13:59:22.428 DEBUG [sleuth-ribbon-consumer,61006e3b87325039,61006e3b87325039,false] 8508 --- [nio-3101-exec-5] o.s.web.servlet.DispatcherServlet        : Successfully completed request
           
2018-07-02 13:59:21.863 DEBUG [sleuth-eureka-client,a62adda9e1031c9f,dee896f4cdf6b927,false] 10544 --- [nio-2101-exec-1] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/infoLog-1]
2018-07-02 13:59:21.877 DEBUG [sleuth-eureka-client,a62adda9e1031c9f,dee896f4cdf6b927,false] 10544 --- [nio-2101-exec-1] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/infoLog-1] is: -1
2018-07-02 13:59:21.912  INFO [sleuth-eureka-client,a62adda9e1031c9f,dee896f4cdf6b927,false] 10544 --- [nio-2101-exec-1] c.t.s.s.controller.TestLogController     : ------infoLog-1------
2018-07-02 13:59:21.995 DEBUG [sleuth-eureka-client,a62adda9e1031c9f,dee896f4cdf6b927,false] 10544 --- [nio-2101-exec-1] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-07-02 13:59:21.999 DEBUG [sleuth-eureka-client,a62adda9e1031c9f,dee896f4cdf6b927,false] 10544 --- [nio-2101-exec-1] o.s.web.servlet.DispatcherServlet        : Successfully completed request
           

4.源碼位址

github位址:https://github.com/cc6688211/springCloud.git

繼續閱讀