| 
 步骤 2 : 先运行,看到效果,再学习 步骤 3 : 模仿和排错 步骤 4 : 改造 步骤 5 : pom.xml 步骤 6 : 配置信息 步骤 7 : sampler 
					在前面的例子里,我们有两个微服务,分别是数据服务和视图服务,随着业务的增加,就会有越来越多的微服务存在,他们之间也会有更加复杂的调用关系。 这个调用关系,仅仅通过观察代码,会越来越难以识别,所以就需要通过 zipkin 服务链路追踪服务器 这个东西来用图片进行识别了。 
					老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。  1. 需要启动链路追踪服务器,这个启动办法是下载右上角的 zipkin-server-2.10.1-exec.jar, 然后用如下命令启动: java -jar zipkin-server-2.10.1-exec.jar 2. 挨个启动 eureka-server, 改造后的 product-data-service 和 product-view-service-feign 。 ( product-view-service-ribbon 后续不再使用,所以既没有被改造,也不用再启动了) 3. 访问一次 http://127.0.0.1:8012/products 通过 视图微服务去访问数据微服务,这样链路追踪服务器才知道有这事儿发生~ 4. 然后打开链路追踪服务器 http://localhost:9411/zipkin/dependency/ 就可以看到如图所示的 视图微服务调用数据微服务 的图形了。 
					在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 
 模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 这里提供了绿色安装和使用教程:diffmerge 下载和使用教程 
					eureka-server 不需要做改造。 product-data-service和product-view-service 需要进行改造以使其可以被追踪到。 
					两个 pom.xml 里都增加 zipkin 的jar包。 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> 
<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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cn.how2j.springcloud</groupId>
    <artifactId>springcloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>product-data-service</artifactId>
  
    <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>
 
<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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cn.how2j.springcloud</groupId>
    <artifactId>springcloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <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>
        </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>
 
					两个的配置文件都加上 spring: zipkin: base-url: http://localhost:9411 
#	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/
 
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; } package cn.how2j.springcloud;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
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 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;
@SpringBootApplication
@EnableEurekaClient
public class ProductDataServiceApplication {
    public static void main(String[] args) {
    	int port = 0;
    	int defaultPort = 8001;
    	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;
	}    
}
 
package cn.how2j.springcloud;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
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 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;
@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;
	}
	
}
 
				HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
			   
		提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢	
	 | |||||||||||