sleuth结合zipkin实现链路追踪(二) 基于MQ

基于上一篇的基于Http的稍加改造。

注册中心不需要做修改,跳过。

Zipkin-Server改动如下:

Gradle依赖:

    //作为eureka client依赖
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'

    //zipkin链路信息请求接收服务器,2.12.5开始使用armeria server
    implementation ("io.zipkin.java:zipkin-server:${zipkinVersion}") {
        //排除自带log与spring boot冲突造成stackOverflowError
        exclude group: "org.springframework.boot", module: "spring-boot-starter-log4j2"
    }
    implementation "io.zipkin.java:zipkin-autoconfigure-ui:${zipkinVersion}"

    //start 新增部分,zipkin收集器和rabbitmq
    implementation "io.zipkin.java:zipkin-autoconfigure-collector-rabbitmq:${zipkinVersion}"
    implementation 'org.springframework.amqp:spring-rabbit'
    //end

Application:

@EnableZipkinServer
@EnableDiscoveryClient
@SpringBootApplication
public class ZipkinServer01Application {

    //这些参数可以通过命令行传入,此处为了演示直接代码设置
    static {
        //zipkin-server-shared.yml内的配置环境变量key
        //设置mq地址和zipkin-ui监听端口
        System.setProperty("QUERY_PORT", "8080");
        System.setProperty("RABBIT_ADDRESSES", "localhost:5672");
        System.setProperty("RABBIT_USER", "guest");
        System.setProperty("RABBIT_PASSWORD", "guest");

    }

    /**
     * {@link ZipkinServer}
     * @param args
     */
    public static void main(String[] args) {
        //zipkin-server-shared.yml
        new SpringApplicationBuilder(ZipkinServer.class)
                .listeners(new RegisterZipkinHealthIndicators())
                //指定配置文件名称为zipkin-server,默认是application
                //zipkin-server.yml中指定profile=shared
                .properties("spring.config.name=zipkin-server").run(args);
    }
}

集成Sleuth的服务改动如下:

Gradle依赖:

    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    //web模块
    implementation 'org.springframework.boot:spring-boot-starter-web'
    //feign是对hystrix+ribbon的一层封装
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    //sleuth 链路追踪,使用mq,starter-zipkin中已包含该依赖,可以注释掉了,去掉的
    //implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'

    //start 由zipkin-report(sender)将统计信息发送到mq. 新增部分
    implementation 'org.springframework.cloud:spring-cloud-starter-zipkin'
    implementation 'org.springframework.amqp:spring-rabbit'
    //end

yml配置文件做如下修改:


spring:
  application:
    name: consumer02-sleuth
  #消息中间件rabbit配置
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
  #ZipkinProperties
  zipkin:
    #ZipkinSenderProperties mq采用rabbit, 可用的值为rabbit,kafka,web
    sender:
      type: rabbit
    # 使用mq接收统计信息,不需要配置baseUrl
    #baseUrl: http://zipkin-server01/
    #使用服务发现
    #discoveryClientEnabled: true
    #当前服务名称,不写默认取spring.application.name
    #service:
      #name: consumer02-sleuth
  #SamplerProperties 采样配置, 1=100%的请求进行采样
  sleuth:
    sampler:
      probability: 1

结束,启动rabbit, 注册中心,zipkin-server和带追踪的微服务(此处consumer02-sleuth), 访问hello接口,查看控制台日志信息,登陆zipkin-server的web界面查看接口访问链路信息。

可以登陆rabbit-web界面查看队列信息,默认情况下,队列名称为zipkin. 使用默认exchange。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值