spring cloud zipkin ( Sleuth )服务链路追踪
spring cloud zipkin ( Sleuth )服务链路追踪
引用:微服务架构是一个分布式架构,微服务系统按业务划分服务单元, 一个微服务系统往往有 很多个服务单元。由于服务单元数量众多,业务的复杂性较高,如果出现了错误和异常,很难 去定位。主要体现在一个请求可能需要调用很多个服务,而内部服务的调用复杂性决定了问题 难以定位。所以在微服务架构中,必须实现分布式链路追踪, 去跟进一个请求到底有哪些服务 参与,参与的顺序又是怎样的,从而达到每个请求的步骤清晰可见,出了问题能够快速定位的目的
springcloud Greenwich.RELEASE取消了 zipkin依赖信息,推荐重官方下载Sleuth服务 ,官当地址为
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
但这里我们手动搭建Sleuth服务
Sleuth服务部分
1.pom 依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
2.application.properties
spring.application.name=cloud-zipkin-demo
server.port=8094
eureka.client.service-url.defaultZone=http://localhost:8080/eureka/
#解决The bean 'characterEncodingFilter', defined in class path resource [zipkin/autoconfigure/ui/ZipkinUiAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/HttpEncodingAutoConfiguration.class] and overriding is disabled.Action:
spring.main.allow-bean-definition-overriding=true
#解决IllegalArgumentException: Prometheus requires that all meters with the same name have the same set of tag keys.
management.metrics.web.server.auto-time-requests=false
3. 代码
@SpringBootApplication
@EnableEurekaClient
@EnableZipkinServer
public class SpringCloudZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudZipkinApplication.class, args);
}
}
客户端部分(服务提供者与服务消费者)
1.pom依赖
父pom
<dependencyManagement>
<dependencies>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-dependencies
</artifactId>
<version>
Greenwich.RELEASE
</version>
<type>
pom
</type>
<scope>
import
</scope>
</dependency>
</dependencies>
</dependencyManagement>
客户端pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
2.application.properties
#开启zipkin
spring.zipkin.enabled=true
#收集追踪信息的比率,如果是0.1则表示只记录10%的追踪数据,如果要全部追踪,设置为1(实际场景不推荐,因为会造成不小的性能消耗)
spring.sleuth.sampler.probability=1
spring.zipkin.base-url=http://localhost:8094