Spring Cloud微服務之 sleuth+zipkin日誌聚合
1.簡介
(1)什麼是服務追蹤 Sleuth
在微服務架構中,要完成一個功能,通過Rest請求服務API呼叫服務來完成,整個呼叫過程可能會聚合多個後臺伺服器協同完成。在整個鏈路上,任何一處呼叫超時
或出錯都有可能造成前端請求失敗。這時跟蹤記錄這些請求的呼叫的情況就要複雜的多,這就需要一個專門的工具來處理,spring cloud sleuth元件就是用於跟蹤記錄的工具
Sleuth就相當於為微服務架構引入了一套記錄體系,包含兩部分,一個是 trace ID;另一個是 span ID,隨時記錄一個請求的每一步操作。
(2)什麼是日誌聚合 Zipkin
如果想學習Java工程化、高效能及分散式、深入淺出。微服務、Spring,MyBatis,Netty原始碼分析的朋友可以加我的Java高階交流:787707172,群裡有阿里大牛直播講解技術,以及Java大型網際網路技術的視訊免費分享給大家。
zipkin 是 Dpper的開源實現,支援多種語言。Sleuth已經將每個請求從開始呼叫到完成的每一步都進行了記錄,但是這些log資訊會很分散,使用起來不太方便,就
需要有一個工具可以將這些資訊進行收集和彙總,並且顯示視覺化的結果,便於分析和定位。這就需要建立一個 Zipkin Server用於收集和展示這些呼叫鏈路的資訊,他
的使用也很方便。
2.如何使用
本例需要建立三個工程:
product-sevice 普通客戶端工程,提供一個rest介面(專案建立參考第三節)
order-service 普通客戶端工程,用於呼叫product-service服務(專案建立參考第三節)
zipkin-service 日誌服務工程,用於追蹤記錄請求鏈路。
第一步:建立 zipkin-service,作為Zipkin Server。
工程目錄如圖:
(1)首先在 pom.xml中增加依賴
<dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency>
(2)在啟動類增加@EnableZipkinServer註解,用來開啟Zipkin Server的功能。
package com.hole; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import zipkin.server.EnableZipkinServer; @EnableDiscoveryClient @EnableZipkinServer @SpringBootApplication public class ZipkinServiceApplication { private static Logger logger = LoggerFactory.getLogger(ZipkinServiceApplication.class); public static void main(String[] args) { SpringApplication.run(ZipkinServiceApplication.class, args); } }
(3)application.properties配置如下:
spring.application.name=zipkin-service server.port=9411 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
(4)啟動 Zipkin Server,訪問 http://localhost:9411/,此時因為還沒有服務請求關聯的zipkin server,所以服務名裡列表裡是空的,如圖:
如果想學習Java工程化、高效能及分散式、深入淺出。微服務、Spring,MyBatis,Netty原始碼分析的朋友可以加我的Java高階交流:787707172,群裡有阿里大牛直播講解技術,以及Java大型網際網路技術的視訊免費分享給大家。
第二步:建立日誌客戶端工程
想要在介面上能看到zipkin server蒐集的日誌資訊及依賴關係,需要在每個工程中增加sleuth與zipkin的依賴,然後增加註冊地址,指向到 zipkin server上就可以了。
1.(1)建立product-service工程,並增加依賴。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin</artifactId> </dependency>
(2)application.properties配置增加配置,指向 zipkin server服務地址。
spring.zipkin.base-url=http://localhost:9411/
(3)啟動類程式碼增加 /hello 介面,並在介面入口處做一個日誌列印,程式碼如下
package com.hole; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @EnableDiscoveryClient @SpringBootApplication @RestController public class ProductServiceApplication { private static Logger logger = LoggerFactory.getLogger(ProductServiceApplication.class); public static void main(String[] args) { SpringApplication.run(ProductServiceApplication.class, args); } @RequestMapping(value = "/hello",method = RequestMethod.GET) public ResponseEntity<String> hello(){ logger.info("called by product-service"); return new ResponseEntity<String>("hello product service!", HttpStatus.OK); } }
2.(1)建立order-service工程。
依賴和配置同 product-sevice。
(2)啟動類增加 /product/hello介面,目的是調動 product-service介面。在介面入口位置做一個日誌列印。
package com.hole; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient @SpringBootApplication @RestController public class OrderServiceApplication { private static Logger logger = LoggerFactory.getLogger(OrderServiceApplication.class); @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } @Autowired RestTemplate restTemplate; public static void main(String[] args) { SpringApplication.run(OrderServiceApplication.class, args); } @RequestMapping(value = "/hello",method = RequestMethod.GET) public ResponseEntity<String> hello(){ return new ResponseEntity<String>("hello order service!", HttpStatus.OK); } @RequestMapping(value = "/product/hello",method = RequestMethod.GET) public String productHello(){ logger.info("order-service calling product-service!"); return restTemplate.getForEntity("http://PRODUCT-SERVICE/hello",String.class).getBody(); } }
第三步:啟動專案並驗證。
(1)啟動全部專案,啟動成功後在監控頁面檢視結果。
(2)訪問 order-service 服務的 /product/hello介面,結果訪問成功。可以多重新整理幾次
(3)訪問 zipkin server(9411埠),檢視日誌服務列表,發現service name下拉框已有下拉選項,驗證成功。
點選 Find Traces 檢視結果可以看到它的完整鏈路條,點選鏈條可以看到呼叫鏈條,點選 Dependencies可以檢視依賴關係,等等,自行發覺吧,就不上圖了。
歡迎工作一到八年的Java工程師朋友們加入Java高階交流:787707172
本群提供免費的學習指導 架構資料 以及免費的解答
不懂得問題都可以在本群提出來 之後還會有直播平臺和