Difference between @RibbonClient and @LoadBalanced

jack picture jack · Sep 20, 2016 · Viewed 27.2k times · Source

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?

Answer

dustin.schultz picture dustin.schultz · Mar 25, 2017

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