H__D  

为什么使用RabbitMQ

  zipkin的原理是服务之间的调用关系会通过HTTP方式上报到zipkin-server端,然后再通过zipkin-ui去调用查看追踪服务之间的调用链路。但是这种方式存在一个隐患,如果微服务之间与zipkin服务端网络不通,或调用链路上的网络闪断,http通信收集方式就无法工作。而且zipkin默认是将数据存储在内存中的,如果服务端重启或宕机,就会导致数据丢失。

  不仅将数据存储在zipkin-serve中,同时还能通过某个消息存储的容器将本次调用其他服务的消息数据进行持久化存储,这样不就可以解决问题吗?结合RabbitMQ证好解决了这一问题,再发一份到消息队列中去。

  项目架构图如下:

  

Sleuth + Zipkin + RabbitMQ 集成项目示例

  本例使用项目参考:【SpringCloud】Spring Cloud Sleuth + Zipkin 服务调用链路追踪(二十五)项目

  1、安装RabbitMQ并启动,参考【RabbitMQ】 RabbitMQ安装

  2、启动Zipkin服务端,并连接到RabbitMQ

    命令:java -jar zipkin-server-2.21.1-exec.jar --zipkin.collector.rabbitmq.addresses=localhost:5672 --zipkin.collector.rabbitmq.username=guest --zipkin.collector.rabbitmq.password=guest

  3、查看RabbitMQ的Web界面,Queue选项中内容如下,增加了一个zipkin的Queue

    

  4、Slehtu服务提供者项目(springcloud-provider-sleuth-payment8010),增加RabbitMQ依赖

 1 <!-- zipkin + sleuth -->
 2 <dependency>
 3     <groupId>org.springframework.cloud</groupId>
 4     <artifactId>spring-cloud-starter-zipkin</artifactId>
 5 </dependency>
 6 
 7 <dependency>
 8     <groupId>org.springframework.boot</groupId>
 9     <artifactId>spring-boot-starter-amqp</artifactId>
10 </dependency>
11 
12 <!-- eureka client -->
13 <dependency>
14     <groupId>org.springframework.cloud</groupId>
15     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
16 </dependency>

    完整依赖如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>test-springcloud</artifactId>
 7         <groupId>com.test</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>springcloud-provider-sleuth-payment8010</artifactId>
13 
14     <dependencies>
15 
16         <!-- zipkin + sleuth -->
17         <dependency>
18             <groupId>org.springframework.cloud</groupId>
19             <artifactId>spring-cloud-starter-zipkin</artifactId>
20         </dependency>
21 
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-amqp</artifactId>
25         </dependency>
26 
27         <!-- eureka client -->
28         <dependency>
29             <groupId>org.springframework.cloud</groupId>
30             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
31         </dependency>
32 
33         <!-- spring boot -->
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-web</artifactId>
37         </dependency>
38         <dependency>
39             <groupId>org.springframework.boot</groupId>
40             <artifactId>spring-boot-starter-actuator</artifactId>
41         </dependency>
42 
43         <dependency>
44             <groupId>org.springframework.boot</groupId>
45             <artifactId>spring-boot-devtools</artifactId>
46             <scope>runtime</scope>
47             <optional>true</optional>
48         </dependency>
49 
50         <dependency>
51             <groupId>org.projectlombok</groupId>
52             <artifactId>lombok</artifactId>
53             <optional>true</optional>
54         </dependency>
55         <dependency>
56             <groupId>org.springframework.boot</groupId>
57             <artifactId>spring-boot-starter-test</artifactId>
58             <scope>test</scope>
59         </dependency>
60     </dependencies>
61 
62 </project>
pom.xml

  5、修改项目(springcloud-provider-sleuth-payment8010)作为Slehtu服务提供者的配置文件

    1)更该zipkin的发送类型

    2)增加spring rabbitmq的配置

 1 spring:
 2   application:
 3     name: cloud-sleuth-provider
 4   zipkin:
 5     # zipkin url地址
 6 #    base-url: http://localhost:9411
 7     sender:
 8       type: rabbit
 9   sleuth:
10     sampler:
11       # 采样率值介于 0 到 1 之间, 1 则表示全部采集
12       # 默认值:0.1,即10%
13       probability: 1
14   #    base-url: http://localhost:9411
15   rabbitmq:
16     host: localhost
17     port: 5672
18     username: guest
19     password: guest

  6、测试

    1)启动Sleuth服务提供者项目

    2)访问地址:http://localhost:8010/payment/zipkin,正常获取内容

    3)在zipkin的web界面中,查看请求日志信息

      

    4)关闭Zipkin服务,访问地址:http://localhost:8010/payment/zipkin

      通过Web界面,查看RabbitMQ中的zipkin队列内容,发现有一个消息,且内容如下:

[
    {
        "traceId":"35381f02daf9c1a4",
        "id":"35381f02daf9c1a4",
        "kind":"SERVER",
        "name":"get /payment/zipkin",
        "timestamp":1589131166688083,
        "duration":6650,
        "localEndpoint":{
            "serviceName":"cloud-sleuth-provider",
            "ipv4":"192.168.1.4"
        },
        "remoteEndpoint":{
            "ipv6":"::1",
            "port":62161
        },
        "tags":{
            "http.method":"GET",
            "http.path":"/payment/zipkin",
            "mvc.controller.class":"PaymentController",
            "mvc.controller.method":"paymentZipkin"
        }
    }
]

    5)再次启动Zipkin服务,RabbitMQ中的zipkin队列消息,被消费了

 

posted on 2020-05-11 01:24  H__D  阅读(795)  评论(0编辑  收藏  举报