【Zipkin】01-Zipkin与SpringCloud

Zipkin

简介

链路追踪。在微服务项目中,用户的一次完整的请求,可能会由多台服务器中不同的服务来完成。如果一个服务部署到了多台服务器,那么该请求通过这个服务时,一定只会通过该服务中的一台服务器。在我们需要查询该请求通过服务器的完整路径时,就需要通过链路追踪来解决该问题。

应用

安装

在Linux服务器的docker中安装

1
2
3
4
5
6
# 1. 拉取项目
docker pull openzipkin/zipkin
# 2. 启动
docker run -d --restart always -p 9411:9411 --name zipkin openzipkin/zipkin
# 3. 访问
http://ip:9411/zipkin/
  1. 拉取项目

    docker pull openzipkin/zipkin

  2. 启动项目

    docker run -d --restart always -p 9411:9411 --name zipkin openzipkin/zipkin

  3. 访问客户端页面

    http://ip:9411/zipkin/

    img-03

编写

在下面链路追踪的使用中,我选择使用一个feign调用的服务,两个service提供接口的服务。

cloud-service服务

将该代码复制两次,记得修改端口号

  1. 项目结构

    img-01

  2. pom.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <dependencies>
    <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>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.5.8</version>
    </dependency>
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
    </dependency>
    </dependencies>
  3. application.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    server:
    port: 8011
    spring:
    application:
    name: cloud-service
    # 链路追踪配置
    zipkin:
    base-url: http://api.zipkin:9411
    sleuth:
    sampler:
    probability: 1 # 采样率介于0~1之间,1表示全部采样
    eureka:
    client:
    service-url:
    defaultZone: http://eureka8001.com:8001/eureka
  4. ServiceMain8011.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    /**
    * 2. 引入链路追踪zipkin-sleuth
    * 2.0 docker安装zipkin
    * 1. `docker pull openzipkin/zipkin`
    * 2. `docker run -d --restart always -p 9411:9411 --name zipkin openzipkin/zipkin`
    * 3. 访问:http://ip:9411/zipkin/
    * 2.1 依赖
    * spring-cloud-starter-zipkin
    * 2.2 配置
    * spring:
    * zipkin:
    * base-url: http://api.zipkin:9411
    * sleuth:
    * sampler:
    * probability: 1
    * @author zsq
    * @date 2021年08月27日 18:09:51
    */
    @EnableEurekaClient
    @SpringBootApplication
    public class ServiceMain8011 {

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

    }
  5. TestController.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @Slf4j
    @RestController
    @RequestMapping("/test")
    public class TestController {

    @GetMapping("/ok/{id}")
    public String ok(@PathVariable String id) {
    String result = "service - ok - id = " + id;
    log.info(result);
    return result;
    }

    }

cloud-feign-hystrix服务

通过feign,调用cloud-service的接口

  1. 项目结构

    img-02

  2. pom.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    <dependencies>
    <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>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.5.8</version>
    </dependency>
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
    </dependency>
    </dependencies>
  3. application.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    server:
    port: 8021
    spring:
    application:
    name: cloud-feign
    zipkin:
    base-url: http://api.zipkin:9411
    sleuth:
    sampler:
    probability: 1
    eureka:
    client:
    service-url:
    defaultZone: http://eureka8001.com:8001/eureka
    # 开启feign
    feign:
    hystrix:
    enabled: true
    # 开启Hystrix Dashboard访问
    management:
    endpoints:
    web:
    exposure:
    include: hystrix.stream
  4. FeignMain8021.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    /**
    * 1. 添加依赖:spring-cloud-starter-openfeign,spring-cloud-starter-netflix-hystrix
    * 2. 启动Feign调用的熔断Hystrix,还需添加配置
    * # 开启feign
    * feign:
    * hystrix:
    * enabled: true
    * 3. 启动Feign,添加注解:@EnableFeignClients;启动Hystrix,添加注解:@EnableHystrix
    * 4. 若使用Hystrix Dashboard启动监控,需要添加actuator配置
    * 4.1 依赖 spring-boot-starter-actuator
    * 4.2 配置文件
    * # 开启Hystrix Dashboard访问
    * management:
    * endpoints:
    * web:
    * exposure:
    * include: hystrix.stream
    * 4.3 注册Bean容器
    * .@Bean
    * public ServletRegistrationBean getServlet() {
    * HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    * ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
    * registrationBean.setLoadOnStartup(1);
    * registrationBean.addUrlMappings("/hystrix.stream");
    * registrationBean.setName("HystrixMetricsStreamServlet");
    * return registrationBean;
    * }
    * @author zsq
    * @date 2021年08月28日 00:39:27
    */
    @EnableEurekaClient
    @EnableFeignClients
    @EnableHystrix
    @SpringBootApplication
    public class FeignMain8021 {

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

    /**
    * 此配置是为了服务监控配置,与服务容错本身无关,Spring Cloud 升级后的坑
    * ServletRegistrationBean因为springboot的默认路径不是hystrix.stream
    * 只要在自己的项目里配置下面的servlet就可以了
    * @return ServletRegistrationBean
    */
    @Bean
    public ServletRegistrationBean getServlet() {
    HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
    registrationBean.setLoadOnStartup(1);
    registrationBean.addUrlMappings("/hystrix.stream");
    registrationBean.setName("HystrixMetricsStreamServlet");
    return registrationBean;
    }

    }
  5. TestFeignController.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    @Slf4j
    @RestController
    @RequestMapping("/feign")
    public class TestFeignController {

    @Resource
    private TestFeign testFeign;

    @GetMapping("/ok/{id}")
    public String ok(@PathVariable("id") String id) {
    String result = "feign - service - ok - id = " + id;
    log.info(result);
    return testFeign.ok(id);
    }

    }
  6. TestFeign.java

    1
    2
    3
    4
    5
    6
    7
    @FeignClient(name = "CLOUD-SERVICE", fallback = TestFeignImpl.class)
    public interface TestFeign {

    @GetMapping("/test/ok/{id}")
    String ok(@PathVariable("id") String id);

    }
  7. TestFeignImpl.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @Slf4j
    @Component
    public class TestFeignImpl implements TestFeign {

    public String ok(String id) {
    String result = "fallback - service - ok - id = " + id;
    log.info(result);
    return result;
    }

    }

启动项目

img-04

使用

  1. 调用接口http://localhost:8021/feign/ok/1,打开链路追踪页面

    img-05

  2. 查看详情

    img-06

-------------本文结束感谢您的阅读-------------