spring-bootspring-cloudspring-cloud-feignjava-17spring-cloud-loadbalancer

How to configure Spring Cloud LoadBalancer with SpringCloudOpenFeign


Changing a work project's version to java 17 resulted in the need to increase the spring-cloud-starter-openfeign to 3.0.6 (part of the 2020.0.0 Spring Cloud release train). Since Spring Cloud Netflix Ribbon has been deprecated and is not included in the 2020.0.0 release train, I have to make some changes in my project and switch to using Spring Cloud LoadBalancer.

After reading some articles I got the following understanding: Feign was using Ribbon as a LoadBalancer. Ribbon was configured in a properties file with different lists of hosts for all the applications clients.

My questions are:

  1. How am I supposed to use Spring Cloud LoadBalancer since I did not manage to find any comprehensible examples for me?
  2. Since I am using SpringCloudOpenFeign 3.0.6, which would be the suitable spring-cloud-starter-loadbalancer version? I have tried using 3.0.5( but I get a Unexpected exception during bean creation; nested exception is java.lang.IllegalArgumentException: @RequestMapping annotation not allowed on @FeignClient interfaces) and also 3.1.0 (and I get java.lang.ClassNotFoundException:org.springframework.cloud.client.loadbalancer.LoadBalancerClientsProperties); did not find any related info for neither of the errors on google

Solution

  • You can find comprehensive documentation of Spring Cloud LoadBalancer, with all the details on how to set it up here. You can use it with Service Registry or, if you want to pass the list of hosts manually in properties, you can use SimpleDiscoveryClient You can also find a sample that uses Spring Cloud OpenFeign with Spring Cloud LoadBalancer here - the spring-cloud-starter-loadbalancer dependency is not in the pom, but gets included transitively via spring-cloud-starter-netflix-eureka-client. For more information about switching from Spring Cloud Netflix - based stack to the currently supported Spring Cloud stack, you can also watch this presentation recording.

    Spring Cloud OpenFeign 3.0.5 works with Spring Cloud LoadBalancer 3.0.4, however, manually setting library versions for Spring Cloud is discouraged in general. You should use dependency management instead. You can find the setup at Spring Cloud projects landing page. Also, the easiest way to generate a correct build file for Spring Cloud Projects is to use start.spring.io.