The @LoadBalanced annotation marks a RestTemplate, RestClient.Builder or WebClient.Builder bean so that Spring Cloud adds a load-balancing interceptor to it. When you call a URL such as http://inventory-service/items, the interceptor treats inventory-service as a logical service ID, asks the discovery client for its instances, picks one, and sends the request there.
The annotation is client-side load balancing: the caller chooses the instance, not a separate proxy. Since Spring Cloud 2020.0 the implementation is Spring Cloud LoadBalancer. Netflix Ribbon was removed and should not be used in new code.
@LoadBalanced is a qualifier annotation in org.springframework.cloud.client.loadbalancer. Spring Cloud collects every bean annotated with it and customizes it with a load-balancer interceptor. Without it, a RestTemplate treats inventory-service as an ordinary host name and DNS resolution fails.
With it, the request flow is:
http://inventory-service/items.ServiceInstanceListSupplier returns the available instances from the service registry (Eureka, Consul, Kubernetes and others).Add the Spring Cloud LoadBalancer starter and a discovery client. The starter is also pulled in transitively by most discovery starters. Use the Spring Cloud release train that matches your Spring Boot version.
Declare the bean once, annotate it, and call services by name.
Every call made through this bean is load balanced. If you also need a plain RestTemplate for external URLs, create a second bean without the annotation and inject it by name or with your own qualifier.
RestTemplate still works, but Spring documents it as being in maintenance mode, and RestClient (Spring Framework 6.1 and later) is the recommended synchronous client. For reactive code use WebClient. In both cases the annotation goes on the builder bean, not on the client.
Inject the builder and call build() in your component. Support for RestClient.Builder requires a recent Spring Cloud release, so check the release notes of your version.
Spring Cloud LoadBalancer uses round robin by default, cycling through the instances in order. It also ships a random implementation. You can pick another one per service with @LoadBalancerClient.
Keep such configuration classes out of component scanning, otherwise they apply to every service. Useful properties include health-check based filtering and caching of instance lists under spring.cloud.loadbalancer.*.
Load balancing only picks an instance; it does not make calls resilient. If the chosen instance is down, the call fails unless you add retry or a circuit breaker. Combine it with Spring Retry or Resilience4j, and enable spring.cloud.loadbalancer.retry.enabled where it suits your idempotency rules. Do not retry non-idempotent calls such as payments blindly.
Yes. You can supply a static list of instances with spring.cloud.loadbalancer.configurations and spring.cloud.discovery.client.simple.instances.*, which is useful in tests. In production you normally use Eureka, Consul or Kubernetes discovery.
No. Ribbon entered maintenance mode and was removed from Spring Cloud in the 2020.0 release. Spring Cloud LoadBalancer replaced it.
The RestTemplate you injected is not the annotated bean, or no discovery client is on the classpath. Check that the bean has @LoadBalanced and that the service is registered under that exact ID.
OpenFeign clients are load balanced automatically through Spring Cloud LoadBalancer. You do not add the annotation.
@LoadBalanced turns an HTTP client bean into a service-aware client that resolves logical service names to live instances and balances calls between them. Use Spring Cloud LoadBalancer with round robin by default, annotate the builder for RestClient and WebClient, keep a separate plain client for external URLs, and add retries or circuit breakers for resilience.