spring cloud 服务链路追踪

阅读数:72 评论数:0

跳转到新版页面

分类

python/Java

正文

简介

Spring cloud Sleuth主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持zipkin,你只需要在pom文件中引入相应的依赖即可。

1、span

基本工作单元,span在不断的启动和停止,同时记录了时间信息,当你创建一相span,你必须在未来的某个时刻停止它。

2、trace

一系列span组成的一个树状结构。

3、annotation

用来及时记录一个事件的存在。

(1)cs,client sent,

客户发起一个请求,描述了这个span的开始。

(2)sr,server received

服务端获得请求并准备开始处理它,sr-cs时间戳便可得到网络延迟。

(3)ss,server sent

请求返回客户端,ss-sr时间戳可得到服务端需要的处理请求时间。

(4)cr , client received

表示span的结束,客户端成功接收到服务端的回复。

实战

主要由三个工程组成:一个server-zipkin,它的主要作用是使用ZipKinServer的功能,收集数据,并展示,一个server-hi对外暴露hi接口,一个service-miya,对外暴露miya接口,这两个service可以相互调用。

1、构建server-zipkin

(1)下载jar包,https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

(2)执行

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

(3)访问localhost:9411

2、创建service-hi

(1)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">
	<modelVersion>4.0.0</modelVersion>
 
	<groupId>com.forezp</groupId>
	<artifactId>service-zipkin</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
 
	<name>service-hi</name>
	<description>Demo project for Spring Boot</description>
 
	<parent>
		<groupId>com.forezp</groupId>
		<artifactId>sc-f-chapter9</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
 
 
 
	<dependencies>
 
		<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>
 
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
 
</project>

(2)application.yml

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

(3)对外接口

import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.logging.Level;
import java.util.logging.Logger;
 
@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;
	}
 
 
}
 

3、创建service-miya

import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
import java.util.logging.Level;
import java.util.logging.Logger;
 
@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:8989/miya,查看http://localhost:9411/




相关推荐

ZuulProxyAutoConfiguration 首先我们看一下zuul的配置类ZuulProxyAutoConfiguration, 这个类有一项工作是初始化Zuul默认

Tomcat Tomcat的最大并发数是可以配置的,实际运用中,最大并发数与硬件性能有很大关系的。Tomcat默认的HTTP实现是采用阻塞式的Socket通信,每个请求都需要创

Eureka Server在运行期间会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85

mvn依赖 &lt;dependency&gt; &lt;groupId

什么是jwt (json web token)jwt是一生中用来在网络上声明某种身份的令牌(TOKEN),它的特点是紧凑且自包含并且基于JSON,通过一些常用的算法对包含的主体

Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图表化界面。 修改service-hi 1、在pom工程文件引入相应的依赖</

简介 在spring cloud中,有分布式配置中心组件spring cloud config,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程git仓库中,在该组

一、JAVA项目中网络接口调用工具 1、HttpClient 它是Apache Jakarta Common下的子项目,用来提供高效、最新的、功能丰富的支持Http协议的客户端编程工具包。 HttpC

在网络请求时,可能会出现异常请求,如果还想在异常情况下使系统可用,那么就需要容错处理。 Spring Cloud Feigh就是通过Fallback实现的,有两种方式: <

目标是屏蔽各种MQ的差异,统一编程模型。spring cloud stream 主要是使用binder和binding。 Binder表示绑定具体哪一个消息中间件,Binding包