Java Code Examples for com.netflix.client.config.DefaultClientConfigImpl#getClientConfigWithDefaultValues()

The following examples show how to use com.netflix.client.config.DefaultClientConfigImpl#getClientConfigWithDefaultValues() . 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: EurekaDynamicServerListLoadBalancerTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    PowerMock.mockStatic(DiscoveryClient.class);

    EasyMock
            .expect(DiscoveryClient.getZone(EasyMock.isA(InstanceInfo.class)))
            .andReturn("zone")
            .anyTimes();

    eurekaClientMock = setUpEurekaClientMock(servers);
    eurekaClientProvider = new Provider<EurekaClient>() {
        @Override
        public EurekaClient get() {
            return eurekaClientMock;
        }
    };

    config = DefaultClientConfigImpl.getClientConfigWithDefaultValues();
    config.set(CommonClientConfigKey.DeploymentContextBasedVipAddresses, vipAddress);
    config.set(CommonClientConfigKey.ServerListUpdaterClassName, EurekaNotificationServerListUpdater.class.getName());
}
 
Example 2
Source File: DynamicServerListLoadBalancerTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testDynamicServerListLoadBalancer() throws Exception {
    DefaultClientConfigImpl config = DefaultClientConfigImpl.getClientConfigWithDefaultValues();
    config.set(CommonClientConfigKey.NIWSServerListClassName, MyServerList.class.getName());
    config.set(CommonClientConfigKey.NFLoadBalancerClassName, DynamicServerListLoadBalancer.class.getName());
    config.set(CommonClientConfigKey.ServerListRefreshInterval, 50);
    DynamicServerListLoadBalancer<Server> lb = new DynamicServerListLoadBalancer<Server>(config);
    try {
        assertTrue(MyServerList.latch.await(2, TimeUnit.SECONDS));
    } catch (InterruptedException e) { // NOPMD
    }
    assertEquals(lb.getAllServers(), MyServerList.list);
    lb.stopServerListRefreshing();
    Thread.sleep(1000);
    int count = MyServerList.counter.get();
    assertTrue(count >= 5);
    Thread.sleep(1000);
    assertEquals(count, MyServerList.counter.get());
    
}
 
Example 3
Source File: ExecutionContextTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubContext() {
    ExecutionContext<String> context = new ExecutionContext<String>("hello", DefaultClientConfigImpl.getEmptyConfig(),
            DefaultClientConfigImpl.getClientConfigWithDefaultValues(), RetryHandler.DEFAULT);
    ExecutionContext<String> subContext1 = context.getChildContext("foo");
    ExecutionContext<String> subContext2 = context.getChildContext("bar");
    assertSame(context, context.getGlobalContext());
    context.put("dummy", "globalValue");
    context.put("dummy2", "globalValue");
    subContext1.put("dummy", "context1Value");
    subContext2.put("dummy", "context2Value");
    assertEquals("context1Value", subContext1.get("dummy"));
    assertEquals("context2Value", subContext2.get("dummy"));
    assertEquals("globalValue", subContext1.getGlobalContext().get("dummy"));
    assertNull(subContext1.get("dummy2"));
}
 
Example 4
Source File: ConfigurationBasedServerListTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testList() {		
	ConfigurationBasedServerList list = new ConfigurationBasedServerList();
	DefaultClientConfigImpl config = DefaultClientConfigImpl.getClientConfigWithDefaultValues("junit1");
	list.initWithNiwsConfig(config);
	assertTrue(list.getInitialListOfServers().isEmpty());
	ConfigurationManager.getConfigInstance().setProperty("junit1.ribbon.listOfServers", "abc.com:80,microsoft.com,1.2.3.4:8080");
	List<Server> servers = list.getUpdatedListOfServers();
	List<Server> expected = new ArrayList<Server>();
	expected.add(new Server("abc.com:80"));
	expected.add(new Server("microsoft.com:80"));
	expected.add(new Server("1.2.3.4:8080"));
	assertEquals(expected, servers);
	ConfigurationManager.getConfigInstance().setProperty("junit1.ribbon.listOfServers", "");
	assertTrue(list.getUpdatedListOfServers().isEmpty());
	ConfigurationManager.getConfigInstance().clearProperty("junit1.ribbon.listOfServers");
	assertTrue(list.getUpdatedListOfServers().isEmpty());
}
 
Example 5
Source File: FollowRedirectTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testRedirectNotFollowed() throws Exception {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues("myclient");
    config.set(CommonClientConfigKey.FollowRedirects, Boolean.FALSE);
    ClientFactory.registerClientFromProperties("myclient", config);
    RestClient client = (RestClient) ClientFactory.getNamedClient("myclient");
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("http://localhost:" + redirectingServer.getPort())).build();
    HttpResponse response = client.executeWithLoadBalancer(request);
    assertEquals(302, response.getStatus());          
}
 
