How to Set Up Distributed Tracing with Spring Cloud, Nacos, and Zipkin
This guide walks through configuring Spring Cloud microservices with Nacos service discovery, adding Zipkin for distributed tracing, and persisting trace data to Elasticsearch, including all necessary Maven dependencies, YAML settings, controller code, and verification steps.
Environment
spring cloud Hoxton.SR11 + spring cloud alibaba 2.2.5.RELEASE + zipkin 2.23.2 + nacos 1.4.1 + Elasticsearch 7.8.0 + Kibana 7.8.0
service‑producer module
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies> server:
port: 9100
---
spring:
cloud:
nacos:
username: dev
password: 123456
discovery:
enabled: true
serverAddr: localhost:8848
namespace: 7205e69c-ac51-4ac1-bbe9-87c28689b88b
---
spring:
application:
name: service-producer
---
# Zipkin service address
spring:
zipkin:
base-url: http://127.0.0.1:9411
sleuth:
sampler:
probability: 1.0API
@RestController
@RequestMapping("discovery")
public class DiscoveryController {
@NacosInjected
private NamingService namingService;
@Resource
private DiscoveryClient discoverClient;
@GetMapping(value = "/get")
public Object get(@RequestParam String serviceName) throws Exception {
Map<String, Object> res = new HashMap<>();
res.put("services", discoverClient.getServices());
res.put("instances", discoverClient.getInstances(serviceName));
res.put("port", 9000);
return res;
}
}service‑consumer module
Dependencies and configuration are the same as service‑producer.
Configure RestTemplate
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}API
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@GetMapping("/get")
public Object invoke(String serviceName) {
return restTemplate.getForObject(
"http://service-producer/discovery/get?serviceName=" + serviceName,
Object.class);
}
}service‑gateway (Zuul) module
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies> server:
port: 10001
---
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000
OkToRetryOnAllOperations: false
MaxAutoRetries: 1
MaxAutoRetriesNextServer: 1
---
zuul:
ignoredServices: '*'
prefix: /api
stripPrefix: true
routes:
service-producer:
path: /api-a/**
serviceId: service-consumer
customSensitiveHeaders: true
sensitiveHeaders:
addHostHeader: true
host:
connectTimeoutMillis: 20000
socketTimeoutMillis: 20000
ignoreLocalService: true
---
spring:
zipkin:
base-url: http://127.0.0.1:9411
sleuth:
sampler:
probability: 1.0
---
spring:
application:
name: service-gateway
---
spring:
cloud:
nacos:
username: dev
password: 123456
discovery:
enabled: true
serverAddr: localhost:8848
namespace: 7205e69c-ac51-4ac1-bbe9-87c28689b88bZipkin service
For Spring Boot 2.x and later you can run Zipkin without building your own server; download and start it as follows:
java -jar zipkin.jar --STORAGE_TYPE=elasticsearch --ES_HOSTS=localhost:9200After restarting the three services, access the API endpoints and view the trace data in Zipkin UI and Kibana.
Persisting trace logs
By default Zipkin stores traces in memory; to persist them you can use memory, MySQL, Cassandra, or Elasticsearch. This guide uses Elasticsearch for its simplicity.
Run Zipkin with Elasticsearch storage:
java -jar zipkin.jar --STORAGE_TYPE=elasticsearch --ES_HOSTS=localhost:9200After restarting the services and invoking the APIs, Kibana will automatically create an index and display the trace logs.
At this point, distributed tracing for the microservice system is fully configured.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
