首页 > 编程语言 > 详细

第九篇:服务链路追踪(Spring Cloud Sleuth)

时间:2020-03-09 21:39:50      阅读:67      评论:0      收藏:0      [点我收藏+]

1.Spring Cloud Sleuth介绍

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

2.准备server-zipkin

在spring Cloud为F版本的时候,已经不需要自己构建Zipkin Server了,只需要下载jar即可启动即可

下载地址:https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

下载java -jar zipkin-server-2.10.1-exec.jar

进入解压后的文件下,进入cmd,输入java -jar zipkin-server-2.12.9-exec.jar,开启服务

技术分享图片

打开浏览器,访问http://127.0.0.1:9411/,可以看到

技术分享图片

3.构建工程

新建Maven父工程,eureka-server子工程,如第一篇文章

创建eureka-client子工程,pom文件如下

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-sleuth</artifactId>
        <groupId>com.niuben</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-client</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--Eureka Client依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--Web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--zipkin-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
    </dependencies>

    <!--Maven打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

编写application.yml配置文件

server:
  port: 8988
spring:
  application:
    name: eureka-client
  zipkin:
    base-url: http://localhost:9411
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

启动类加载@EnableEurekaClient,开始Eureka客户端

@SpringBootApplication
@EnableEurekaClient // 开始Eureka客户端
public class EurekaClientApplication {

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

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

    /**
     * Brave是一个用于捕捉和报告分布式操作的延迟信息给Zipkin的工具库
     * Sampler.ALWAYS_SAMPLE返回一个sampler,设置需要采样
     */
    @Bean
    public Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }

}

创建controller层,TestEurekaClient类

@RestController
public class TestEurekaClient {

    @Autowired
    private RestTemplate restTemplate;

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

    @RequestMapping("/hi")
    public String home(){
        return "i am eureka-client-two";
    }

}

 

@RestController
public class TestEurekaClient {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/test")
    public String callHome() {
        return restTemplate.getForObject("http://localhost:8989/miya", String.class);
    }

    @RequestMapping("/info")
    public String info() {
        return "i am eureka-client";

    }

}

创建eureka-client-two子工程,pom文件如下

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-sleuth</artifactId>
        <groupId>com.niuben</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-client-two</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--Eureka Client依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--Web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--zipkin-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
    </dependencies>

    <!--Maven打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

编写application.yml配置文件

server:
  port: 8989
spring:
  application:
    name: eureka-client-two
  zipkin:
    base-url: http://localhost:9411
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

启动类加载@EnableEurekaClient,开始Eureka客户端

@SpringBootApplication
@EnableEurekaClient // 开始Eureka客户端
public class EurekaClientTwoApplication {

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

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

    /**
     * Brave是一个用于捕捉和报告分布式操作的延迟信息给Zipkin的工具库
     * Sampler.ALWAYS_SAMPLE返回一个sampler,设置需要采样
     */
    @Bean
    public Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }

}

依次启动eureka-server,eureka-client,eureka-client-two,打开浏览器访问:http://localhost:9411/,再访问http://localhost:8989/test2

点击依赖查看依赖关系

技术分享图片

 

查找,进入查看具体详情

 技术分享图片

 

 技术分享图片

 

 

源码:https://gitee.com/niugit/spring-cloud-sleuth

第九篇:服务链路追踪(Spring Cloud Sleuth)

原文:https://www.cnblogs.com/niudaben/p/12451500.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!