美文网首页
SpringCloud 2020.0.4 系列之 Sleuth

SpringCloud 2020.0.4 系列之 Sleuth

作者: 追风人聊Java | 来源:发表于2022-01-13 21:26 被阅读0次

1. 概述

老话说的好:安全不能带来财富,但盲目的冒险也是不可取的,大胆筹划,小心实施才是上策。

言归正传,微服务的特点就是服务多,服务间的互相调用也很复杂,就像一张关系网,因此为了更好的定位故障和优化性能,我们需要有工具帮我们很快的梳理出服务间上下游的调用关系。Sleuth 就可以很好的帮我们解决这个问题。

但 Sleuth 只是做了后台工作,没有展示页面,不方便我们对调用链进行分析。因此需要 Zipkin 去把数据展示出来,方便我们进行分析。

闲话不多说,直接上代码。

2. Zipkin 服务的搭建

2.1 Zipkin 服务的搭建方式

我们打开Zipkin的官网地址:https://zipkin.io/pages/quickstart.html

image

如图所示,Zipkin Server 支持 Docker方式启动,Java 运行 Jar 包方式启动,以及编译源码的方式启动。

2.2 Java 运行 jar 包方式启动

大家可以根据自己的喜好选择启动方式,这里我们选择 Java 运行 jar 包的方式启动。

1)下载 zipkin.jar

# curl -sSL https://zipkin.io/quickstart.sh | bash -s

2)启动 Zipkin Server

# java -jar zipkin.jar

2.3 访问 Zipkin Server

http://IP:9411/zipkin/

image

3. Demo 服务的搭建

3.1 概述

这里我们搭建两个有调用关系的服务作为 Demo。

两个服务都依赖 Sleuth 和 Zipkin。

3.2 my-sleuth-A 的搭建

3.2.1 主要依赖

        <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-web</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-sleuth</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
            <version>2.2.8.RELEASE</version>
        </dependency>

3.2.2 主要配置

server:
  port: 46000
spring:
  application:
    name: my-sleuth-A
  sleuth:
    sampler:
      probability: 1  # 采样率100%
  zipkin:
    base-url: http://192.168.1.22:9411  # Zipkin Server 地址

eureka:
  client:
    service-url:
      defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/    # Eureka Server的地址

3.2.3 启动类

@SpringBootApplication
@EnableDiscoveryClient
public class MySleuthAApplication {

    @LoadBalanced
    @Bean
    public RestTemplate lb() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(MySleuthAApplication.class, args);
    }
}

3.2.4 Demo Controller

@RestController
@Slf4j
public class Controller {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/traceA")
    public String traceA() {
        log.info("-------- traceA -------");
        return restTemplate.getForEntity
                        ("http://my-sleuth-B/traceB", String.class).getBody();
    }
}

3.3 my-sleuth-B 的搭建

3.3.1 主要依赖

        <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-web</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-sleuth</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
            <version>2.2.8.RELEASE</version>
        </dependency>

3.3.2 主要配置

server:
  port: 46001
spring:
  application:
    name: my-sleuth-B
  sleuth:
    sampler:
      probability: 1  # 采样率100%
  zipkin:
    base-url: http://192.168.1.22:9411  # Zipkin Server 地址

eureka:
  client:
    service-url:
      defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/    # Eureka Server的地址

3.3.3 启动类

@SpringBootApplication
@EnableDiscoveryClient
public class MySleuthBApplication {

    @LoadBalanced
    @Bean
    public RestTemplate lb() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(MySleuthBApplication.class, args);
    }
}

3.3.4 Demo Controller

@RestController
@Slf4j
public class Controller {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/traceB")
    public String traceB() {
        log.info("-------- traceB -------");
        return "traceB";
    }
}

4. Zipkin 的简单使用

4.1 调用 Demo 接口

调用两个Demo工程的接口

GET http://localhost:46001/traceB

GET http://localhost:46000/traceA

4.2 打开 zipkin 查看

image image

4.3 依赖关系图查看

image

5. 综述

今天聊了一下 调用链追踪 Sleuth + Zipkin ,希望可以对大家的工作有所帮助。

欢迎帮忙点赞、评论、转发、加关注 :)

关注追风人聊Java,每天更新Java干货。

相关文章

  • SpringCloud 2020.0.4 系列之 Sleuth

    1. 概述 老话说的好:安全不能带来财富,但盲目的冒险也是不可取的,大胆筹划,小心实施才是上策。 言归正传,微服务...

  • SpringCloud 2020.0.4 系列之Eureka

    1. 概述 老话说的好:遇见困难,首先要做的是积极的想解决办法,而不是先去泄气、抱怨或生气。 言归正传,微服务是当...

  • SpringCloud 2020.0.4 系列之 Bus

    1. 概述 老话说的好:会休息的人才更会工作,身体是革命的本钱,身体垮了,就无法再工作了。 言归正传,之前我们聊了...

  • SpringCloud 2020.0.4 系列之 Stream

    1. 概述 老话说的好:出错不怕,怕的是出了错,却不去改正。如果屡次出错,无法改对,就先记下了,然后找援军解决。 ...

  • SpringCloud 2020.0.4 系列之 Gateway

    1. 概述 老话说的好:做人要有幽默感,懂得幽默的人才会活的更开心。 言归正传,今天我们来聊聊 SpringClo...

  • SpringCloud 2020.0.4 系列之 Config

    1. 概述 老话说的好:一条路走不通,就去走另一条路,A计划执行不下去,就按B计划执行,多准备几套方案总是有用的。...

  • SpringCloud 2020.0.4 系列之 Feign

    1. 概述 老话说的好:任何问题都有不止一种的解决方法,当前的问题没有解决,只是还没有发现解决方法,而并不是无解。...

  • SpringCloud 2020.0.4 系列之 Stream

    1. 概述 老话说的好:事情太多,做不过来,就先把事情记在本子上,然后理清思路、排好优先级,一件一件的去完成。 言...

  • SpringCloud 2020.0.4 系列之 Stream

    1. 概述 老话说的好:对待工作要有责任心,不仅要完成自己的部分,还要定期了解整体的进展。 言归正传,我们在开发产...

  • SpringCloud 2020.0.4 系列之Hystrix看

    1. 概述 老话说的好:沉默是金,有时适当的沉默,比滔滔不绝更加有效。 言归正传,前面我们聊了有关 Hystrix...

网友评论

      本文标题:SpringCloud 2020.0.4 系列之 Sleuth

      本文链接:https://www.haomeiwen.com/subject/swrocrtx.html