Difference between @RibbonClient and @LoadBalanced

31,463

Solution 1

TL;DR: @LoadBalanced is a marker annotation & @RibbonClient is used for configuration purposes.


@LoadBalanced

Used as a marker annotation indicating that the annotated RestTemplate should use a RibbonLoadBalancerClient for interacting with your service(s).

In turn, this allows you to use "logical identifiers" for the URLs you pass to the RestTemplate. These logical identifiers are typically the name of a service. For example:

restTemplate.getForObject("http://some-service-name/user/{id}", String.class, 1);

where some-service-name is the logical identifier.

@RibbonClient

Used for configuring your Ribbon client(s).

Is @RibbonClient required?

No! If you're using Service Discovery and you're ok with all of the default Ribbon settings, you don't even need to use the @RibbonClient annotation.

When should I use @RibbonClient?

There are at least two cases where you need to use @RibbonClient

  1. You need to customize your Ribbon settings for a particular Ribbon client
  2. You're not using any service discovery

Customizing your Ribbon settings:

Define a @RibbonClient

@RibbonClient(name = "some-service", configuration = SomeServiceConfig.class)
  • name - set it to the same name of the service you're calling with Ribbon but need additional customizations for how Ribbon interacts with that service.
  • configuration - set it to an @Configuration class with all of your customizations defined as @Beans. Make sure this class is not picked up by @ComponentScan otherwise it will override the defaults for ALL Ribbon clients.

See the section "Customizing the RibbonClient` in the Spring Cloud Netflix documentation (link)

Using Ribbon without Service Discovery

If you're not using Service Discovery, the name field of the @RibbonClient annotation will be used to prefix your configuration in the application.properties as well as "logical identifier" in the URL you pass to RestTemplate.

Define a @RibbonClient

@RibbonClient(name = "myservice")

then in your application.properties

myservice.ribbon.eureka.enabled=false
myservice.ribbon.listOfServers=http://localhost:5000, http://localhost:5001

Solution 2

RestTemplate supports load balancing, using @LoadBalanced tells Spring Cloud that we want to take advantage of its load balancing support(If you are using Ribbon then the effect of using @LoadBalanced will be that RestTemplate will use RibbionClient to get the server address).
You can also check how LoadBalancerAutoConfiguration works here

Using @RibbonClients you can provide declarative configuration for a ribbon client.

E.g.

@SpringBootApplication
@RestController
@RibbonClient(name = "app", configuration = RibbonConfig.class)
public class App {

  @LoadBalanced
  @Bean
  RestTemplate restTemplate(){
    return new RestTemplate();
  }
  //...
}

Then you create RibbonConfig.class to override any Ribbon related bean.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PingUrl;
import com.netflix.loadbalancer.AvailabilityFilteringRule;

public class RibbonConfig {

  @Autowired
  IClientConfig ribbonClientConfig;

  @Bean
  public IPing ribbonPing (IClientConfig config) {
    return new PingUrl();//we override default Iping which is a NoOpPing
  }

  @Bean
  public IRule ribbonRule(IClientConfig config) {
    return new AvailabilityFilteringRule(); // we override the default ZoneAvoidanceRule
  }

}
Share:
31,463
jack
Author by

jack

Updated on February 08, 2020

Comments

  • jack
    jack over 4 years

    I understand @LoadBalanced indicates the Rest template should be based on Client Side Load Balancing using Ribbon and checks Eureka server for resolving the service name to host/port.

    What is the use of @RibbonClient. Is it to support native Ribbon Client LB without Eureka and also support Eureka Discover when configured with DiscoveryEnabledNIWSServerList?