一、简介
Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,为微服务提供链路跟踪的机制,可以监控微服务之间的调用。
二、搭建zipkin服务监控端
zikpin服务jar包下载地址_____https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
# 下载jar包,执行命令运行即可
java -jar zipkin-server-2.12.9-exec.jar
启动成功之后,即可访问zikpin客户端地址____http://127.0.0.1:9411
三、搭建zipkin的服务提供端
<?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">
<parent>
<artifactId>mo-cloud</artifactId>
<groupId>com.mo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>zipkin-hello</artifactId>
<description>zipkin的服务提供者</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- eureka服务提供者的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- feign依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 链路追踪的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>
</project>
server:
# springboot设置随机端口号
port: 0
eureka:
client:
service-url:
default-zone: http://127.0.0.1:8761/eureka/
instance:
# 重写服务的唯一Id,防止多实例对被注册成一个服务
instance-id: ${spring.application.name}:${random.int}
# 设置优先使用ip进行注册
prefer-ip-address: true
spring:
application:
name: zipkin-hello
# 配置链路追踪服务端的地址
zipkin:
base-url: http://127.0.0.1:9411
feign:
hystrix:
enabled: true
package com.mo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* zipkin 服务提供者,区别如下
* 1. 添加链路追踪的依赖 spring-cloud-starter-zipkin
* 2. 配置文件配置链路追踪服务端的地址
* 即可完成服务的提供,并且支持zipkin服务监控端进行监控
*
* @author x.pan
* @email px5215201314@163.com
* @date 2020/4/27 23:50
*/
@RestController
@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
public class ZipkinHelloApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinHelloApplication.class, args);
}
/**
* 对外提供的服务,用于其他服务的消费
*
* @return
*/
@GetMapping("/hello")
public String hello() {
return "zipkin,hello";
}
}
四、搭建zipkin的服务消费端
<?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">
<parent>
<artifactId>mo-cloud</artifactId>
<groupId>com.mo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>zipkin-hi</artifactId>
<description>zipkin链路追踪服务消费端</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- eureka服务提供者的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- feign依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 链路追踪的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>
</project>
server:
# springboot设置随机端口号
port: 0
eureka:
client:
service-url:
default-zone: http://127.0.0.1:8761/eureka/
instance:
# 重写服务的唯一Id,防止多实例对被注册成一个服务
instance-id: ${spring.application.name}:${random.int}
# 设置优先使用ip进行注册
prefer-ip-address: true
spring:
application:
name: zipkin-hi
# 配置链路追踪服务端的地址
zipkin:
base-url: http://127.0.0.1:9411
feign:
hystrix:
enabled: true
package com.mo.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author x.pan
* @email px5215201314@163.com
* @date 2020/4/27 23:57
*/
@FeignClient(value = "ZIPKIN-HELLO")
public interface ServiceHelloFeign {
/**
* feign调用ZIPKIN-HELLO服务的hello接口
*
* @return
*/
@GetMapping("/hello")
String hello();
}
package com.mo;
import com.mo.feign.ServiceHelloFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* zipkin服务消费端与提供者操作一致
* 1. 添加链路追踪的依赖 spring-cloud-starter-zipkin
* 2. 配置文件配置链路追踪服务端的地址
* 即可完成服务的消费,并且支持zipkin服务监控端进行监控
*
* @author x.pan
* @email px5215201314@163.com
* @date 2020/4/27 23:50
*/
@RestController
@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
public class ZipkinHiApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinHiApplication.class, args);
}
@Autowired
private ServiceHelloFeign serviceHelloFeign;
/**
* feign调用远程服务,进行链路追踪的测试
*
* @return
*/
@GetMapping("/hi")
public String hi() {
return serviceHelloFeign.hello();
}
}
五、测试