Spring Cloud Sleuth 整合 Zipkin
Spring Cloud Sleuth 是分布式服务跟踪,Zipkin可以帮助收集时间数据,解决在microservice架构下的延迟问题。
<?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.example</groupId>
<artifactId>server-zipkin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>server-zipkin</name>
<description>Demo project for Spring Cloud</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
添加 @EnableZipkinServer
@EnableZipkinServer
@EnableEurekaClient
@SpringBootApplication
public class ServerZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(ServerZipkinApplication.class, args);
}
}
zipkin server 默认将跟踪信息存储在内存中,每次重启都会丢失之前的跟踪信息,可以修改为mysql存储
application.yml
# 修改为mysql存储:
# 1.添加依赖 <dependency>
# <groupId>org.springframework.boot</groupId>
# <artifactId>spring-boot-starter-jdbc</artifactId>
# </dependency>
# <dependency>
# <groupId>mysql</groupId>
# <artifactId>mysql-connector-java</artifactId>
# </dependency>
# <dependency>
# <groupId>io.zipkin.java</groupId>
# <artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
# </dependency>
#
# 2.配置 spring.datasource
# 3.配置 zipkin.storage.type=mysql
spring:
datasource:
# zipkin-autoconfigure-storage-mysql 包下有 mysql.sql 脚本,程序启动时会自动根据sql脚本创建表结构
schema: classpath:/mysql.sql
url: jdbc:mysql://localhost:3306/qinwei?useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
initialize: true
application.properties
server.port=8090
spring.application.name=server-zipkin
eureka.client.service-url.defaultZone=http://localhost:8081/eureka-server/eureka/
management.security.enabled=false
#将zipkin server 存储切换为 mysql
zipkin.storage.type=mysql
在需要跟踪的服务中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
在配置文件中指定Zipkin Server的地址
application.properties
spring.zipkin.base-url=http://localhost:8090
创建 bean
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
/** * sleuth 默认使用 PercentageBasedSampler 实现的抽样策略,以请求百分比的方式配置和收集跟踪信息. * 默认是0.1(表示收集10%的跟踪信息),可以配置spring.sleuth.sampler.percentage来修改. * <p> * 创建 AlwaysSampler 的bean,会覆盖默认的 PercentageBasedSampler 策略,收集全部的跟踪信息 */
@Bean
public AlwaysSampler defaultSampler() {
return new AlwaysSampler();
}
还没有评论,来说两句吧...