com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList Java Examples

The following examples show how to use com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProxyHandler.java    From Prana with Apache License 2.0 6 votes vote down vote up
private LoadBalancingHttpClient<ByteBuf, ByteBuf> getClient(String vip) {
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = httpClients.get(vip);
    if (client == null) {
        IClientConfig config = IClientConfig.Builder.newBuilder("prana_backend").
                withDefaultValues().
                withDeploymentContextBasedVipAddresses(vip).
                build().
                set(IClientConfigKey.Keys.MaxTotalConnections, 2000).
                set(IClientConfigKey.Keys.MaxConnectionsPerHost, 2000).
                set(IClientConfigKey.Keys.OkToRetryOnAllOperations, false).
                set(IClientConfigKey.Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());

        client = RibbonTransport.newHttpClient(new HttpClientPipelineConfigurator<ByteBuf, ByteBuf>(), config);
        httpClients.putIfAbsent(vip, client);

    }
    return client;
}
 
Example #2
Source File: DiscoveryEnabledServerListTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testDynamicServers() {
    ConfigurationManager.getConfigInstance().setProperty("MyService.ribbon." + Keys.DeploymentContextBasedVipAddresses, getVipAddress());
    ConfigurationManager.getConfigInstance().setProperty("MyService.ribbon." + Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());
    HttpResourceGroup group = Ribbon.createHttpResourceGroupBuilder("MyService")
            .withClientOptions(ClientOptions.create()
                    .withMaxAutoRetriesNextServer(3)
                    .withReadTimeout(300000)).build();
    HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class)
            .withUriTemplate("/")
            .withMethod("GET").build();
    RibbonRequest<ByteBuf> request = template
            .requestBuilder().build();
    String result = request.execute().toString(Charset.defaultCharset());
    assertEquals("Hello world", result);
}
 
Example #3
Source File: DiscoveryLoadBalancerTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadBalancer() {
    IClientConfig config = IClientConfig.Builder.newBuilder().withDefaultValues()
            .withDeploymentContextBasedVipAddresses(getVipAddress()).build()
            .set(IClientConfigKey.Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(config);
    LoadBalancerContext lbContext = client.getLoadBalancerContext();
    List<Server> serverList = lbContext.getLoadBalancer().getAllServers();
    assertEquals(getMockServerList(), serverList);
}
 
Example #4
Source File: EurekaLoadBalancer.java    From suro with Apache License 2.0 6 votes vote down vote up
/**
 * @param config contains vipAddress
 */
@Inject
public EurekaLoadBalancer(ClientConfig config) {
    String[] vipAddress_port = config.getLoadBalancerServer().split(":");
    if (vipAddress_port.length != 2) {
        throw new IllegalArgumentException(String.format(
                "EurekaLoadBalancer server should be formatted vipAddress:port ('%s')", 
                config.getLoadBalancerServer()));
    }

    this.port = Integer.parseInt(vipAddress_port[1]);
    IClientConfig loadBalancerConfig = new DefaultClientConfigImpl();
    loadBalancerConfig.loadProperties("suroClient");
    loadBalancerConfig.setProperty(CommonClientConfigKey.DeploymentContextBasedVipAddresses, vipAddress_port[0]);
    loadBalancerConfig.setProperty(CommonClientConfigKey.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());
    super.initWithNiwsConfig(loadBalancerConfig);
}
 
Example #5
Source File: EurekaDynamicServerListLoadBalancerTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadBalancerHappyCase() throws Exception {
    Assert.assertNotEquals("the two test server list counts should be different",
            secondServerListSize, initialServerListSize);

    DynamicServerListLoadBalancer<DiscoveryEnabledServer> lb = null;
    try {
        Capture<EurekaEventListener> eventListenerCapture = new Capture<EurekaEventListener>();
        eurekaClientMock.registerEventListener(EasyMock.capture(eventListenerCapture));

        PowerMock.replay(DiscoveryClient.class);
        PowerMock.replay(eurekaClientMock);

        // actual testing
        // initial creation and loading of the first serverlist
        lb = new DynamicServerListLoadBalancer<DiscoveryEnabledServer>(
                config,
                new AvailabilityFilteringRule(),
                new DummyPing(),
                new DiscoveryEnabledNIWSServerList(vipAddress, eurekaClientProvider),
                new ZoneAffinityServerListFilter<DiscoveryEnabledServer>(),
                new EurekaNotificationServerListUpdater(eurekaClientProvider)
        );

        Assert.assertEquals(initialServerListSize, lb.getServerCount(false));

        // trigger an eureka CacheRefreshEvent
        eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());

        Assert.assertTrue(verifyFinalServerListCount(secondServerListSize, lb));

    } finally {
        if (lb != null) {
            lb.shutdown();

            PowerMock.verify(eurekaClientMock);
            PowerMock.verify(DiscoveryClient.class);
        }
    }
}
 
Example #6
Source File: EurekaDynamicServerListLoadBalancerTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testShutdownMultiple() {
    try {
        eurekaClientMock.registerEventListener(EasyMock.anyObject(EurekaEventListener.class));
        EasyMock.expectLastCall().anyTimes();

        PowerMock.replay(DiscoveryClient.class);
        PowerMock.replay(eurekaClientMock);

        DynamicServerListLoadBalancer<DiscoveryEnabledServer> lb1 = new DynamicServerListLoadBalancer<DiscoveryEnabledServer>(
                config,
                new AvailabilityFilteringRule(),
                new DummyPing(),
                new DiscoveryEnabledNIWSServerList(vipAddress, eurekaClientProvider),
                new ZoneAffinityServerListFilter<DiscoveryEnabledServer>(),
                new EurekaNotificationServerListUpdater(eurekaClientProvider)
        );

        DynamicServerListLoadBalancer<DiscoveryEnabledServer> lb2 = new DynamicServerListLoadBalancer<DiscoveryEnabledServer>(
                config,
                new AvailabilityFilteringRule(),
                new DummyPing(),
                new DiscoveryEnabledNIWSServerList(vipAddress, eurekaClientProvider),
                new ZoneAffinityServerListFilter<DiscoveryEnabledServer>(),
                new EurekaNotificationServerListUpdater(eurekaClientProvider)
        );

        DynamicServerListLoadBalancer<DiscoveryEnabledServer> lb3 = new DynamicServerListLoadBalancer<DiscoveryEnabledServer>(
                config,
                new AvailabilityFilteringRule(),
                new DummyPing(),
                new DiscoveryEnabledNIWSServerList(vipAddress, eurekaClientProvider),
                new ZoneAffinityServerListFilter<DiscoveryEnabledServer>(),
                new EurekaNotificationServerListUpdater(eurekaClientProvider)
        );

        lb3.shutdown();
        lb1.shutdown();
        lb2.shutdown();
    } finally {
        PowerMock.verify(eurekaClientMock);
        PowerMock.verify(DiscoveryClient.class);
    }
}