隨着業務愈來愈複雜,系統也隨之進行各類拆分,特別是隨着微服務架構的興起,看似一個簡單的應用,後臺可能不少服務在支撐;一個請求可能須要多個服務的調用;當請求遲緩或不可用時,沒法得知是哪一個微服務引發的,這時就須要解決如何快速定位服務故障點,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
一,環境安裝服務器
二,建立微服務架構
<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>
@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 服務
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 服務器的地址
@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 調用的
<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>
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
java -jar zipkin.jar --RABBIT_URI=amqp://admin:12345@localhost:5672/sleuth --STORAGE_TYPE=elasticsearch --ES_HOSTS=http//:localhost:9200 --ES_HTTP_LOGGING=BASIC
--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 存儲的數據信息。以下圖:
以上一個完成的分佈式服務鏈路追蹤系統完成。