当我尝试执行以下使用功能区并尝试从 eureka 获取服务器列表的代码时,出现以下异常。
例外
3122 [main] WARN com.netflix.loadbalancer.RoundRobinRule - No up servers available from load balancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=origin,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:DiscoveryEnabledNIWSServerList:; clientName:origin; Effective vipAddresses:localhost; isSecure:false; datacenter:null
Exception in thread "main" com.netflix.client.ClientException: LoadBalancer returned null Server for :origin
at com.netflix.client.LoadBalancerContext.computeFinalUriWithLoadBalancer(LoadBalancerContext.java:418)
at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:166)
at com.netflix.zuul.RibbonTest.main(RibbonTest.java:23)
代码
public static void main(String[] args) throws Exception {
ConfigurationManager.loadPropertiesFromResources("ribbon-client.properties");
RestClient client = (RestClient) ClientFactory.getNamedClient("origin");
HttpRequest request = HttpRequest.newBuilder().uri(new URI("/serviceContext/api/v1/")).build();
for (int i = 0; i < 20; i++) {
HttpResponse response = client.executeWithLoadBalancer(request);
}
}
功能区-client.properties
origin.ribbon.NIWSServerListClassName=com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
#also tried unsuccessfully with localhost:8091, localhost:8091/serviceContext
origin.ribbon.DeploymentContextBasedVipAddresses=localhost
该服务是一个 Spring-Boot(不使用 Spring-Cloud)应用程序,使用eureka-client.properties
:
eureka-client.properties
eureka.registration.enabled=true
eureka.preferSameZone=true
eureka.shouldUseDns=false
eureka.serviceUrl.default=http://localhost:9080/eureka/v2/
eureka.region=default
eureka.name=service-Eureka
eureka.vipAddress=localhost
eureka.port=8091
eureka.instanceId=CHNSHL119363L
该服务已成功注册到部署在本地 tomcat@port 9080 中的 eureka,并且可以发现@http://localhost:9080/eureka/ and http://localhost:9080/eureka/v2/apps/
W/O 使用 Spring-Cloud 需要在上面的代码/配置中修复什么才能使用功能区从 eureka 动态获取服务器列表?