Spring Cloud 分佈式鏈路跟蹤 Sleuth + Zipkin + Elasticsearch

隨着業務愈來愈複雜,系統也隨之進行各類拆分,特別是隨着微服務架構的興起,看似一個簡單的應用,後臺可能不少服務在支撐;一個請求可能須要多個服務的調用;當請求遲緩或不可用時,沒法得知是哪一個微服務引發的,這時就須要解決如何快速定位服務故障點,Zipkin 分佈式跟蹤系統就能很好的解決這樣的問題。
若是想學習Java工程化、高性能及分佈式、深刻淺出。微服務、Spring,MyBatis,Netty源碼分析的朋友能夠加個人Java高級交流:854630135,裏面有阿里大牛直播講解技術,以及Java大型互聯網技術的視頻免費分享給你們。html

那麼到底怎麼使用呢?接下來完成一個具體的實例來體會一把微服務鏈路追蹤:java

本文使用的 Spring Cloud Finchley 版本,和其餘版本會有不一樣web

咱們使用user-service,order-service 做爲兩個微服務,zuul-gateway 做爲服務網關spring

zuul-gateway -> order-service -> user-service, 造成服務調用鏈路,完成一次請求。centos

注意:Zipkin 再也不推薦咱們來自定義 Server 端,在最新版本的 Spring Cloud 依賴管理裏已經找不到 Zipkin-server 了 ,根本就不須要本身新建一個 Zipkin-server 服務,網上的各類教程都數互相抄的,請無視api

一,環境安裝服務器

  1. 本人使用 centos 7 ,java-10
  2. 安裝 Zipkin:聚合各個業務系統之間的調用延遲數據
  3. 安裝 RabbitMQ:系統調用數據傳輸
  4. 安裝 Elasticsearch:系統調用數據持久化
  5. 安裝Elasticsearch-head:Elasticsearch 可視化

二,建立微服務架構

  1. user-service
  2. 如下是pom依賴文件
 
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--服務鏈路追蹤--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <!--數據傳輸--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency>
  1. 新建@RestController 接口 UserOrderController,代碼以下:
 
@RestController public class UserOrderController { @Autowired private UserOrderService orderService; @RequestMapping(value = "/getUserOrder", method = RequestMethod.GET) public String getUserOrder() { return orderService.getOrder(); } } 說明:在 user-service 使用 FeignClient 調用 order-service 的 getOrder 服務
  1. application.yml 配置文件以下:
 
spring:  application:  name: user-service  sleuth:  web:  client:  enabled: true  sampler:  probability: 1.0  zipkin:  base-url: http://192.168.10.100:9411/  enabled: true  sender:  type: RABBIT  rabbitmq:  addresses: 192.168.10.100  port: 15672  username: admin  password: 12345  virtual-host: sleuth  server:  port: 9100 zipkin 參數說明:  probability: 1.0 #將採樣比例設置爲 1.0,也就是所有都須要。默認是 0.1  base-url: http://192.168.10.100:9411/ #Zipkin 服務器的地址
  1. order-service
  2. pom依賴文件和user-service相同
  3. 新建@RestController 接口 OrderController,代碼以下:
 
@RestController public class OrderController { @Value("${server.port}") private String port;  @RequestMapping(value = "/getOrder", method = RequestMethod.GET) public String getOrder() { return "Success, Order-Service, Port :" + port;  } } 說明:getOrder接口就是給 user-service 調用的
  1. application.yml 配置文件和user-service相同
  2. zuul-gateway網關
  3. 如下是pom依賴文件
  4.  
 
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--服務鏈路追蹤--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <!--數據傳輸--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency>
  1. application.yml 配置文件以下:
  2.  
 
spring:  application:  name: zuul-gateway  sleuth:  web:  client:  enabled: true  sampler:  probability: 1.0  zipkin:  base-url: http://192.168.10.100:9411/  enabled: true  sender:  type: RABBIT  rabbitmq:  addresses: 192.168.10.100  port: 15672  username: admin  password: 12345  virtual-host: sleuth  server:  port: 9310  eureka:  client:  service-url:  defaultZone: http://localhost:8080/eureka/  zuul:  prefix: /v1  routes: # http://localhost:9310/v1/user/ # user Api  user-api:  path: /user/**  serviceId: user-service # order Api  order-api:  path: /order/**  serviceId: order-service
  1. zipkin配置和user-service相同
  2. zuul 路由配置本身找資料參考啊,這裏不作說明
  3. 以上咱們微服務所有完成,而後所有啓動
  4. 三,啓動各系統和組件
  5. 前面說不推薦用戶本身建立 Zipkin服務,那怎麼把數據傳輸到 Zipkin服務器呢?就是利用Zipkin的環境變量,經過環境變量讓 Zipkin 從 RabbitMQ 中讀取信息
  6. 1,啓動Zipkin服務,並指定 RabbitMQ作數據傳輸,Elasticsearch持久化數據,啓動命令以下:
 
java -jar zipkin.jar --RABBIT_URI=amqp://admin:12345@localhost:5672/sleuth --STORAGE_TYPE=elasticsearch --ES_HOSTS=http//:localhost:9200 --ES_HTTP_LOGGING=BASIC
  1. 說明:

--RABBIT_URI=amqp://admin:12345@localhost:5672/sleuth 指定用 RabbitMQ 作數據傳輸app

--STORAGE_TYPE=elasticsearch --ES_HOSTS=http//:localhost:9200 --ES_HTTP_LOGGING=BASIC 指定用 Eelasticsearch 作數據傳輸elasticsearch

可配置的環境變量,請參考:https://www.rabbitmq.com/uri-spec.html

固然你以爲 搭建Elasticsearch太麻煩了,也能夠用MYSQL 生成環境推薦使用 Elasticsearch,或者你只想本身試一下,那你能夠不用存儲,數據就在內存中。

2,啓動RabbitMQ服務 http://192.168.10.100:15672/ 查看啓動生個,推薦本身新建個用戶,而後登陸 查看。

3,啓動Elasticsearch服務,http://192.168.10.100:9200/ 查看ES啓動,注意Elasticsearch 不能用root用戶啓動,具體怎麼操做請百度教程。

4,啓動Elasticsearch-head,http://192.168.10.100:9100/ 能夠看到界面,注意 集羣健康值,要是未鏈接就是有問題,本身解決。

5,啓動user-service,order-service,zuul-gateway 網關,請求你本身定義的接口,這個有錯本身解決

查看RabbitMQ可視化界面,就能看到 數據傳輸信息。以下圖:

 

查看Zipkin可視化界面,就能看到服務調用鏈路信息。以下圖:

 

查看Elasticsearch-head可視化界面,就能看到 Elasticsearch 存儲的數據信息。以下圖:

 

以上一個完成的分佈式服務鏈路追蹤系統完成。

相關文章
相關標籤/搜索