I have this code that I would like to migrate to the latest version of 'org.springframework.cloud:spring-cloud-openfeign-core:3.1.1'
import feign.Client;
import feign.httpclient.ApacheHttpClient;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LoadbalancerConfig {
@Bean
public Client client(
HttpClientConnectionManager httpClientConnectionManager,
CachingSpringLoadBalancerFactory lbClientFactory,
SpringClientFactory clientFactory) {
CloseableHttpClient closeableHttpClient = HttpClients.custom()
.setConnectionManager(httpClientConnectionManager)
.build();
ApacheHttpClient client = new ApacheHttpClient(closeableHttpClient);
return new LoadBalancerFeignClient(client, lbClientFactory, clientFactory);
}
}
With version 'org.springframework.cloud:spring-cloud-openfeign-core:2.2.2.RELEASE'
the code is working fine bur when I switch to version 3.1.1 I get import errors:
Cannot resolve symbol 'ribbon'
Do you know how I have to migrate the code?
Searching more about... I found an issue in this topic:
spring-cloud-netflix-ribbon has been removed from SpringCloud with this version
Then you need to add the spring-cloud-starter-loadbalancer
dependency to your project.
implementation 'org.springframework.cloud:spring-cloud-starter-loadbalancer:3.1.1'
Spring Cloud OpenFeign official reference in 1.2 Overriding Feign Defaults
section:
Client feignClient: if Ribbon is in the classpath and is enabled it is a LoadBalancerFeignClient,
otherwise if Spring Cloud LoadBalancer is in the classpath, FeignBlockingLoadBalancerClient is used.
If none of them is in the classpath, the default feign client is used.
So you can try to use the FeignBlockingLoadBalancerClient
in the LoadBalancerConfig
:
import feign.Client;
import feign.httpclient.ApacheHttpClient;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LoadBalancerConfig {
@Bean
public Client client(HttpClientConnectionManager connectionManager,
LoadBalancerClient lbClient,
LoadBalancerClientFactory lbClientFactory) {
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.build();
ApacheHttpClient client = new ApacheHttpClient(httpClient);
return new FeignBlockingLoadBalancerClient(client, lbClient, lbClientFactory);
}
}