zoukankan      html  css  js  c++  java
  • 27.Spring Cloud Sleuth使用消息中间件收集数据

    前面已经介绍了使用HTTP直接收集跟踪数据,下面演示使用消息中间件收集跟踪数据。相比HTTP的方式。消息中间件有以下好处:

    微服务与ZipkinServer解耦,微服务无须知道ZipkinServer的网络地址。

    一些场景下,ZipkinServer与微服务网络可能不同,使用HTPP直接收集的方式无法工作,此时可借助消息中间件实现数据收集。

    ZipkinServer

    pom.xml

        <!--zipkin相关依赖 -->
            <dependency>
                <groupId>io.zipkin.java</groupId>
                <artifactId>zipkin-autoconfigure-ui</artifactId>
            </dependency>
    
            <dependency>
                <groupId>io.zipkin.java</groupId>
                <artifactId>zipkin-server</artifactId>
            </dependency>
    
            <!--集成消息中间件 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
            </dependency>

    application.properties

    需要添加消息中间件连接信息(以rabbitmq为例)

    server.port=9411
    #注册中心地址
    eureka.client.serviceUrl.defaultZone=http://testhost:8000/eureka/,http://testhost2:8001/eureka/
    #把客户端的检测检测交给actuator来完成
    eureka.client.healthcheck.enabled=true
    spring.application.name=zipkin-server
    
    spring.rabbitmq.host=localhost
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=springcloud
    spring.rabbitmq.password=123456

    启动类

    package com.niugang;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.sleuth.zipkin.stream.EnableZipkinStreamServer;
    
    
    /**
     * zipserver启动类
     * @author niugang
     *
     */
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableZipkinStreamServer
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

    各微服务需要调整的地方

    pom.xml

    <!--分布式追踪 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-sleuth-stream</artifactId>
    </dependency>

    application.properties

    需要添加消息中间件连接信息(以rabbitmq为例)

    #指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问
    spring.application.name=ribbon-consumer-trace-zipkin
    server.port=9001
    #负载平衡RestTemplate可以配置为重试失败的请求。默认情况下,该逻辑被禁用
    spring.cloud.loadbalancer.retry.enabled=true
    #注册中心地址
    eureka.client.serviceUrl.defaultZone=http://testhost:8000/eureka/
    logging.level.root=info
    #注意:您可以设置logging.level.org.springframework.web.servlet.DispatcherServlet = DEBUG,而不是在处理程序中明确记录请求。
    logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG
    spring.sleuth.sampler.percentage=1.0
    #rabbitmq相关配置
    spring.rabbitmq.host=localhost
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=springcloud
    spring.rabbitmq.password=123456
    
    

    配置以上,启动注册中心,zipkinserver,各个微服务。访问http://localhost:9411/查看追踪收集的数据。

       

    微信公众号

                              
  • 相关阅读:
    bzoj1015星球大战(并查集+离线)
    bzoj1085骑士精神(搜索)
    bzoj1051受欢迎的牛(Tarjan)
    左偏树学习
    hdu1512 Monkey King(并查集,左偏堆)
    左偏树(模板)
    PAT (Basic Level) Practice (中文) 1079 延迟的回文数 (20分) (大数加法)
    PAT (Basic Level) Practice (中文) 1078 字符串压缩与解压 (20分) (字符转数字——栈存放)
    PAT (Basic Level) Practice (中文) 1077 互评成绩计算 (20分) (四舍五入保留整数)
    PAT (Basic Level) Practice (中文) 1076 Wifi密码 (15分)
  • 原文地址:https://www.cnblogs.com/niugang0920/p/12188159.html
Copyright © 2011-2022 走看看