无法解析符号 'CachingSpringLoadBalancerFactory'

Cannot resolve symbol 'CachingSpringLoadBalancerFactory'

我有这段代码,我想迁移到最新版本的 '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);
  }
}

使用版本 'org.springframework.cloud:spring-cloud-openfeign-core:2.2.2.RELEASE' 代码运行良好,但当我切换到版本 3.1.1 时出现导入错误:

Cannot resolve symbol 'ribbon'

你知道我必须如何迁移代码吗?

代码示例:https://github.com/rcbandit111/Generic_SO_POC/blob/master/src/main/java/org/merchant/database/service/sql/LoadbalancerConfig.java

搜索更多关于...我在这个 topic 中发现了一个问题:

spring-cloud-netflix-ribbon has been removed from SpringCloud with this version

然后你需要将spring-cloud-starter-loadbalancer依赖添加到你的项目中。

implementation 'org.springframework.cloud:spring-cloud-starter-loadbalancer:3.1.1'

Spring Cloud OpenFeign official reference 1.2 Overriding Feign Defaults 部分:

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.

所以你可以尝试在LoadBalancerConfig中使用FeignBlockingLoadBalancerClient:

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 httpClientConnectionManager,
            LoadBalancerClient lbClientFactory,
            LoadBalancerClientFactory clientFactory) {

        CloseableHttpClient closeableHttpClient = HttpClients.custom()
                .setConnectionManager(httpClientConnectionManager)
                .build();
        ApacheHttpClient client = new ApacheHttpClient(closeableHttpClient);
        return new FeignBlockingLoadBalancerClient(client, lbClientFactory, clientFactory);
    }
}

参考文献: