Spring Cloud Commons Guide: Discovery, Load Balancing & Custom Features
This article explains how Spring Cloud Commons abstracts service discovery, load balancing, and circuit breaker patterns, covering annotations like @EnableDiscoveryClient, SimpleDiscoveryClient configuration, service registration, RestTemplate and WebClient load balancing, network interface filtering, HTTP client factories, and creating custom features for Spring Cloud applications.
1. Introduction
Spring Cloud Commons provides a common abstraction layer for patterns such as service discovery, load balancing, and circuit breakers, allowing all Spring Cloud clients to use these abstractions independent of the underlying implementation (e.g., Eureka or Nacos).
2. Common Services
2.1 @EnableDiscoveryClient Annotation
The annotation is no longer required; you can register a DiscoveryClient bean directly. If used, it looks for the key
org.springframework.cloud.client.discovery.EnableDiscoveryClientin META-INF/spring.factories to register the bean.
SimpleDiscoveryClient
If no DiscoveryClient implementation is present on the classpath, Spring Cloud Commons falls back to SimpleDiscoveryClient, which obtains service and instance information from properties. Example configuration:
spring:
cloud:
discovery:
client:
simple:
instances:
order-service-ack:
- service-id: OrderService
instance-id: OrderService001
host: localhost
port: 9000
- service-id: OrderService
instance-id: OrderService002
host: localhost
port: 9001SimpleDiscoveryClient will use the above configuration as instance information.
2.2 Service Registration
Commons now provides a ServiceRegistry interface with methods like register(Registration) and deregister(Registration) for custom service registration. Example:
@Configuration
@EnableDiscoveryClient(autoRegister=false)
public class PackConfig {
private final ServiceRegistry registry;
public PackConfig(ServiceRegistry registry) {
this.registry = registry;
}
public void register() {
// Registration extends ServiceInstance
Registration registration = // service instance information;
this.registry.register(registration);
}
}Service Auto‑Registration
By default, ServiceRegistry implementations automatically register the running service. To disable, set @EnableDiscoveryClient(autoRegister=false) or configure
spring.cloud.service-registry.auto-registration.enabled=false.
Service Registration Events
When a service is auto‑registered, the InstancePreRegisteredEvent is triggered before registration, and InstanceRegisteredEvent after registration. These events can be listened to.
2.3 RestTemplate Load Balancing
Configure a RestTemplate to use a load‑balancer client by defining a bean with the @LoadBalanced qualifier:
@Configuration
public class PackConfig {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Service
public class UserService {
private final RestTemplate restTemplate;
public UserService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String query() {
String results = restTemplate.getForObject("http://order-service/orders/666", String.class);
return results;
}
}Here order-service is the remote service name used for load balancing.
2.4 WebClient Load Balancing
Similarly, a WebClient can be configured with @LoadBalanced:
@Configuration
public class PackConfig {
@Bean
@LoadBalanced
public WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder();
}
}
@Service
public class UserService {
private final WebClient.Builder webClientBuilder;
public UserService(WebClient.Builder webClientBuilder) {
this.webClientBuilder = webClientBuilder;
}
public Mono<String> doOtherStuff() {
return webClientBuilder.build()
.get()
.uri("http://order-service/orders/666")
.retrieve()
.bodyToMono(String.class);
}
}2.5 Multiple RestTemplate Configurations
To have both a load‑balanced and a non‑balanced RestTemplate, define two beans, marking one as @Primary:
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
RestTemplate loadBalanced() {
return new RestTemplate();
}
@Primary
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
public class PackComponent {
@Resource
private RestTemplate restTemplate;
@Resource
@LoadBalanced
private RestTemplate loadBalanced;
public String createOrder() {
return loadBalanced.postForObject("http://order-service/orders/create", String.class);
}
public String query() {
return restTemplate.getForObject("http://192.168.1.2/storage/666", String.class);
}
}The same approach applies to multiple WebClient beans.
2.6 Ignoring Network Interfaces
Sometimes certain network interfaces should be excluded from service discovery registration (e.g., Docker containers). Configure a list of regular expressions to ignore interfaces, for example:
spring:
cloud:
inetutils:
ignoredInterfaces:
- docker0
- veth.*You can also force specific network addresses:
spring:
cloud:
inetutils:
preferredNetworks:
- 192.168
- 10.0Or restrict to site‑local addresses only:
spring:
cloud:
inetutils:
useOnlySiteLocalInterfaces: true2.7 HTTP Client Factories
Spring Cloud Commons provides beans for creating Apache HTTP client ( ApacheHttpClientFactory) and OK HTTP client ( OkHttpClientFactory) factories. The OK HTTP factory is created only if the OK HTTP jar is on the classpath. Connection manager beans are also provided for both clients. You can disable these factories by setting spring.cloud.httpclientfactories.apache.enabled or spring.cloud.httpclientfactories.ok.enabled to false.
2.8 Custom Features
You can develop a custom feature packaged as a JAR with a manifest like:
Manifest-Version: 1.0
Implementation-Title: Spring Cloud Commons RPC
Implementation-Version: 1.0.1
Build-Jdk-Spec: 17
Created-By: Pack
Implementation-Vendor: Pack Software, Inc.Spring Cloud can read this information. Define a bean to expose the feature:
@Bean
public HasFeatures rpcFeatures() {
return HasFeatures.namedFeatures(new NamedFeature("RPC", PackRpcService.class));
}These custom features can be viewed via the Actuator endpoint.
The article concludes with the hope that the content is helpful.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.
