一、环境准备
1、环境搭建,查看Spring Cloud专栏
2、(前提)准备一个服务提供方(sys-server)的接口和服务调用方(security-oauth2-server)接口,本文是在文章【Spring Cloud、OpenFeign】搭建Spirng Cloud项目(七):Spring Cloud使用OpenFeign调用其他服务中进行扩展。
二、功能介绍
参考文章:服务链路追踪(Spring Cloud Sleuth)
三、代码
背景:本文提供的是通过OpenFegin调用服务提供方请求实现该演示,自己可以通过RestTemplate直接调用服务提供方的请求实现该演示。
1、改造sys-server服务
- pom依赖(推荐直接放在父级pom中)
<!--包含了sleuth+zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
- application.yml中添加配置
spring:
zipkin:
base-url: http://localhost:7004
sleuth:
sampler:
# span的采样率,默认为 0.1
probability: 1
# 为了使用速率限制采样器,请设置spring.sleuth.sampler.rate属性以选择每秒间隔
rate: 1000
2、改造security-oauth2-server服务
- pom依赖(推荐直接放在父级pom中)
<!--包含了sleuth+zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
- application.yml中添加配置
spring:
zipkin:
en
base-url: http://localhost:7004
sleuth:
sampler:
# span的采样率,默认为 0.1
probability: 1
# 为了使用速率限制采样器,请设置spring.sleuth.sampler.rate属性以选择每秒间隔
rate: 1000
四、服务重启
- Postman调用服务调用方(security-oauth2-server)接口(客户端 -> 请求security-oauth2-server服务,security-oauth2-server服务 -> 请求sys-server服务)
- ZipKin界面展示