《SpringCloud微服务实战》学习笔记之分布式服务跟踪Sleuth

来源:互联网 发布:用友数据库重装 编辑:程序博客网 时间:2024/06/02 04:41

通常一个由客户端发起的请求会在后端系统中经过多个不同的微服务调用来协同产生最后的请求结果,在复杂的微服务架构系统中,几乎每一个前端请求都会形成一条复杂的分布式服务调用链路,每条链路服务痴线错误或者延迟都有可能引起请求的失败。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接口

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

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

@RestControllerpublic 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@SpringBootApplicationpublic 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=9411spring.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详细链路。
这里写图片描述

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 驾考 交通考试 交通法规考试题及答案 交通规则 驾照理论考试速成 肇事逃逸怎么处罚新交规 肇事逃逸处罚新交规 交警 贵州交警 深圳交警 汕头交警 交警123123 交警手势 深圳交警网 交警报警 山西交警网 交警大队 交警app 北京交警app 贵州交警app 交警违章 金华交警 武汉交警 山东交警 广西交警 北京交警 西安交警 广西交警网 投诉交警 山西交警 临汾交警网 临沂交警网 交警平台 中国交警 北京交警网 交警信息网 乐清交警网 交警1213 交警在线 公安交警 交警网