原创

Spring Cloud 学习笔记9 服务链路追踪(Spring Cloud Sleuth)

Spring Cloud 学习笔记9 服务链路追踪(Spring Cloud Sleuth)

本次代码都已经提交在github上,点击这里访问,项目当前的构建状态:构建状态

本篇代码基于上篇文章的代码,见Spring Cloud 学习笔记8 消息总线(Spring Cloud Bus)

Spring Cloud Sleuth 集成了zipkin.

微服务架构上通过业务来划分服务的,通过REST调用,对外暴露的一个接口,可能需要很多个服务协同才能完成这个接口功能,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败。随着业务的不断扩张,服务之间互相调用会越来越复杂。

1.运行zipkin服务

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

你如果是windows系统,则需要Shell Script运行时,推荐下载git的时候,直接带git的bash,这样就可以自动跑了。否则,你需要在这个地址进行下载.
下载后,使用

java -jar zipkin.jar

运行即可。


                  oo
                 oooo
                oooooo
               oooooooo
              oooooooooo
             oooooooooooo
           ooooooo  ooooooo
          oooooo     ooooooo
         oooooo       ooooooo
        oooooo   o  o   oooooo
       oooooo   oo  oo   oooooo
     ooooooo  oooo  oooo  ooooooo
    oooooo   ooooo  ooooo  ooooooo
   oooooo   oooooo  oooooo  ooooooo
  oooooooo      oo  oo      oooooooo
  ooooooooooooo oo  oo ooooooooooooo
      oooooooooooo  oooooooooooo
          oooooooo  oooooooo
              oooo  oooo

     ________ ____  _  _____ _   _
    |__  /_ _|  _ \| |/ /_ _| \ | |
      / / | || |_) | ' / | ||  \| |
     / /_ | ||  __/| . \ | || |\  |
    |____|___|_|   |_|\_\___|_| \_|

:: version 2.21.7 :: commit 7c8e569 ::

2020-08-24 16:04:01.200  INFO 14140 --- [oss-http-*:9411] c.l.a.s.Server                           : Serving HTTP at /0:0:0:0:0:0:0:0:9411 - http://127.0.0.1:9411/

将会在本地监听9411端口。

2.创建模块zipkinfirst

2.1修改zipkiinfirst的pom.xml,类似于eurekaclient的

pom.xml如下

<?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>cn.djc8</groupId>
        <artifactId>blog2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>cn.djc8</groupId>
    <artifactId>zipkinfirst</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>zipkinfirst</name>
    <description>zipkin first project</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-spring-beans</artifactId>
            <version>5.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.2 修改application.yml,路径zipkinfirst\src\main\resources\,增加zipkin地址

server:
  port: 7401
spring:
  zipkin:
    base-url: http://localhost:9411
  application:
    name: zipkinfirst

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/

2.3 修改ZipkinfirstApplication.java,增加暴露的接口

此时ZipkinfirstApplication.java如下

package cn.djc8.zipkinfirst;

import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
@EnableEurekaClient
public class ZipkinfirstApplication {

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

    @Autowired
    private RestTemplate restTemplate;

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

    /**
     * 调用另外的服务
     * @return
     */
    @RequestMapping("/getzipkintwo")
    public String getzipkintwo(){
        //因为已经注册到注册中心,所以可以直接使用服务名来调用方法
        return restTemplate.getForObject("http://zipkintwo/getzipkintwo", String.class);
    }

    @RequestMapping("/info")
    public String info(){
        return "i'm zipkinfirst interface";

    }

    @Bean
    public Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }

}

3.创建模块zipkintwo

3.1修改zipkintwo的pom.xml,跟zipkinfirst一样的方式,

pom.xml文件如下

<?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>cn.djc8</groupId>
        <artifactId>blog2</artifactId>
n        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>cn.djc8</groupId>
    <artifactId>zipkintwo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>zipkintwo</name>
    <description>zipkin two project</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-spring-beans</artifactId>
            <version>5.5.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.2修改application.yml,如下


server:
  port: 7403
spring:
  zipkin:
    base-url: http://localhost:9411
  application:
      name: zipkintwo

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/

####3.3 修改ZipkintwoApplication.java,如下

package cn.djc8.zipkintwo;

import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
@EnableEurekaClient
public class ZipkintwoApplication {

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

    @RequestMapping("/getzipkintwo")
    public String getzipkintwo(){
        return "hi i'm zipkintwo!";
    }

    @RequestMapping("/info")
    public String info(){
        return restTemplate.getForObject("http://zipkinfirst/info",String.class);
    }

    @Autowired
    private RestTemplate restTemplate;

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


    @Bean
    public Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }

}

4.测试

依次启动项目:eurekaserver,zipkinfirst,zipkintwo.等待启动完成

打开http://localhost:7401/getzipkintwo,浏览器显示
hi i'm zipkintwo!
打开http://localhost:7403/info,浏览器显示
i'm zipkinfirst interface
这时候说明调用链路是通的,然后打开http://127.0.0.1:9411/
就可以看到这两个服务调用来调用去的。

1.png


后记
我这里有用到注册中心,方大佬的还没有用到注册中心,我认为既然学习了微服务的整体架构,最好在后续的学习中主动去应用。

参考

  1. 记一次zipkin和spring cloud bus冲突
  2. 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

本文来自:Spring Cloud 学习笔记9 服务链路追踪(Spring Cloud Sleuth)-小码农,转载请保留本条链接,感谢!

温馨提示:
本文最后更新于 2022年03月01日,已超过 996 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
正文到此结束
该篇文章的评论功能已被站长关闭
本文目录