如何在Zuul网关/代理中部署Zipkin?附配置说明
Hey there! Let me break down exactly how to set up Zipkin with your Zuul gateway/proxy—since you already know Zipkin’s perfect for tracing requests and tracking latency in microservice projects, let’s get straight to the steps.
1. Spin Up the Zipkin Server First
Before configuring Zuul, you need a Zipkin server to collect and visualize all tracing data. The quickest way to launch one is using the official quickstart jar:
curl -sSL https://zipkin.io/quickstart.sh | bash -s java -jar zipkin.jar
By default, this will run Zipkin at http://localhost:9411—you can visit this URL later to check the tracing UI.
2. Add Zipkin Dependency to Your Zuul Project
Next, add the Zipkin starter dependency to your Zuul project's build file. Here's how for both Gradle and Maven:
Gradle
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zipkin', version: '1.3.2.RELEASE'
Maven
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> <version>1.3.2.RELEASE</version> </dependency>
This dependency includes both Spring Sleuth (which generates trace IDs) and the Zipkin client components, so you don't need to add any extra packages.
3. Configure the Sampler Bean
To control what percentage of requests get traced, define a Sampler bean in your Zuul configuration class. Here's the expanded code example for clarity:
import org.springframework.cloud.sleuth.sampler.Sampler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.beans.factory.annotation.Value; @Configuration public class SleuthTracingConfig { @Value("${spring.sleuth.sampler.percentage}") private double samplingRate; @Bean public Sampler requestSampler() { // Returns the sampling rate we've configured return () -> samplingRate; } }
Then set the sampling rate in your application.properties or application.yml:
application.properties
# Trace 100% of requests (great for testing; lower in production!) spring.sleuth.sampler.percentage=1.0
application.yml
spring: sleuth: sampler: percentage: 1.0
Pro tip: For production environments, drop this to a lower value like 0.1 (10% of requests) to avoid overload from tracing data.
4. Point Zuul to Your Zipkin Server (If Not Default)
If your Zipkin server isn't running on the default localhost:9411, add this configuration to tell Zuul where to send tracing data:
application.properties
spring.zipkin.base-url=http://your-zipkin-server-address:9411
application.yml
spring: zipkin: base-url: http://your-zipkin-server-address:9411
5. Test the Setup
Start your Zuul gateway, send a few requests through it, then head to the Zipkin UI (http://localhost:9411). You’ll see full traces of your requests, including how long Zuul took to route them, and any downstream service calls tied to the request.
Quick Notes
- Make sure your other microservices also have Zipkin dependencies and sampling configured if you want to see the complete call chain.
- The
1.3.2.RELEASEversion is a bit older—if you’re using a newer Spring Cloud release (like Greenwich or later), use a compatible Zipkin version to avoid conflicts.
内容的提问来源于stack exchange,提问作者RITESH GUPTA

