Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Zipkin持久化mysql存储rabbitmq消息队列链路追踪(四)

问题背景

上一篇文章使用了zipkin的追踪链路使用的是http的方式,是一种同步的方式,其中有一个微服务一直没有返回,链路会一直卡住,并且默认使用内存记录的,一旦zipkin重启之后,之前的链路就消失了,所这个篇章介绍使用mysql进行持久化,并且使用rabbitmq进行异步消费,提高效率
注意事项:

  zipkin:
    base-url: http://10.10.195.199:9411/
    sender:
      type: rabbit    # web
    rabbitmq:
      queue: zipkin   # 队列名称
    locator:
      discovery:
        enabled: true
  sleuth:
    sampler:
      probability: 1.0

Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+FeignClient+Nacos通过网关远程调用微服务(一)

Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)

Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Zipkin安装部署可视化http方式链路追踪(三)

Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Zipkin持久化mysql存储rabbitmq消息队列链路追踪(四)

mysql数据库创建

1 使用navicat连接mysql,并进行创建

2 创建 zipkin 数据库名字,不要搞错了哦,可以创建其他名字,但怕你启动的时候忘了改

3 选择运行sql文件

4 sql文件如下,也可以之间在编译器里面运行

CREATE TABLE IF NOT EXISTS zipkin_spans (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL,
  `id` BIGINT NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `remote_service_name` VARCHAR(255),
  `parent_id` BIGINT,
  `debug` BIT(1),
  `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
  `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
  PRIMARY KEY (`trace_id_high`, `trace_id`, `id`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
ALTER TABLE zipkin_spans ADD INDEX(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames';
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';

CREATE TABLE IF NOT EXISTS zipkin_annotations (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
  `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
  `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
  `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
  `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
  `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
  `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
  `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';

CREATE TABLE IF NOT EXISTS zipkin_dependencies (
  `day` DATE NOT NULL,
  `parent` VARCHAR(255) NOT NULL,
  `child` VARCHAR(255) NOT NULL,
  `call_count` BIGINT,
  `error_count` BIGINT,
  PRIMARY KEY (`day`, `parent`, `child`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;


rabbitmq安装部署

1 解压压缩包之后,可以看见以下几个文件

2 通过xftp把文件放入centos通过指令进行安装,注意安装顺序需要安装下面来安装

rpm -ivh erlang-21.3.8.16-1.el7.x86_64.rpm

rpm -ivh socat-1.7.3.2-5.el7.lux.x86_64.rpm

rpm -ivh rabbitmq-server-3.8.6-1.el7.noarch.rpm


3 启动rabbitmq

systemctl start rabbitmq-server.service

4 查看进程号

ps -ef | grep rabbitmq

5 关闭防火墙

systemctl stop firewalld

6 浏览器输入http://10.10.195.199:15672,输入你所安装的IP

7 默认的账号guest,密码guest,在rabbitmq3.0版本之后不允许远程访问了,User can only log in via localhost,可以创建远程访问的用户

8 创建远程用户,账号:root 密码:123456

rabbitmqctl add_user root 123456

9 设置root用户角色

rabbitmqctl set_user_tags root administrator

10 设置root用户权限

rabbitmqctl set_permissions -p "/" root ".*" ".*" ".*"

11 查看当前用户和角色

rabbitmqctl list_users

12 验证用户鉴权

rabbitmqctl authenticate_user 'root' '123456'


13 登录http://10.10.195.199:15672,输入root,密码:123456

项目搭建

1 gateway微服务

1.1 通过前几篇文章的代码进行更改,引入pom依赖,新添加rabbitmq依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yg</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway</name>
    <description>gateway</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
<!--            <version>2.1.0.RELEASE</version>-->
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
<!--            <scope>test</scope>-->
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!--            <version>2.1.0.RELEASE</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

1.2 更改application配置文件

server:
  port: 3900

spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: service2  #设置路由id(理论上是可以随便写的)
          uri: lb://service2 #设置路由的url lb://nacos服务注册名称 lb://service1 http://localhost:3901
          predicates:
            - Path=/three/** #路径匹配规则,微服务必须有一个统一的入口,不然网关不能访问,@RequestMapping("/three")
        - id: service1  #设置路由id(理论上是可以随便写的)
          uri: lb://service1 #设置路由的url lb://nacos服务注册名称 lb://service1 http://localhost:3901
          predicates:
            - Path=/four/** #路径匹配规则,@RequestMapping("/four")

  zipkin:
    base-url: http://10.10.195.199:9411/
    sender:
      type: rabbit    # web
    rabbitmq:
      queue: zipkin   # 队列名称
    locator:
      discovery:
        enabled: true
  sleuth:
    sampler:
      probability: 1.0

  rabbitmq:
    host: 10.10.195.199
    port: 5672   # 单机端口
    username: root  # 账号
    password: 123456  # 密码
    virtual-host: /  # 虚拟主机地址
    listener:
      direct:
        retry:
          enabled: true   # 开启发布重试
          max-attempts: 5 #重试次数
          initial-interval: 5000 # 重试间隔
      simple:
        retry:
          enabled: true   #开启消费重试
          max-attempts: 5  # 重试次数
          initial-interval: 5000 #重试间隔

2 service微服务

2.1 更改service公共pom文件,service1和service2单独的pom文件没有变化

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yg</groupId>
    <artifactId>sleuthTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <modules>
        <module>service1</module>
        <module>service2</module>
    </modules>

    <name>sleuthTest</name>
    <description>sleuthTest</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!--            <version>2.1.0.RELEASE</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!--Spring Cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2.2 更改service1的application配置

server:
  port: 3901

spring:
  application:
    name: service1
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

  zipkin:
    base-url: http://10.10.195.199:9411/
    sender:
      type: rabbit    # web
    rabbitmq:
      queue: zipkin   # 队列名称
    locator:
      discovery:
        enabled: true
  sleuth:
    sampler:
      probability: 1.0

  rabbitmq:
    host: 10.10.195.199
    port: 5672   # 单机端口
    username: root  # 账号
    password: 123456  # 密码
    virtual-host: /  # 虚拟主机地址
    listener:
      direct:
        retry:
          enabled: true   # 开启发布重试
          max-attempts: 5 #重试次数
          initial-interval: 5000 # 重试间隔
      simple:
        retry:
          enabled: true   #开启消费重试
          max-attempts: 5  # 重试次数
          initial-interval: 5000 #重试间隔

2.3 更改service2的application配置

server:
  port: 3902

spring:
  application:
    name: service2
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

  zipkin:
    base-url: http://10.10.195.199:9411/
    sender:
      type: rabbit    # web
    rabbitmq:
      queue: zipkin   # 队列名称
    locator:
      discovery:
        enabled: true
  sleuth:
    sampler:
      probability: 1.0

  rabbitmq:
    host: 10.10.195.199
    port: 5672   # 单机端口
    username: root  # 账号
    password: 123456  # 密码
    virtual-host: /  # 虚拟主机地址
    listener:
      direct:
        retry:
          enabled: true   # 开启发布重试
          max-attempts: 5 #重试次数
          initial-interval: 5000 # 重试间隔
      simple:
        retry:
          enabled: true   #开启消费重试
          max-attempts: 5  # 重试次数
          initial-interval: 5000 #重试间隔

测试步骤

1 启动nacos服务端,zipkin服务端,gateway,service1,service2

  • 启动zipkin的参数变了,由于是zipkin和rabbitmq在一个服务器,所以地址使用的是127.0.0.1,没有使用这个地址会报这个错误:PLAIN login refused: user ‘root’ - invalid credentials
  • 在服务器下面就可以使用guest账号和密码了
  • –RABBIT_QUEUE=zipkin,如果没有zipkin这个队列,会自动创建
nohup java -jar zipkin-server-2.23.16-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=localhost --MYSQL_TCP_PORT=3306 --MYSQL_USER=root --MYSQL_PASS=123456 --MYSQL_DB=zipkin --RABBIT_ADDRESSES=127.0.0.1:5672 --RABBIT_USER=guest --RABBIT_PASSWORD=guest --RABBIT_VIRTUAL_HOST=/ --RABBIT_QUEUE=zipkin >> zipkin.log &



2 查看注册中心http://localhost:8848/nacos,密码和账号都为:nacos

3 使用网关路由调用service1微服务API,service1微服务调用service2

4 查看rabbitmq队列,业务发布消息,由于zipkin马上就消费了,所以看不到ready的数量,但可以把zipkin先关闭,查看后再启动

5 查看zipkin,点击RUN QUERY,显示调用链路

6 点击红框,查看依赖链路
7 通过注册中心使用微服务名显示链路关系

心得

  • 做的这套组件,中间调试的版本兼容bug太多了,中间件都要自己安装,比较麻烦,有时间讲解一下docker吧,测试方便




作为程序员第 21 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric: 吐气在我的耳朵

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值