What is service link
Check the call relationship. Like the previous microservices, only two microservices are OK. If there are too many, it's hard to read the code. So, we introduced one thing -- distributed system call tracing. At present, the most widely used open source implementation is Twitter's zipkin. The zipkin service link tracing server can be identified with pictures.
Download and start link tracking server
1. In reference [1], it is a jar package. For more information, please refer to reference [2] for details.
2. Start the link tracking server, and use this command
java -jar zipkin-server-2.10.1-exec.jar
I don't know anything about wulala.
3. Website: http://localhost:9411/zipkin/dependency/
Renovation project
Transformation scope
Eureka server does not need to be modified.
Product data service and product view service need to be modified so that they can be tracked.
pom.xml
Add the jar package of zipkin
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> <!--Yes feign Mode support--> </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
Add the underlying path of zipkin to the configuration.
product-data-service
# server: # Port: since multiple product data services will be started, the port number is set automatically by the user. 800180028003 is recommended. 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
Startup class
Configure the Sampler Sampling Policy in the startup class: always sample indicates continuous sampling.
@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("Please enter the port number in 5 seconds, 8001, 8002 or 8003 are recommended, which will be used by default after 5 seconds " + defaultPort); Scanner scanner = new Scanner(System.in); while(true) { String strPort = scanner.nextLine(); if(!NumberUtil.isInteger(strPort)) { System.err.println("It can only be a number"); 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("port%d Occupied, unable to start%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("Please enter the port number in 5 seconds, 8012, 8013 or 8014 are recommended, which will be used by default after 5 seconds"+defaultPort); Scanner scanner = new Scanner(System.in); while(true) { String strPort = scanner.nextLine(); if(!NumberUtil.isInteger(strPort)) { System.err.println("It can only be a number"); 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("port%d Occupied, unable to start%n", port ); System.exit(1); } new SpringApplicationBuilder(ProductViewServiceFeignApplication.class).properties("server.port=" + port).run(args); } @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
The transformation is finished here.
Start access
Start Eureka server, product data service, and product view service. Access view microservices: http://localhost:8012/products
To access the link tracking page: http://localhost:9411/zipkin/dependency/
You can see the dependence of product view service feign on product data service.
Reference resources:
[1],http://how2j.cn/k/springcloud/springcloud-zipkin/2052.html#nowhere
[2],https://windmt.com/2018/04/24/spring-cloud-12-sleuth-zipkin/