SpringCloud微服务实战之分布式服务跟踪Sleuth

通常一个由客户端发起的请求会在后端系统中经过多个不同的微服务调用来协同产生最后的请求结果,在复杂的微服务架构系统中,几乎每一个前端请求都会形成一条复杂的分布式服务调用链路,每条链路服务痴线错误或者延迟都有可能引起请求的失败。Spring Cloud Sleuth提供了一套全链路调用跟踪以及监控分析方案。

一、测试链路跟踪
1、创建项目SpringCloud-Sleuth和SpringCloud-Trace2项目,项目Sleuth中调用Trace2项目中提供的服务接口。
这里写图片描述

这里写图片描述

2、pom中的依赖,两个项目相同

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- add ribbon depend -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <!-- add sleuth depend -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
    </dependencies>

3、Trace2项目中提供/trace-2接口

@RestController
public class TraceController {

    @RequestMapping(value="/trace-2",method=RequestMethod.GET)
    public String trace(){
        System.out.println("======================call Trace 2========================");
        return "Trace";
    }
}

Sleuth中使用RestTemplate调用Trace-2服务

@RestController
public class TraceController {


    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value="/trace-1",method=RequestMethod.GET)
    public String trace(){
        System.out.println("============>Call Trace=============");
        ResponseEntity<String> entity = restTemplate.getForEntity("http://trace-2/trace-2", String.class);

        return entity.getBody();
    }
}

Trace2主类

@EnableDiscoveryClient
@SpringBootApplication
public class Trace2Application {

    @Bean
    public AlwaysSampler defaultSampler(){
        return new AlwaysSampler();
    }
    public static void main(String[] args) {
        SpringApplication.run(Trace2Application.class, args);
    }


}

4、访问http://localhost:9003/trace-1

============>Call Trace=============
2017-12-25 14:32:48.808  INFO [sleuth-trace,1ccd077442207e68,191e2353159ad4bb,false] 11140 

二、整合Zipkin
1、创建项目SpringCloud-Zipkin
这里写图片描述

2、在pom中添加依赖

<dependencies>
        <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>io.zipkin.java</groupId>
            <artifactId>zipkin-server</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-ui</artifactId>    
            <!-- <version>2.4.2</version> -->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

3、在主类中使用注解@EnableZipkinServer 开启Zipkin服务。
4、在application中配置端口和服务名

server.port=9411

spring.application.name=zipkin-server

客户端默认连接Zipkin的url为http://localhost:9411, 故而这里的端口设置为9411,也可以改成其他端口。

5、启动服务,访问:http://localhost:9411

这里写图片描述

6、在(一)中的项目Sleuth和Trace中的pom中添加Zipkin依赖

<!-- add zipkin depend -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        </dependency>

7、在application.properties中添加配置,spring.zipkin.base-url默认配置为http://localhost:9411,所以这里不做配置也行。

spring.zipkin.base-url=http://localhost:9411

8、依次启动EurekaServer、Trace2、Sleuth项目,访问http://localhost:9004/trace-1,在ZipkinServer中心查看
这里写图片描述

点击Find Traces按钮,查询出结果,可以查看Service详细链路。
这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值