Understanding Nacos Architecture and Service Registration in Spring Cloud
This article explains Nacos's core components, the principles of service registration and health checking, how Spring Cloud integrates Nacos via ServiceRegistry and AutoServiceRegistration, the implementation details of NacosServiceRegistry, heartbeat mechanisms, and dynamic service address discovery using HostReactor.
Nacos Architecture
The Nacos ecosystem consists of several key parts:
Provider APP : service provider
Consumer APP : service consumer
Name Server : routes requests using VIP or DNS for high‑availability clustering
Nacos Server : provides Open API, Config Service, Naming Service, and a Raft‑based consistency protocol for data synchronization
Nacos Console : management UI
Registration Center Principles
When a service instance starts, it registers itself with the registry and deregisters on shutdown. Consumers query the registry to obtain healthy instances, and the registry periodically calls the instance's health‑check API to verify availability.
Spring Cloud Registration Timing
Spring Cloud defines the org.springframework.cloud.client.serviceregistry.ServiceRegistry interface as the standard for service registration. Nacos integration implements this interface via NacosServiceRegistry . The auto‑configuration class AutoServiceRegistrationAutoConfiguration creates an AutoServiceRegistration bean, which is an instance of AbstractAutoServiceRegistration . The concrete class NacosAutoServiceRegistration extends this abstract class.
During the WebServerInitializedEvent , AbstractAutoServiceRegistration invokes bind(event) , which ultimately calls NacosServiceRegistry.register() to register the service.
NacosServiceRegistry Implementation
The registry method calls namingService.registerInstance from the Nacos client SDK to complete registration.
Heartbeat Mechanism
The client creates heartbeat information via beatReactor.addBeatInfo() and sends it using serverProxy.registerService() . If the server does not receive a heartbeat within the configured timeout, the instance is marked unhealthy.
Registration Principle (Open API & SDK)
Both Open API and SDK ultimately send an HTTP request to nacos/v1/ns/instance . The server-side InstanceController extracts serviceName and namespaceId , creates or retrieves a Service object stored in a ConcurrentHashMap , and adds the instance via addInstance . The service is then cached with putService() , and a heartbeat task is scheduled.
Service Provider Address Query
The InstanceController.list method parses request parameters, retrieves the corresponding Service from the cache, extracts provider IPs, assembles a JSON response, and returns it to the consumer.
Dynamic Service Address Detection
Clients subscribe to service updates. HostReactor runs an UpdateTask thread that pulls the latest address list every 10 seconds. If a provider fails its heartbeat, the server pushes a notification, and the consumer updates its local cache via processServiceJSON .
In summary, Nacos registers services through Open API or SDK, stores them in an in‑memory map, maintains health via periodic heartbeats, synchronizes data using a Raft‑based consistency protocol, and provides dynamic address updates to clients through a pull‑push mechanism.
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.
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.