Example 6
Source File: LoadBalancingHttpClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
public LoadBalancingHttpClient<I, O> build() {
    if (retryHandler == null) {
        retryHandler = new NettyHttpLoadBalancerErrorHandler();
    }
    if (config == null) {
        config = DefaultClientConfigImpl.getClientConfigWithDefaultValues();
    }
    if (lb == null) {
        lb = LoadBalancerBuilder.newBuilder().withClientConfig(config).buildLoadBalancerFromConfigWithReflection();
    }
    if (listeners == null) {
        listeners = Collections.<ExecutionListener<HttpClientRequest<I>, HttpClientResponse<O>>>emptyList();
    }
    if (backoffStrategy == null) {
        backoffStrategy = new Func1<Integer, Integer>() {
            @Override
            public Integer call(Integer backoffCount) {
                int interval = config.getOrDefault(IClientConfigKey.Keys.BackoffInterval);
                if (backoffCount < 0) {
                    backoffCount = 0;
                }
                else if (backoffCount > 10) {   // Reasonable upper bound
                    backoffCount = 10;
                }
                return (int)Math.pow(2, backoffCount) * interval;
            }
        };
    }
    if (responseToErrorPolicy == null) {
        responseToErrorPolicy = new DefaultResponseToErrorPolicy<O>();
    }
    return build.call(this);
}
 
Example 7
Source File: UdpClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testUdpClientTimeout() throws Exception {
    int port = choosePort();
    UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 5000).createServer();
    server.start();
    BaseLoadBalancer lb = new BaseLoadBalancer();
    Server myServer = new Server("localhost", port);
    lb.setServersList(Lists.newArrayList(myServer));
    MyUDPClient client = new MyUDPClient(lb, DefaultClientConfigImpl.getClientConfigWithDefaultValues());
    try {
        String response = client.submit("Is there anybody out there?")
                .map(new Func1<DatagramPacket, String>() {
                    @Override
                    public String call(DatagramPacket datagramPacket) {
                        return datagramPacket.content().toString(Charset.defaultCharset());
                    }
                })
                .toBlocking()
                .first();
        fail("Exception expected");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof TimeoutException);
        assertEquals(1, client.getLoadBalancerContext().getServerStats(myServer).getSuccessiveConnectionFailureCount());
    }
    finally {
        server.shutdown();
    }
}
 
Example 8
Source File: BasicNettyOrigin.java    From zuul with Apache License 2.0 5 votes vote down vote up
protected IClientConfig setupClientConfig(String name) {
    // Get the NIWS properties for this Origin.
    IClientConfig niwsClientConfig = DefaultClientConfigImpl.getClientConfigWithDefaultValues(name);
    niwsClientConfig.set(CommonClientConfigKey.ClientClassName, name);
    niwsClientConfig.loadProperties(name);
    return niwsClientConfig;
}
 
Example 9
Source File: RibbonTransport.java    From ribbon with Apache License 2.0 4 votes vote down vote up
public static LoadBalancingHttpClient<ByteBuf, ByteBuf> newHttpClient() {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues();
    return newHttpClient(config);
}
 
Example 10
Source File: RibbonTransport.java    From ribbon with Apache License 2.0 4 votes vote down vote up
public static LoadBalancingHttpClient<ByteBuf, ByteBuf> newHttpClient(ILoadBalancer loadBalancer) {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues();
    return newHttpClient(loadBalancer, config);
}
 
Example 11
Source File: RibbonTransport.java    From ribbon with Apache License 2.0 4 votes vote down vote up
public static LoadBalancingHttpClient<ByteBuf, ServerSentEvent> newSSEClient() {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues();
    return newSSEClient(config);
}
 
Example 12
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadBalancingWithTwoServers() throws Exception {
    MockWebServer server = new MockWebServer();
    String content = "{\"name\": \"ribbon\", \"age\": 2}";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json")
            .setBody(content));       
    server.play();

    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues();
            
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/person")
            .withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null))
            .withHeader("Content-type", "application/json");
    NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
    HttpClientListener externalListener = HttpClientListener.newHttpListener("external");
    lbObservables.subscribe(externalListener);
    Server server1 = new Server("localhost:" + server.getPort());
    Server server2 = new Server("localhost:" + port);
    
    lb.setServersList(Lists.newArrayList(server1, server2));
    RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) {
        @Override
        public boolean isRetriableException(Throwable e, boolean sameServer) {
            return true;
        }
    };
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    assertEquals("ribbon", observer.obj.name);
    assertEquals(EmbeddedResources.defaultPerson.age, observer.obj.age);
    
    observer = new ObserverWithLatch<Person>();
    observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    assertEquals("ribbon", observer.obj.name);
    assertEquals(2, observer.obj.age);
    
    ServerStats stats = lbObservables.getServerStats(server1);
    server.shutdown();
    // assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    
    stats = lbObservables.getServerStats(server2);
    // two requests to bad server because retry same server is set to 1
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
    final HttpClientListener listener = lbObservables.getListener();
    assertEquals(2, listener.getPoolAcquires());
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 2;
        }
    });
    assertEquals(2, listener.getConnectionCount());
    assertEquals(0, listener.getPoolReuse());
    assertEquals(2, externalListener.getPoolAcquires());
}