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

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

准备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/,可以看到

构建工程

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

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

 <!--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>

编写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
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文件如下

  <!--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>

编写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,service-client,service-client-two,打开浏览器访问:http://localhost:9411/,再访问http://localhost:8989/test2

点击依赖查看依赖关系

 

查找,进入查看具体详情

 

 

 

源码:https://gitee.com/niugit_admin/spring-cloud-finchley

posted @ 2020-03-09 20:59  如何下笔呢  阅读(257)  评论(0编辑  收藏  举报
levels of contents