Search in sources :

Example 36 with Server

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;
}
Also used : Server(com.netflix.loadbalancer.Server)

Example 37 with 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());
}
Also used : DiscoveryEnabledServer(com.netflix.niws.loadbalancer.DiscoveryEnabledServer) Server(com.netflix.loadbalancer.Server) DynamicServerListLoadBalancer(com.netflix.loadbalancer.DynamicServerListLoadBalancer) DiscoveryEnabledServer(com.netflix.niws.loadbalancer.DiscoveryEnabledServer) InstanceInfo(com.netflix.appinfo.InstanceInfo) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Example 38 with Server

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();
}
Also used : DiscoveryEnabledServer(com.netflix.niws.loadbalancer.DiscoveryEnabledServer) Server(com.netflix.loadbalancer.Server) DynamicServerListLoadBalancer(com.netflix.loadbalancer.DynamicServerListLoadBalancer) DiscoveryEnabledServer(com.netflix.niws.loadbalancer.DiscoveryEnabledServer) InstanceInfo(com.netflix.appinfo.InstanceInfo) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Example 39 with Server

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));
}
Also used : DiscoveryEnabledServer(com.netflix.niws.loadbalancer.DiscoveryEnabledServer) Server(com.netflix.loadbalancer.Server) DynamicServerListLoadBalancer(com.netflix.loadbalancer.DynamicServerListLoadBalancer) DiscoveryEnabledServer(com.netflix.niws.loadbalancer.DiscoveryEnabledServer) InstanceInfo(com.netflix.appinfo.InstanceInfo) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Example 40 with Server

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);
}
Also used : DiscoveryEnabledServer(com.netflix.niws.loadbalancer.DiscoveryEnabledServer) Server(com.netflix.loadbalancer.Server) DynamicServerListLoadBalancer(com.netflix.loadbalancer.DynamicServerListLoadBalancer) DiscoveryEnabledServer(com.netflix.niws.loadbalancer.DiscoveryEnabledServer) InstanceInfo(com.netflix.appinfo.InstanceInfo) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Aggregations

Server (com.netflix.loadbalancer.Server)133 Test (org.junit.Test)98 ArrayList (java.util.ArrayList)40 ByteBuf (io.netty.buffer.ByteBuf)26 MockWebServer (com.google.mockwebserver.MockWebServer)25 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)25 IClientConfig (com.netflix.client.config.IClientConfig)23 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)20 DummyPing (com.netflix.loadbalancer.DummyPing)18 HttpServer (com.sun.net.httpserver.HttpServer)18 URI (java.net.URI)15 Invocation (org.apache.servicecomb.core.Invocation)14 DynamicServerListLoadBalancer (com.netflix.loadbalancer.DynamicServerListLoadBalancer)12 ServerStats (com.netflix.loadbalancer.ServerStats)12 Person (com.netflix.ribbon.test.resources.EmbeddedResources.Person)12 MockUp (mockit.MockUp)12 ClientException (com.netflix.client.ClientException)11 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)10 ExecutionListener (com.netflix.loadbalancer.reactive.ExecutionListener)9 HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)9