以前是使用 Spring cloud Finchley.M9 发布的。我们使用如下所示的 LoadBalancerClient。
'''import java.net.URI;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Component;
@Component
public class UriResolver {
private static final Logger LOGGER = LoggerFactory.getLogger(UriResolver.class);
private LoadBalancerClient loadBalancerClient;
@Autowired
public UriResolver(LoadBalancerClient loadBalancerClient) {
this.loadBalancerClient = loadBalancerClient;
}
public URI getBaseUri(String vendorName) {
ServiceInstance instance = loadBalancerClient.choose(vendorName);
URI uri = null;
if (!Objects.isNull(instance)) {
uri = instance.getUri();
LOGGER.debug("LoadBalancer Instance specific URI: " + instance.getUri());
LOGGER.debug("LoadBalancer Instance specific ServiceId: " + instance.getServiceId());
LOGGER.debug("LoadBalancer Instance specific Host: " + instance.getHost());
}
return uri;
}
}'''
现在我们已经将spring cloud版本升级到了Hoxton.SR8。启动 spring boot 应用程序时出现以下错误。
'RibbonAutoConfiguration' 中的 Bean 方法 'loadBalancerClient' 未加载,因为 AllNestedConditions 2 匹配 2 没有;RibbonAutoConfiguration.RibbonClassesConditions.RibbonPresent @ConditionalOnClass 上的 NestedCondition 未找到所需的类“com.netflix.ribbon.Ribbon”;RibbonAutoConfiguration.RibbonClassesConditions.AsyncRestTemplatePresent @ConditionalOnClass 上的 NestedCondition 找到所需的类 'org.springframework.web.client.AsyncRestTemplate';RibbonAutoConfiguration.RibbonClassesConditions.RestTemplatePresent @ConditionalOnClass 上的 NestedCondition 找到所需的类 'org.springframework.web.client.RestTemplate';RibbonAutoConfiguration.RibbonClassesConditions.IClientPresent @ConditionalOnClass 上的 NestedCondition 未找到所需的类“com.netflix.client.IClient”