use of com.netflix.loadbalancer.Server in project cloud-sea-towerman by huadahuang1983.
the class HostnameFirstRule method choose.
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
logger.warn("no load balancer");
return null;
}
Server server = null;
while (server == null) {
if (Thread.interrupted()) {
return null;
}
List<Server> upList = lb.getReachableServers();
List<Server> allList = lb.getAllServers();
int serverCount = allList.size();
if (serverCount == 0) {
logger.warn("No servers. End regardless of pass, because subsequent passes only get more restrictive.");
return null;
}
server = choose(upList);
if (server == null) {
logger.warn("no reachable servers found, try choose from all servers.");
server = choose(allList);
}
if (server == null) {
logger.warn("没有找到指定主机,系统将随机选择");
int index = rand.nextInt(serverCount);
server = upList.get(index);
}
if (server == null) {
/*
* The only time this should happen is if the server list were
* somehow trimmed. This is a transient condition. Retry after
* yielding.
*/
Thread.yield();
continue;
}
if (server.isAlive()) {
return (server);
}
// Shouldn't actually happen.. but must be transient or a bug.
server = null;
Thread.yield();
}
return server;
}
use of com.netflix.loadbalancer.Server in project zuul by Netflix.
the class DiscoveryResultTest method serverStatsCacheForSameServer.
@Test
public void serverStatsCacheForSameServer() {
final InstanceInfo instanceInfo = Builder.newBuilder().setAppName("serverstats-cache").setHostName("serverstats-cache").setPort(7777).build();
final DiscoveryEnabledServer server = new DiscoveryEnabledServer(instanceInfo, false);
final DiscoveryEnabledServer serverSecure = new DiscoveryEnabledServer(instanceInfo, true);
final DynamicServerListLoadBalancer<Server> lb = new DynamicServerListLoadBalancer<>(new DefaultClientConfigImpl());
final DiscoveryResult result = new DiscoveryResult(server, lb.getLoadBalancerStats());
final DiscoveryResult result1 = new DiscoveryResult(serverSecure, lb.getLoadBalancerStats());
Truth.assertThat(result.getServerStats()).isSameInstanceAs(result1.getServerStats());
}
use of com.netflix.loadbalancer.Server in project zuul by Netflix.
the class DiscoveryResultTest method securePortMustCheckInstanceInfo.
@Test
public void securePortMustCheckInstanceInfo() {
final InstanceInfo instanceInfo = Builder.newBuilder().setAppName("secure-port").setHostName("secure-port").setPort(7777).enablePort(PortType.SECURE, false).build();
final InstanceInfo secureEnabled = Builder.newBuilder().setAppName("secure-port").setHostName("secure-port").setPort(7777).enablePort(PortType.SECURE, true).build();
final DiscoveryEnabledServer server = new DiscoveryEnabledServer(instanceInfo, true);
final DiscoveryEnabledServer secureServer = new DiscoveryEnabledServer(secureEnabled, true);
final DynamicServerListLoadBalancer<Server> lb = new DynamicServerListLoadBalancer<>(new DefaultClientConfigImpl());
final DiscoveryResult result = new DiscoveryResult(server, lb.getLoadBalancerStats());
final DiscoveryResult secure = new DiscoveryResult(secureServer, lb.getLoadBalancerStats());
Truth.assertThat(result.isSecurePortEnabled()).isFalse();
Truth.assertThat(secure.isSecurePortEnabled()).isTrue();
}
use of com.netflix.loadbalancer.Server in project zuul by Netflix.
the class DiscoveryResultTest method ipAddrV4FromInstanceInfo.
@Test
public void ipAddrV4FromInstanceInfo() {
final String ipAddr = "100.1.0.1";
final InstanceInfo instanceInfo = Builder.newBuilder().setAppName("ipAddrv4").setHostName("ipAddrv4").setIPAddr(ipAddr).setPort(7777).build();
final DiscoveryEnabledServer server = new DiscoveryEnabledServer(instanceInfo, false);
final DynamicServerListLoadBalancer<Server> lb = new DynamicServerListLoadBalancer<>(new DefaultClientConfigImpl());
final DiscoveryResult result = new DiscoveryResult(server, lb.getLoadBalancerStats());
Truth.assertThat(result.getIPAddr()).isEqualTo(Optional.of(ipAddr));
}
use of com.netflix.loadbalancer.Server in project zuul by Netflix.
the class DiscoveryResultTest method sameUnderlyingInstanceInfoEqualsSameResult.
@Test
public void sameUnderlyingInstanceInfoEqualsSameResult() {
final InstanceInfo instanceInfo = Builder.newBuilder().setAppName("server-equality").setHostName("server-equality").setPort(7777).build();
final DiscoveryEnabledServer server = new DiscoveryEnabledServer(instanceInfo, false);
final DiscoveryEnabledServer otherServer = new DiscoveryEnabledServer(instanceInfo, false);
final DynamicServerListLoadBalancer<Server> lb = new DynamicServerListLoadBalancer<>(new DefaultClientConfigImpl());
final DiscoveryResult result = new DiscoveryResult(server, lb.getLoadBalancerStats());
final DiscoveryResult otherResult = new DiscoveryResult(otherServer, lb.getLoadBalancerStats());
Truth.assertThat(result).isEqualTo(otherResult);
}
Aggregations