Graceful Service Deployment with Nacos Registry in Spring Boot
This article explains how to achieve graceful service publishing using Nacos as a registration center in a Spring Boot microservice architecture, covering environment setup, provider startup and shutdown, client-side load‑balancing with Ribbon, and configuration tweaks for both online and offline scenarios.
The article introduces Nacos as a service registry and demonstrates how to perform graceful deployment—ensuring a service can receive requests after registration and stop receiving them after deregistration.
Graceful Requirements
Two conditions must be met: graceful online (the consumer should not see the provider before it is fully initialized) and graceful offline (the consumer should stop receiving the provider after it is taken down).
Environment Setup
A local Nacos instance is started and its logs are observed to verify registration. Screenshots of the Nacos console are provided.
Provider Startup
Running the springboot‑provider application registers the service to Nacos. Sample log output shows the beat and registration information:
2023-06-11 18:58:10,120 [main] INFO com.alibaba.nacos.client.naming - [BEAT] adding beat: BeatInfo{port=8083, ip='192.168.31.94', ...}After registration, the service appears in the Nacos management UI.
Provider Shutdown
When the provider stops, Nacos logs indicate the client connection disconnects and the instance is removed:
2023-06-11 19:01:03,375 INFO Client connection 192.168.31.94:51885#true disconnect, remove instances and subscribersService Invocation
A Feign client in springboot‑consumer calls the provider via @FeignClient . The log shows Ribbon load‑balancing and client initialization details.
Ribbon Cache Refresh
Ribbon refreshes its local service list every 30 seconds, as shown in the following code snippet:
scheduledFuture = getRefreshExecutor().scheduleWithFixedDelay(wrapperRunnable, initialDelayMs, refreshIntervalMs, TimeUnit.MILLISECONDS); // refreshIntervalMs = 30sGraceful Publishing Implementation
Graceful Online
To avoid premature exposure, automatic registration is disabled:
spring.cloud.nacos.discovery.registerEnabled=falseAfter the service is fully initialized, manual registration is performed via the Nacos SDK:
Properties setting = new Properties();
setting.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
NamingService naming = NacosFactory.createNamingService(setting);
naming.registerInstance("springboot-provider", "192.168.31.94", 8083);Graceful Offline
For normal shutdown, an API endpoint can be added to deregister the instance before the process exits:
@GetMapping("/nacos/deregisterInstance")
public String deregisterInstance() {
Properties prop = new Properties();
prop.setProperty("serverAddr", "localhost");
NacosNamingService client = new NacosNamingService(prop);
client.deregisterInstance("springboot-provider", "192.168.31.94", 8083);
return "success";
}After deregistration, wait for the Ribbon cache (default 30 s) to refresh before terminating.
Handling Service Failures
If a provider crashes, Nacos relies on heartbeat detection. The default heartbeat interval is 5 s and the timeout is 15 s; the instance becomes unhealthy after the timeout and is removed after another 30 s. These values can be tuned:
spring.cloud.nacos.discovery.metadata.preserved.heart.beat.interval=1000
spring.cloud.nacos.discovery.metadata.preserved.heart.beat.timeout=3000
spring.cloud.nacos.discovery.metadata.preserved.ip.delete.timeout=5000Even with tuning, a sudden failure may still be visible to consumers.
Conclusion
Regardless of the registry used, graceful deployment requires handling both online and offline phases. This guide details how Nacos achieves that and provides practical code snippets and configuration tips for Spring Boot microservices.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.