spring cloud(七、链路追踪)

Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,zipkin为分布式链路调用监控系统,聚合各业务系统调用延迟数据,达到链路调用监控跟踪。

随着微服务数量不断增长,它们之间的关系会越来越复杂,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败,需要跟踪一个请求从一个微服务到下一个微服务的传播过程

分布式服务跟踪可以:

  1. 提供链路追踪,故障快速定位:可以通过调用链结合业务日志快速定位错误信息
  2. 可视化各个阶段耗时,进行性能分析
  3. 各个调用环节的可用性、梳理服务依赖关系以及优化
  4. 数据分析,优化链路:可以得到用户的行为路径,汇总分析应用在很多业务场景

案例主要有三个工程组成: 
一个server-zipkin,它的主要作用使用ZipkinServer 的功能,收集调用数据,并展示; 
一个service-hi,对外暴露hi接口; 
一个service-miya,对外暴露miya接口; 
这两个service可以相互调用;并且只有调用了,server-zipkin才会收集数据的 

一、server-zipkin

在spring Cloud为F版本的时候,已经不支持自己构建Zipkin Server了,下载jar启动即可,下载地址:

https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

https://pan.baidu.com/s/1w614Z8gJXHtqLUB6dKWOpQ 密码:26pf

下载完成jar 包之后,需要运行jar,如

java -jar zipkin-server-2.10.1-exec.jar

 访问http://localhost:9411

二、service-hi

引入依赖spring-cloud-starter-zipkin

配置文件:

server:
  port: 8988
spring:
  zipkin:
    base-url: http://localhost:9411
  application:
    name: service-hi

启动类:

@SpringBootApplication
@RestController
public class ServiceHiApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceHiApplication.class, args);
	}

    private static final Logger LOG = Logger.getLogger(ServiceHiApplication.class.getName());


    @Autowired
    private RestTemplate restTemplate;

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    @RequestMapping("/hi")
    public String callHome(){
        LOG.log(Level.INFO, "calling trace service-hi  ");
        return restTemplate.getForObject("http://localhost:8989/miya", String.class);
    }
    @RequestMapping("/info")
    public String info(){
        LOG.log(Level.INFO, "calling trace service-hi ");

        return "i'm service-hi";

    }

    @Bean
    public Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }
}

三、service-miya

引入依赖spring-cloud-starter-zipkin

配置文件:

server:
  port: 8989
spring:
  zipkin:
    base-url: http://localhost:9411
  application:
    name: service-miya

启动类:

@SpringBootApplication
@RestController
public class ServiceMiyaApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceMiyaApplication.class, args);
	}

	private static final Logger LOG = Logger.getLogger(ServiceMiyaApplication.class.getName());


	@RequestMapping("/hi")
	public String home(){
		LOG.log(Level.INFO, "hi is being called");
		return "hi i'm miya!";
	}

	@RequestMapping("/miya")
	public String info(){
		LOG.log(Level.INFO, "info is being called");
		return restTemplate.getForObject("http://localhost:8988/info",String.class);
	}

	@Autowired
	private RestTemplate restTemplate;

	@Bean
	public RestTemplate getRestTemplate(){
		return new RestTemplate();
	}


	@Bean
	public Sampler defaultSampler() {
		return Sampler.ALWAYS_SAMPLE;
	}
}

四、演示

访问http://localhost:9411/

 访问http://localhost:8989/miya

再打开http://localhost:9411,选择“依赖分析”

 点击其他按钮会出现调用情况的相关数据

源码:https://github.com/lrn-white/springcloud

猜你喜欢

转载自blog.csdn.net/qq_33283652/article/details/82783195