目录
全链路追踪—接入Spring Cloud Sleuth及与Zipkin配合使用
全链路追踪—接入Spring Cloud Sleuth及与Zipkin配合使用
接入Spring Cloud Sleuth
接入步骤
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
修改log4j2.xml
<RollingRandomAccessFile name="FILE-INFO" fileName="logs/house-info.log" filePattern="logs/house-info.%d{yyyy-MM-dd-HH}.log">
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%logger{50}:%L] [%X{X-B3-TraceId},%X{X-B3-SpanId}] - %msg%n" charset="UTF-8" />
<TimeBasedTriggeringPolicy interval="1" />
<DefaultRolloverStrategy max="1" >
<Delete basePath="logs" maxDepth="2">
<IfFileName glob="*house-info.*.log" />
<IfLastModified age="1h" />
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
修改各个服务的application.properties
spring.sleuth.sampler.percentage=1
spring.zipkin.baseUrl=http://localhost:9411
说明:每个服务都需要添加;
Zipkin Server的搭建
创建工程zipkinUi
pom.xml
<?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>
<parent>
<groupId>com.mooc.house</groupId>
<artifactId>house-monitor-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.mooc.house.zipkin</groupId>
<artifactId>zipkinUi</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>zipkinUi</name>
<description>zipkinUi</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.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>Camden.SR7</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
application.properties
server.port=9411
spring.application.name=zipkin
zipkin.storage.type=mem
ziplin启动类
package com.mooc.house.zipkin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.EnableZipkinServer;
@SpringBootApplication
@EnableZipkinServer
public class ZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinApplication.class, args);
}
}
说明:添加@EnableZipkinServer注解,启动Zipkin;