Spring Cloud Zipkin
Zipkim是一个可以采集并且跟踪分布式系统中请求数据的组件,让开发者可以更加
直观的监控到请求在各个微服务所耗费的时间等,Zipkin:Zipkin Server、Zipkin Client,
**创建Zipkin server**
创建一个maven工程,pom.xml
```yaml
<dependencies>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>2.9.4</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>2.9.4</version>
</dependency>
</dependencies>
```
创建配置文件 application.yml
```yaml
server:
port: 9090
```
创建启动类
```java
package com.shuang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.internal.EnableZipkinServer;
@SpringBootApplication
@EnableZipkinServer
public class ZipKinApplication {
public static void main(String[] args) {
SpringApplication.run(ZipKinApplication.class,args);
}
}
```
注解说明:
@EnableZipkinServer:声明启动Zipkin Server
**创建 Zipkin Client**
创建maven工程,pom.xml
```yaml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
```
创建配置文件,application.yml
```yaml
server:
port: 8090
spring:
application:
name: zipkinclient
sleuth:
web:
client:
enabled: true
sampler:
probability: 1.0
zipkin:
base-url: http://localhost:9090/
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
```
属性说明:
spring.sleuth.web.client.enabled:设置开启请求跟踪
spring.sleuth.sampler.probability:设置采样比例,默认值是1.0。
spring.zipkin.base-url:Zipkin Server地址。
创建启动类
```java
package com.shuang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ZipkinClientApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinClientApplication.class,args);
}
}
```
Handler
```java
package com.shuang.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/zipkin")
public class ZipkinHandler {
@Value("${server.port}")
public String port;
@GetMapping("/index")
public String index(){
return this.port;
}
}
```
依次启动:
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/2020071806370183.png)
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200718063715941.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200718063728788.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)