Nacos Architecture and Service Registration Mechanism in Spring Cloud
This article explains Nacos's architecture, the principles of service registration and discovery, how Spring Cloud integrates Nacos through auto‑configuration and event listeners, and details the registration, heartbeat, and dynamic address update mechanisms used by Nacos servers and clients.
Nacos Architecture : Nacos consists of Provider APP (service provider), Consumer APP (service consumer), Name Server (provides 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), and the Nacos Console.
Registration Center Principle : Service instances register themselves on startup and deregister on shutdown; consumers query the registry to obtain available instances; the registry performs health checks via a dedicated API.
Spring Cloud Registration Timing : Spring Cloud defines the org.springframework.cloud.client.serviceregistry.ServiceRegistry interface; Nacos implements it with NacosServiceRegistry . Auto‑configuration is declared in META-INF/spring.factories via AutoServiceRegistrationAutoConfiguration , which injects an AutoServiceRegistration instance. The abstract class AbstractAutoServiceRegistration is extended by NacosAutoServiceRegistration . When the WebServerInitializedEvent fires, the bind(event) method triggers NacosServiceRegistry.register() to register the service.
NacosServiceRegistry Implementation : The registry method calls namingService.registerInstance from the Nacos client SDK. Heartbeat information is created with beatReactor.addBeatInfo() , and the actual registration is performed by serverProxy.registerService() .
Heartbeat Mechanism : The client schedules periodic heartbeat packets; a dedicated thread monitors server responses. If no response is received within the timeout, the instance is marked unhealthy.
Registration Process : 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 , adds the instance via addInstance , initializes heartbeat with service.init() , and registers a consistency listener via consistencyService.listen .
Service Provider Address Query : The Open API and SDK expose endpoints to list instances. InstanceController.list parses request parameters, obtains the corresponding Service , gathers provider addresses from srvIPs , and returns a JSON response.
Dynamic Service Address Perception : The client’s HostReactor runs an UpdateTask thread that pulls the latest address list every 10 seconds. If a provider fails, the server pushes a notification; the client processes the JSON via processServiceJSON to update its local cache.
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.