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