Integrating Nacos, Ribbon, OpenFeign, and Spring Cloud LoadBalancer for Service Registration and Load Balancing
This article explains how Nacos, Ribbon, OpenFeign and Spring Cloud's loadbalancer work together to provide service registration, discovery and client‑side load balancing, detailing the required Spring Cloud interfaces and showing practical code examples.
The author answers a question about integrating a registry with Spring Cloud to achieve load balancing, and proceeds to explain the coordination principles of Nacos, OpenFeign, Ribbon and the Spring Cloud loadbalancer component.
Nacos is both a service registry and a configuration center. By adding the nacos-client dependency and using the provided SDK, services can register themselves and discover others. Example Maven dependency and Java registration code are shown:
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.4.4</version>
</dependency> Properties properties = new Properties();
properties.setProperty("serverAddr", "localhost");
properties.setProperty("namespace", "8848");
NamingService naming = NamingFactory.createNamingService(properties);
// Register an instance of the "order" service
naming.registerInstance("order", "192.168.2.100", 8080);
// Discover all instances of the "order" service
List<Instance> instanceList = naming.selectInstances("order", true);When a service registers, Nacos stores the information in a service registry collection.
Automatic service registration in Spring Cloud relies on three core interfaces, often called the "three swords":
Registration
The Registration interface extends ServiceInstance and encapsulates the current service's IP, port and metadata. Nacos provides an implementation called NacosRegistration .
ServiceRegistry
The ServiceRegistry interface defines a register method that persists a Registration into the registry. Nacos implements this with NacosServiceRegistry , whose code mirrors the simple demo shown above.
AutoServiceRegistration
AutoServiceRegistration is a marker interface; its abstract class AbstractAutoServiceRegistration listens for WebServerInitializedEvent (triggered after the embedded web server starts). When the event fires, the listener invokes ServiceRegistry.register , completing automatic registration. Nacos supplies NacosAutoServiceRegistration that extends this abstract class.
These three interfaces allow any registry that implements them to be plugged into Spring Cloud without changing application code.
Ribbon is a client‑side load‑balancing component. It obtains service instances via the ServerList interface. Nacos implements ServerList (as NacosServerList ), enabling Ribbon to fetch instance data from the Nacos registry. Other registries such as Eureka or Zookeeper provide similar adapters.
OpenFeign is an RPC framework that creates a dynamic proxy for a declared interface. When a method is invoked, Feign builds a URL of the form http://service-name/path . Because the actual IP and port are unknown, Feign delegates to Ribbon (or the newer loadbalancer ) to resolve a concrete instance.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>The Spring Cloud loadbalancer component uses DiscoveryClient to obtain instances, so a registry only needs to implement DiscoveryClient to be compatible.
In summary, Nacos, Ribbon, OpenFeign and Spring Cloud's loadbalancer cooperate by exposing extension points (interfaces) that third‑party registries implement, allowing seamless replacement of components without modifying business logic.
The author concludes with a request for likes, follows, shares, and mentions a paid "knowledge planet" community offering additional Spring‑related content.
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.