Backend Development 10 min read

Understanding Nacos Architecture and Service Registration in Spring Cloud

This article explains the Nacos architecture, the principles of service registration, how Spring Cloud integrates with Nacos for automatic registration, the implementation details of NacosServiceRegistry, heartbeat mechanisms, service address queries, and dynamic service discovery, providing a comprehensive guide for backend developers working with microservices.

Top Architect
Top Architect
Top Architect
Understanding Nacos Architecture and Service Registration in Spring Cloud

Nacos Architecture

Nacos consists of Provider APP (service provider), Consumer APP (service consumer), Name Server (high‑availability routing via VIP or DNS), Nacos Server (provides Open API, Config Service, Naming Service, and a Raft‑based consistency protocol), and Nacos Console.

Registration Center Principles

Service instances register to the registry on startup and deregister on shutdown.

Consumers query the registry to obtain available instances.

The registry performs health‑check API calls to verify instance availability.

Spring Cloud Registration Timing

Spring‑Cloud‑Common defines the interface org.springframework.cloud.client.serviceregistry.ServiceRegistry , implemented by NacosServiceRegistry . The auto‑configuration class AutoServiceRegistrationAutoConfiguration creates an AutoServiceRegistration bean, whose concrete class NacosAutoServiceRegistration extends AbstractAutoServiceRegistration . When the web server is initialized, onApplicationEvent triggers bind(event) , ultimately calling NacosServiceRegistry.register() .

NacosServiceRegistry Implementation

The registry method invokes namingService.registerInstance from the Nacos client SDK. Registration uses HTTP requests to nacos/v1/ns/instance , handled by InstanceController on the server side, which creates or updates a Service object stored in a ConcurrentHashMap and starts a heartbeat task.

Heartbeat Mechanism

Clients create heartbeat info via beatReactor.addBeatInfo() and send periodic heartbeats using serverProxy.registerService() . The server updates instance health status based on received heartbeats; missing heartbeats mark the instance as unhealthy.

Service Provider Address Query

Both Open API and SDK can query service addresses. The InstanceController.list method parses request parameters, retrieves the Service by namespaceId and serviceName , and returns a JSON list of provider instances.

Dynamic Service Address Perception

Clients subscribe to service updates via subscribe or selectInstance with subscribe=true . The HostReactor runs an UpdateTask thread that pulls the latest address list every 10 seconds, while the server pushes updates when a provider fails, and the client processes the JSON to refresh its local cache.

Overall, Nacos provides both SDK and Open API mechanisms for service registration, health checking, address querying, and dynamic discovery, forming a core component of backend microservice architectures.

service discoveryNacosSpring CloudheartbeatRegistration
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.