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。

工程目錄如圖:

Spring Cloud微服務之 sleuth+zipkin日誌聚合

(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,所以服務名裡列表裡是空的,如圖:

Spring Cloud微服務之 sleuth+zipkin日誌聚合

如果想學習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);
}
}
/<string>/<string>

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();
}
}
/<string>/<string>

第三步:啟動項目並驗證。

(1)啟動全部項目,啟動成功後在監控頁面查看結果。

(2)訪問 order-service 服務的 /product/hello接口,結果訪問成功。可以多刷新幾次

Spring Cloud微服務之 sleuth+zipkin日誌聚合

(3)訪問 zipkin server(9411端口),查看日誌服務列表,發現service name下拉框已有下拉選項,驗證成功。

Spring Cloud微服務之 sleuth+zipkin日誌聚合

點擊 Find Traces 查看結果可以看到它的完整鏈路條,點擊鏈條可以看到調用鏈條,點擊 Dependencies可以查看依賴關係,等等,自行發覺吧,就不上圖了。

歡迎工作一到八年的Java工程師朋友們加入Java高級交流:787707172

本群提供免費的學習指導 架構資料 以及免費的解答

不懂得問題都可以在本群提出來 之後還會有直播平臺和講師直接交流噢


分享到:


相關文章: