Java Examples for com.netflix.loadbalancer.BaseLoadBalancer

The following java examples will help you to understand the usage of com.netflix.loadbalancer.BaseLoadBalancer. These source code samples are taken from different open source projects.

Example 1
Project: ribbon-master  File: NettyClientTest.java View source code
@Test
public void testObservableWithMultipleServers() throws Exception {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder().withRule(new AvailabilityFilteringRule()).withPing(new DummyPing()).buildFixedServerListLoadBalancer(servers);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(1, 3, true));
    Person person = getPersonObservable(lbObservables.submit(request)).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    ServerStats stats = lbObservables.getServerStats(badServer);
    // two requests to bad server because retry same server is set to 1
    assertEquals(4, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(4, stats.getSuccessiveConnectionFailureCount());
    stats = lbObservables.getServerStats(goodServer);
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
    person = getPersonObservable(lbObservables.submit(request)).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    HttpClientListener listener = lbObservables.getListener();
    assertEquals(1, listener.getPoolReuse());
}
Example 2
Project: dataservices-sdk-java-master  File: SmartHttpClientTest.java View source code
@Test
public void testRibbonClient() throws Exception {
    int totalRequests = 20;
    String vipAddress = "www.foo.com";
    String serverString = "www.wikipedia.org:80,www.time.gov:80,www.bing.com:80,api.atmosonline.com:80";
    List<Server> servers = SmartClientConfig.parseServerList(serverString);
    String clientName = "testRibbonClient";
    SmartHttpClient client = new SmartHttpClient(clientName, new SmartClientConfig().withInitialNodes(servers.toArray(new Server[servers.size()])).withVipAddresses(vipAddress).withPollProtocol("http"));
    // fire off a number of requests
    for (int i = 0; i < totalRequests; i++) {
        HttpEntity e = client.execute(new HttpGet("http://" + vipAddress + "/")).getEntity();
        if (e != null)
            readAndClose(e.getContent());
    }
    BaseLoadBalancer lb = (BaseLoadBalancer) client.getLoadBalancer();
    LoadBalancerStats lbStats = lb.getLoadBalancerStats();
    assertNull("No requests should go to " + vipAddress, lbStats.getServerStats().get(new Server(vipAddress, 80)));
    int requestCount = 0;
    for (Server server : servers) {
        requestCount += lbStats.getSingleServerStat(server).getTotalRequestsCount();
        assertTrue("At least one request should go to " + server, lbStats.getSingleServerStat(server).getTotalRequestsCount() > 0);
    }
    assertEquals("Total requests should be " + totalRequests, totalRequests, requestCount);
}
Example 3
Project: suro-master  File: TestElasticSearchSink.java View source code
@Test
public void testCreate() throws IOException {
    String desc = "    {\n" + "        \"type\": \"elasticsearch\",\n" + "        \"queue4Sink\":{\"type\": \"memory\", \"capacity\": 0 },\n" + "        \"batchSize\": 100,\n" + "        \"batchTimeout\": 1000,\n" + "        \"clientName\": \"es_test\",\n" + "        \"cluster.name\": \"es_test\",\n" + "        \"addressList\": [\"http://host1:8080\", \"http://host2:8080\"],\n" + "        \"indexInfo\":{\n" + "            \"type\": \"default\",\n" + "            \"indexTypeMap\":{\"routingkey1\":\"index1:type1\", \"routingkey2\":\"index2:type2\"},\n" + "            \"idFields\":{\"index\":[\"f1\", \"f2\"]},\n" + "            \"timestamp\": {\"field\":\"ts\"},\n" + "            \"indexSuffixFormatter\":{\"type\": \"date\", \"properties\":{\"dateFormat\":\"YYYYMMdd\"}}\n" + "        }\n" + "    }";
    final ObjectMapper jsonMapper = new DefaultObjectMapper();
    jsonMapper.registerSubtypes(new NamedType(ElasticSearchSink.class, "elasticsearch"));
    jsonMapper.setInjectableValues(new InjectableValues() {

        @Override
        public Object findInjectableValue(Object valueId, DeserializationContext ctxt, BeanProperty forProperty, Object beanInstance) {
            if (valueId.equals(ObjectMapper.class.getCanonicalName())) {
                return jsonMapper;
            } else {
                return null;
            }
        }
    });
    Sink sink = jsonMapper.readValue(desc, new TypeReference<Sink>() {
    });
    assertTrue(sink instanceof ElasticSearchSink);
    ElasticSearchSink esSink = (ElasticSearchSink) sink;
    esSink.createClient();
    RestClient client = esSink.getClient();
    IClientConfig config = ((BaseLoadBalancer) client.getLoadBalancer()).getClientConfig();
    assertTrue(config.get(CommonClientConfigKey.OkToRetryOnAllOperations));
    assertEquals(2, config.get(CommonClientConfigKey.MaxAutoRetriesNextServer).intValue());
    assertEquals(0, esSink.getSleepOverClientException());
    assertFalse(esSink.getReenqueueOnException());
}
Example 4
Project: Tests-master  File: TomcatApp.java View source code
@Bean
public ILoadBalancer ribbonLoadBalancer() {
    //because of this, it doesn't use eureka to lookup the server,
    // but the classpath is tested
    BaseLoadBalancer balancer = new BaseLoadBalancer();
    balancer.setServersList(Arrays.asList(new Server("localhost", port)));
    return balancer;
}
Example 5
Project: spring-cloud-netflix-master  File: FeignAcceptEncodingTests.java View source code
@Bean
public ILoadBalancer ribbonLoadBalancer() {
    BaseLoadBalancer balancer = new BaseLoadBalancer();
    balancer.setServersList(Collections.singletonList(new Server("localhost", this.port)));
    return balancer;
}