什么是服务链路
检查调用关系,像前面的只有两个微服务还好,如果非常多的话还靠读代码估计够呛。所以呢,就引入了一个东西——分布式系统调用跟踪,目前使用最广泛的是使用最为广泛的开源实现是 Twitter 的 zipkin,zipkin 服务链路追踪服务器可以用图片进行识别。
下载与启动链路追踪服务器
1、在参考【1】中下,是个jar包,更多地了解直接看参考【2】,介绍的比较详细。
2、启动链路追踪服务器,用这个命令
java -jar zipkin-server-2.10.1-exec.jar
乌拉拉一片,也不知道什么东西。
3、访问网址:http://localhost:9411/zipkin/dependency/
改造项目
改造范围
eureka-server 不需要做改造。
product-data-service和product-view-service 需要进行改造以使其可以被追踪到。
pom.xml
添加 zipkin 的jar包
product-data-service
<?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>springcloud</artifactId> <groupId>edu.hpu.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>product-data-service</artifactId> <name>product-data-service</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> </dependencies> </project>
product-view-service-fegin
<?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>springcloud</artifactId> <groupId>edu.hpu.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>product-view-service-feign</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <!--对feign方式的支持--> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> </dependencies> </project>
application.yml
在配置中添加zipkin的基础路径。
product-data-service
# server: # port: 因为会启动多个 product-data-service, 所以端口号由用户自动设置,推荐 8001,8002,8003 spring: application: name: product-data-service zipkin: base-url: http://localhost:9411 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
product-view-service-fegin
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ spring: application: name: product-view-service-feign zipkin: base-url: http://localhost:9411 thymeleaf: cache: false prefix: classpath:/templates/ suffix: .html encoding: UTF-8 content-type: text/html mode: HTML5
启动类
在启动类里配置 Sampler 抽样策略: ALWAYS_SAMPLE 表示持续抽样。
@Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; }
product-data-service
package edu.hpu.springcloud; import brave.sampler.Sampler; import cn.hutool.core.convert.Convert; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.NetUtil; import cn.hutool.core.util.NumberUtil; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import java.util.Scanner; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @SpringBootApplication @EnableEurekaClient public class ProductDataServiceApplication { public static void main(String[] args) { int port = 0; int defaultPort = 8003; Future<Integer> future = ThreadUtil.execAsync(() ->{ int p = 0; System.out.println("请于5秒钟内输入端口号, 推荐 8001 、 8002 或者 8003,超过5秒将默认使用 " + defaultPort); Scanner scanner = new Scanner(System.in); while(true) { String strPort = scanner.nextLine(); if(!NumberUtil.isInteger(strPort)) { System.err.println("只能是数字"); continue; } else { p = Convert.toInt(strPort); scanner.close(); break; } } return p; }); try{ port=future.get(5, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e){ port = defaultPort; } if(!NetUtil.isUsableLocalPort(port)) { System.err.printf("端口%d被占用了,无法启动%n", port ); System.exit(1); } new SpringApplicationBuilder(ProductDataServiceApplication.class).properties("server.port=" + port).run(args); } @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
product-view-service-fegin
package edu.hpu.springcloud; import brave.sampler.Sampler; import cn.hutool.core.convert.Convert; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.NetUtil; import cn.hutool.core.util.NumberUtil; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import java.util.Scanner; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients public class ProductViewServiceFeignApplication { public static void main(String[] args) { int port = 0; int defaultPort = 8012; Future<Integer> future = ThreadUtil.execAsync(() ->{ int p = 0; System.out.println("请于5秒钟内输入端口号, 推荐 8012 、 8013 或者 8014,超过5秒将默认使用"+defaultPort); Scanner scanner = new Scanner(System.in); while(true) { String strPort = scanner.nextLine(); if(!NumberUtil.isInteger(strPort)) { System.err.println("只能是数字"); continue; } else { p = Convert.toInt(strPort); scanner.close(); break; } } return p; }); try{ port=future.get(5, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e){ port = defaultPort; } if(!NetUtil.isUsableLocalPort(port)) { System.err.printf("端口%d被占用了,无法启动%n", port ); System.exit(1); } new SpringApplicationBuilder(ProductViewServiceFeignApplication.class).properties("server.port=" + port).run(args); } @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
改造到这就完成了。
启动访问
依次启动eureka-server 、product-data-service和product-view-service。访问视图微服务:http://localhost:8012/products
访问链路追踪页面:http://localhost:9411/zipkin/dependency/
可以看到product-view-service-feign对product-data-service的依赖关系。
参考:
【1】、http://how2j.cn/k/springcloud/springcloud-zipkin/2052.html#nowhere
【2】、https://windmt.com/2018/04/24/spring-cloud-12-sleuth-zipkin/