How to Consume Services with Spring Cloud’s LoadBalancerClient
This guide shows how to build a Spring Cloud consumer that registers with Eureka, configures LoadBalancerClient and RestTemplate, and calls a provider service’s endpoint using load‑balanced discovery, complete with Maven dependencies, configuration files, and full Java code examples.
Overview
The article demonstrates how to use Spring Cloud's LoadBalancerClient to consume a service registered in Eureka (or Consul). It builds a consumer project, adds required dependencies, configures the application, and implements a REST controller that selects a service instance via load balancing and invokes its endpoint.
1. Create the Consumer Project
Generate a new Maven project named eureka-consumer and add the following dependencies to pom.xml (parent and dependencyManagement sections omitted for brevity):
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>2. Application Configuration
In src/main/resources/application.properties specify the consumer’s name, port, and the Eureka server address:
spring.application.name=eureka-consumer
server.port=2101
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/3. Main Application Class
Enable discovery and expose a RestTemplate bean:
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}4. Consumer Controller
Create a REST controller that injects LoadBalancerClient and RestTemplate. The /consumer endpoint selects an instance of the provider service ( eureka-client), builds the target URL, and forwards the request:
@RestController
public class DcController {
@Autowired
LoadBalancerClient loadBalancerClient;
@Autowired
RestTemplate restTemplate;
@GetMapping("/consumer")
public String dc() {
ServiceInstance instance = loadBalancerClient.choose("eureka-client");
String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/dc";
System.out.println(url);
return restTemplate.getForObject(url, String.class);
}
}5. How It Works
The LoadBalancerClient.choose() method queries the Eureka registry, applies a load‑balancing algorithm, and returns a ServiceInstance containing host and port information. The consumer then constructs the full URL for the provider’s /dc endpoint and uses RestTemplate.getForObject to retrieve the response.
6. Running the Example
Start the Eureka server, the provider service ( eureka-client), and this consumer project. Access http://localhost:2101/consumer in a browser or via curl; the request will be routed to a healthy provider instance and the response from /dc will be returned.
For a Consul‑based version, refer to the corresponding consul-client and consul-consumer repositories.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
