Nacos Architecture and Service Registration Mechanism in Spring Cloud
This article explains the Nacos architecture, including its provider, consumer, name server, and server components, and details how Spring Cloud integrates with Nacos for service registration, heartbeat monitoring, and dynamic service discovery using Open API, SDK, and Raft-based consistency protocols.
Nacos Architecture consists of Provider APP (service provider), Consumer APP (service consumer), Name Server (high‑availability routing via VIP or DNS), Nacos Server (exposes Open API, Config Service, Naming Service) and a Consistency Protocol based on the Raft algorithm.
The registration center works by having service instances register on startup and deregister on shutdown, while consumers query the registry for available instances and health‑check APIs verify service health.
In Spring Cloud, the org.springframework.cloud.client.serviceregistry.ServiceRegistry interface defines the registration contract; its implementation NacoServiceRegistry integrates Nacos. The auto‑configuration class AutoServiceRegistrationAutoConfiguration creates an AutoServiceRegistration bean, whose concrete class NacosAutoServiceRegistration extends AbstractAutoServiceRegistration and listens to WebServerInitializedEvent to trigger registration.
During registration, NacosServiceRegistry.register() calls the Nacos client SDK's namingService.registerInstance . The SDK ultimately invokes beatReactor.addBeatInfo() to create heartbeat information, and serverProxy.registerService() to complete the registration request.
The registration request is sent to the Nacos server endpoint nacos/v1/ns/instance , where the server parses serviceName and namespaceId , creates or retrieves a Service object stored in a ConcurrentHashMap , adds the instance, starts a scheduled heartbeat task, and synchronizes data via the consistency service.
Service address queries can be performed via Open API or SDK, both ultimately issuing HTTP requests to the same endpoint. The server’s InstanceController processes the request, builds a JSON response containing the list of provider instances, and returns it to the consumer.
Dynamic service discovery is achieved through the subscribe method. The client’s HostReactor runs an UpdateTask thread that pulls the latest address list every 10 seconds, while the server pushes updates when a provider’s heartbeat fails. The client parses the received JSON via processServiceJSON and updates its local cache.
Overall, Nacos client registers services via Open API or SDK, the server constructs service objects, maintains heartbeat health checks, and synchronizes data using Raft, while consumers discover services dynamically through subscription and periodic polling.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.