com.netflix.client.config.CommonClientConfigKey Java Examples

The following examples show how to use com.netflix.client.config.CommonClientConfigKey. 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: AbstractServerList.java    From ribbon with Apache License 2.0 6 votes vote down vote up
/**
 * Get a ServerListFilter instance. It uses {@link ClientFactory#instantiateInstanceWithClientConfig(String, IClientConfig)}
 * which in turn uses reflection to initialize the filter instance. 
 * The filter class name is determined by the value of {@link CommonClientConfigKey#NIWSServerListFilterClassName}
 * in the {@link IClientConfig}. The default implementation is {@link ZoneAffinityServerListFilter}.
 */
public AbstractServerListFilter<T> getFilterImpl(IClientConfig niwsClientConfig) throws ClientException {
    String niwsServerListFilterClassName = null;
    try {
        niwsServerListFilterClassName = niwsClientConfig.get(
                        CommonClientConfigKey.NIWSServerListFilterClassName,
                        ZoneAffinityServerListFilter.class.getName());

        AbstractServerListFilter<T> abstractNIWSServerListFilter = 
                (AbstractServerListFilter<T>) ClientFactory.instantiateInstanceWithClientConfig(niwsServerListFilterClassName, niwsClientConfig);
        return abstractNIWSServerListFilter;
    } catch (Throwable e) {
        throw new ClientException(
                ClientException.ErrorType.CONFIGURATION,
                "Unable to get an instance of CommonClientConfigKey.NIWSServerListFilterClassName. Configured class:"
                        + niwsServerListFilterClassName, e);
    }
}
 
Example #2
Source File: SecureGetTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testSunnyDayNoClientAuth() throws Exception{

	AbstractConfiguration cm = ConfigurationManager.getConfigInstance();

	String name = "GetPostSecureTest" + ".testSunnyDayNoClientAuth";

	String configPrefix = name + "." + "ribbon";

	cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsSecure, "true");
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.SecurePort, Integer.toString(testServer2.getPort()));
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsHostnameValidationRequired, "false");
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.TrustStore, FILE_TS2.getAbsolutePath());
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.TrustStorePassword, PASSWORD);

	RestClient rc = (RestClient) ClientFactory.getNamedClient(name);

	testServer2.accept();

	URI getUri = new URI(SERVICE_URI2 + "test/");
       HttpRequest request = HttpRequest.newBuilder().uri(getUri).queryParams("name", "test").build();
	HttpResponse response = rc.execute(request);
	assertEquals(200, response.getStatus());
}
 
Example #3
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testPostWithByteBuf() throws Exception {
    Person myPerson = new Person("netty", 5);
    ObjectMapper mapper = new ObjectMapper();
    byte[] raw = mapper.writeValueAsBytes(myPerson);
    ByteBuf buffer = Unpooled.copiedBuffer(raw);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost(SERVICE_URI + "testAsync/person")
            .withHeader("Content-type", "application/json")
            .withHeader("Content-length", String.valueOf(raw.length))
            .withContent(buffer);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(
            DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(CommonClientConfigKey.ReadTimeout, 10000));
    Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request);
    Person person = getPersonObservable(response).toBlocking().single();
    assertEquals(myPerson, person);
}
 
Example #4
Source File: RestClientTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testSecureClient2()  throws Exception {
    ConfigurationManager.getConfigInstance().setProperty("test3.ribbon." + CommonClientConfigKey.IsSecure, "true");
    ConfigurationManager.getConfigInstance().setProperty("test3.ribbon." + CommonClientConfigKey.TrustStore, secureServer.getTrustStore().getAbsolutePath());
    ConfigurationManager.getConfigInstance().setProperty("test3.ribbon." + CommonClientConfigKey.TrustStorePassword, SecureGetTest.PASSWORD);
    
    RestClient client = (RestClient) ClientFactory.getNamedClient("test3");
    BaseLoadBalancer lb = new BaseLoadBalancer();
    Server[] servers = new Server[]{new Server("localhost", secureServer.getServerPort())}; 
    lb.addServers(Arrays.asList(servers));
    client.setLoadBalancer(lb);
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
    HttpResponse response = client.executeWithLoadBalancer(request);
    assertStatusIsOk(response.getStatus());
    assertEquals(secureServer.getServerPath("/"), response.getRequestedURI().toString());
    
}
 
Example #5
Source File: ClientPropertiesProcessor.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Override
public void process(String groupName, GroupBuilder groupBuilder, RibbonResourceFactory resourceFactory, Class<?> interfaceClass) {
    ClientProperties properties = interfaceClass.getAnnotation(ClientProperties.class);
    if (properties != null) {
        IClientConfig config = resourceFactory.getClientConfigFactory().newConfig();
        for (Property prop : properties.properties()) {
            String name = prop.name();
            config.set(CommonClientConfigKey.valueOf(name), prop.value());
        }
        ClientOptions options = ClientOptions.from(config);
        groupBuilder.withClientOptions(options);
        if (properties.exportToArchaius()) {
            exportPropertiesToArchaius(groupName, config, interfaceClass.getName());
        }
    }
}
 
Example #6
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 #7
Source File: RetryTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testThrottledWithRetryNextServer() throws Exception {
    int connectionCount = connectionPoolManager.getConnectionsInPool();
    URI localUrl = new URI("/status?code=503");
    HttpRequest request = HttpRequest.newBuilder().uri(localUrl).build();
    try {
        client.executeWithLoadBalancer(request, DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.MaxAutoRetriesNextServer, 2));
        fail("Exception expected");
    } catch (ClientException e) { // NOPMD
    }
    assertEquals(3, lb.getLoadBalancerStats().getSingleServerStat(localServer).getSuccessiveConnectionFailureCount());
    System.out.println("Initial connections count " + connectionCount);
    System.out.println("Final connections count " + connectionPoolManager.getConnectionsInPool());
    // should be no connection leak        
    assertTrue(connectionPoolManager.getConnectionsInPool() <= connectionCount + 1);
}
 
Example #8
Source File: NamedConnectionPoolTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testConnectionPoolCleaner() throws Exception {
    // LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
    ConfigurationManager.getConfigInstance().setProperty("ConnectionPoolCleanerTest.ribbon." + CommonClientConfigKey.ConnIdleEvictTimeMilliSeconds, "100");
    ConfigurationManager.getConfigInstance().setProperty("ConnectionPoolCleanerTest.ribbon." + CommonClientConfigKey.ConnectionCleanerRepeatInterval, "500");
    RestClient client = (RestClient) ClientFactory.getNamedClient("ConnectionPoolCleanerTest");
    NFHttpClient httpclient = NFHttpClientFactory.getNamedNFHttpClient("ConnectionPoolCleanerTest");
    assertNotNull(httpclient);
    com.netflix.client.http.HttpResponse response = null;
    try {
        response = client.execute(HttpRequest.newBuilder().uri(server.getServerPath("/")).build());
    } finally {
        if (response != null) {
            response.close();
        }
    }
    MonitoredConnectionManager connectionPoolManager = (MonitoredConnectionManager) httpclient.getConnectionManager();
    Thread.sleep(2000);
    assertEquals(0, connectionPoolManager.getConnectionsInPool());
    client.shutdown();
}
 
Example #9
Source File: RetryTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Before
public void beforeTest() {
    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon.NFLoadBalancerClassName", BaseLoadBalancer.class.getName());
    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon.client.NFLoadBalancerPingClassName", DummyPing.class.getName());

    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon.ReadTimeout", "1000");
    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon." + CommonClientConfigKey.ConnectTimeout, "500");
    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon." + CommonClientConfigKey.OkToRetryOnAllOperations, "true");

    client = (RestClient) ClientFactory.getNamedClient("RetryTest");

    lb = (BaseLoadBalancer) client.getLoadBalancer();
    lb.setServersList(Lists.newArrayList(localServer));
    
    httpClient = NFHttpClientFactory.getNamedNFHttpClient("RetryTest");
    connectionPoolManager = (MonitoredConnectionManager) httpClient.getConnectionManager(); 
    
    client.setMaxAutoRetries(0);
    client.setMaxAutoRetriesNextServer(0);
    client.setOkToRetryOnAllOperations(false);
    lb.setServersList(Lists.newArrayList(localServer));
    // reset the server index
    lb.setRule(new AvailabilityFilteringRule());
    lb.getLoadBalancerStats().getSingleServerStat(localServer).clearSuccessiveConnectionFailureCount();
}
 
Example #10
Source File: LoadBalancerContext.java    From ribbon with Apache License 2.0 6 votes vote down vote up
/**
 * Set necessary parameters from client configuration and register with Servo monitors.
 */
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
    if (clientConfig == null) {
        return;    
    }
    clientName = clientConfig.getClientName();
    if (StringUtils.isEmpty(clientName)) {
        clientName = "default";
    }
    vipAddresses = clientConfig.resolveDeploymentContextbasedVipAddresses();
    maxAutoRetries = clientConfig.getOrDefault(CommonClientConfigKey.MaxAutoRetries);
    maxAutoRetriesNextServer = clientConfig.getOrDefault(CommonClientConfigKey.MaxAutoRetriesNextServer);
    okToRetryOnAllOperations = clientConfig.getOrDefault(CommonClientConfigKey.OkToRetryOnAllOperations);
    defaultRetryHandler = new DefaultLoadBalancerRetryHandler(clientConfig);
    
    tracer = getExecuteTracer();

    Monitors.registerObject("Client_" + clientName, this);
}
 
Example #11
Source File: LBClient.java    From feign with Apache License 2.0 6 votes vote down vote up
@Override
public RibbonResponse execute(RibbonRequest request, IClientConfig configOverride)
    throws IOException, ClientException {
  Request.Options options;
  if (configOverride != null) {
    options =
        new Request.Options(
            configOverride.get(CommonClientConfigKey.ConnectTimeout, connectTimeout),
            TimeUnit.MILLISECONDS,
            (configOverride.get(CommonClientConfigKey.ReadTimeout, readTimeout)),
            TimeUnit.MILLISECONDS,
            configOverride.get(CommonClientConfigKey.FollowRedirects, followRedirects));
  } else {
    options = new Request.Options(connectTimeout, TimeUnit.MILLISECONDS, readTimeout,
        TimeUnit.MILLISECONDS, true);
  }
  Response response = request.client().execute(request.toRequest(), options);
  if (retryableStatusCodes.contains(response.status())) {
    response.close();
    throw new ClientException(ClientException.ErrorType.SERVER_THROTTLED);
  }
  return new RibbonResponse(request.getUri(), response);
}
 
Example #12
Source File: StaticLoadBalancer.java    From suro with Apache License 2.0 6 votes vote down vote up
/**
   * @param config contains the server list, comma separated with the format
   *               hostname:port
   */
  @Inject
  public StaticLoadBalancer(ClientConfig config) {
      List<Server> serverList = new ArrayList<Server>();
      for (String s : config.getLoadBalancerServer().split(",")) {
          String[] host_port = s.split(":");
          serverList.add(new Server(host_port[0], Integer.parseInt(host_port[1])));
      }
      if (serverList.isEmpty()) {
          throw new IllegalArgumentException("empty server list");
      }

      IClientConfig loadBalancerConfig = new DefaultClientConfigImpl();
      loadBalancerConfig.loadProperties("suroClient");
loadBalancerConfig.setProperty(CommonClientConfigKey.NFLoadBalancerPingClassName, "com.netflix.suro.connection.SuroPing");
      super.initWithNiwsConfig(loadBalancerConfig);
      addServers(serverList);
  }
 
Example #13
Source File: RestClient.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Override
protected Pair<String, Integer> deriveSchemeAndPortFromPartialUri(URI uri) {
    boolean isSecure = ncc.get(CommonClientConfigKey.IsSecure, this.isSecure);
    String scheme = uri.getScheme();
    if (scheme != null) {
        isSecure = 	scheme.equalsIgnoreCase("https");
    }
    int port = uri.getPort();
    if (port < 0 && !isSecure){
        port = 80;
    } else if (port < 0 && isSecure){
        port = 443;
    }
    if (scheme == null){
        if (isSecure) {
            scheme = "https";
        } else {
            scheme = "http";
        }
    }
    return new Pair<>(scheme, port);
}
 
Example #14
Source File: RestClient.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
    super.initWithNiwsConfig(clientConfig);
    this.ncc = clientConfig;
    this.restClientName = ncc.getClientName();
    this.isSecure = ncc.get(CommonClientConfigKey.IsSecure, this.isSecure);
    this.isHostnameValidationRequired = ncc.get(CommonClientConfigKey.IsHostnameValidationRequired, this.isHostnameValidationRequired);
    this.isClientAuthRequired = ncc.get(CommonClientConfigKey.IsClientAuthRequired, this.isClientAuthRequired);
    this.bFollowRedirects = ncc.get(CommonClientConfigKey.FollowRedirects, true);
    this.ignoreUserToken = ncc.get(CommonClientConfigKey.IgnoreUserTokenInConnectionPoolForSecureClient, this.ignoreUserToken);

    this.config = new DefaultApacheHttpClient4Config();
    this.config.getProperties().put(
            ApacheHttpClient4Config.PROPERTY_CONNECT_TIMEOUT,
            ncc.get(CommonClientConfigKey.ConnectTimeout));
    this.config.getProperties().put(
            ApacheHttpClient4Config.PROPERTY_READ_TIMEOUT,
            ncc.get(CommonClientConfigKey.ReadTimeout));

    this.restClient = apacheHttpClientSpecificInitialization();
    this.setRetryHandler(new HttpClientLoadBalancerErrorHandler(ncc));
}
 
Example #15
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 #16
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 #17
Source File: RestClient.java    From s2g-zuul with MIT License 6 votes vote down vote up
@Override
protected Pair<String, Integer> deriveSchemeAndPortFromPartialUri(HttpRequest task) {
    URI theUrl = task.getUri();
    boolean isSecure = getBooleanFromConfig(task.getOverrideConfig(), CommonClientConfigKey.IsSecure, this.isSecure);
    String scheme = theUrl.getScheme();
    if (scheme != null) {
        isSecure = 	scheme.equalsIgnoreCase("https");
    }
    int port = theUrl.getPort();
    if (port < 0 && !isSecure){
        port = 80;
    } else if (port < 0 && isSecure){
        port = 443;
    }
    if (scheme == null){
        if (isSecure) {
            scheme = "https";
        } else {
            scheme = "http";
        }
    }
    return new Pair<String, Integer>(scheme, port);
}
 
Example #18
Source File: RestClient.java    From s2g-zuul with MIT License 6 votes vote down vote up
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
    super.initWithNiwsConfig(clientConfig);
    this.ncc = clientConfig;
    this.restClientName = ncc.getClientName();
    this.isSecure = getBooleanFromConfig(ncc, CommonClientConfigKey.IsSecure, this.isSecure);
    this.isHostnameValidationRequired = getBooleanFromConfig(ncc, CommonClientConfigKey.IsHostnameValidationRequired, this.isHostnameValidationRequired);
    this.isClientAuthRequired = getBooleanFromConfig(ncc, CommonClientConfigKey.IsClientAuthRequired, this.isClientAuthRequired);
    this.bFollowRedirects = getBooleanFromConfig(ncc, CommonClientConfigKey.FollowRedirects, true);
    this.ignoreUserToken = getBooleanFromConfig(ncc, CommonClientConfigKey.IgnoreUserTokenInConnectionPoolForSecureClient, this.ignoreUserToken);

    this.config = new DefaultApacheHttpClient4Config();
    this.config.getProperties().put(
            ApacheHttpClient4Config.PROPERTY_CONNECT_TIMEOUT,
            Integer.parseInt(String.valueOf(ncc.getProperty(CommonClientConfigKey.ConnectTimeout))));
    this.config.getProperties().put(
            ApacheHttpClient4Config.PROPERTY_READ_TIMEOUT,
            Integer.parseInt(String.valueOf(ncc.getProperty(CommonClientConfigKey.ReadTimeout))));

    this.restClient = apacheHttpClientSpecificInitialization();
    this.restClient.setFollowRedirects(bFollowRedirects);
}
 
Example #19
Source File: ExecutionContextListenerInvoker.java    From ribbon with Apache License 2.0 6 votes vote down vote up
private boolean isListenerDisabled(ExecutionListener<?, ?> listener) {
    if (clientConfig == null) {
        return false;
    } else {
        String className = listener.getClass().getName();
        IClientConfigKey key = classConfigKeyMap.get(className);
        if (key == null) {
            key = CommonClientConfigKey.valueOf("listener." + className + ".disabled");
            IClientConfigKey old = classConfigKeyMap.putIfAbsent(className, key);
            if (old != null) {
                key = old;
            }
        }
        return clientConfig.get(key, false);
    }
}
 
Example #20
Source File: BaseLoadBalancer.java    From ribbon with Apache License 2.0 5 votes vote down vote up
void initWithConfig(IClientConfig clientConfig, IRule rule, IPing ping, LoadBalancerStats stats) {
    this.config = clientConfig;
    this.name = clientConfig.getClientName();
    int pingIntervalTime = clientConfig.get(CommonClientConfigKey.NFLoadBalancerPingInterval, 30);
    int maxTotalPingTime = clientConfig.get(CommonClientConfigKey.NFLoadBalancerMaxTotalPingTime, 2);

    setPingInterval(pingIntervalTime);
    setMaxTotalPingTime(maxTotalPingTime);

    // cross associate with each other
    // i.e. Rule,Ping meet your container LB
    // LB, these are your Ping and Rule guys ...
    setRule(rule);
    setPing(ping);

    setLoadBalancerStats(stats);
    rule.setLoadBalancer(this);
    if (ping instanceof AbstractLoadBalancerPing) {
        ((AbstractLoadBalancerPing) ping).setLoadBalancer(this);
    }
    logger.info("Client: {} instantiated a LoadBalancer: {}", name, this);
    boolean enablePrimeConnections = clientConfig.getOrDefault(CommonClientConfigKey.EnablePrimeConnections);

    if (enablePrimeConnections) {
        this.setEnablePrimingConnections(true);
        PrimeConnections primeConnections = new PrimeConnections(
                this.getName(), clientConfig);
        this.setPrimeConnections(primeConnections);
    }
    init();

}
 
Example #21
Source File: ListenerTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testSuccessExecutionOnAbosoluteURI() throws IOException {
    MockWebServer server = new MockWebServer();
    String content = "OK";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json")
            .setBody(content));
    server.play();

    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "2000")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://localhost:" + server.getPort() + "/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + server.getPort());
    List<Server> servers = Lists.newArrayList(goodServer, badServer);

    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.ConnectTimeout, 500);
    TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig);
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    HttpClientResponse<ByteBuf> response = client.submit(request, null, overrideConfig).toBlocking().last();
    assertEquals(200, response.getStatus().code());
    assertEquals(1, listener.executionStartCounter.get());
    assertEquals(1, listener.startWithServerCounter.get());
    assertEquals(0, listener.exceptionWithServerCounter.get());
    assertEquals(0, listener.executionFailedCounter.get());
    assertEquals(1, listener.executionSuccessCounter.get());
    assertEquals(500, listener.getContext().getClientProperty(CommonClientConfigKey.ConnectTimeout).intValue());
    assertTrue(listener.isContextChecked());
    assertTrue(listener.isCheckExecutionInfo());
    assertSame(response, listener.getResponse());
}
 
Example #22
Source File: ListenerTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailedExecutionForAbsoluteURI() {
    IClientConfig config = DefaultClientConfigImpl
            .getClientConfigWithDefaultValues()
            .withProperty(CommonClientConfigKey.ConnectTimeout, "100")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://xyz.unknowhost.xyz/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server badServer2 = new Server("localhost:34567");
    List<Server> servers = Lists.newArrayList(badServer, badServer2);

    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
    TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig);
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    try {
        client.submit(request, null, overrideConfig).toBlocking().last();
        fail("Exception expected");
    } catch(Exception e) {
        assertNotNull(e);
    }
    assertEquals(1, listener.executionStartCounter.get());
    assertEquals(2, listener.startWithServerCounter.get());
    assertEquals(2, listener.exceptionWithServerCounter.get());
    assertEquals(1, listener.executionFailedCounter.get());
    assertTrue(listener.isContextChecked());
    assertTrue(listener.isCheckExecutionInfo());
    assertTrue(listener.getFinalThrowable() instanceof ClientException);
}
 
Example #23
Source File: RequestSpecificRetryHandler.java    From ribbon with Apache License 2.0 5 votes vote down vote up
public RequestSpecificRetryHandler(boolean okToRetryOnConnectErrors, boolean okToRetryOnAllErrors, RetryHandler baseRetryHandler, @Nullable IClientConfig requestConfig) {
    Preconditions.checkNotNull(baseRetryHandler);
    this.okToRetryOnConnectErrors = okToRetryOnConnectErrors;
    this.okToRetryOnAllErrors = okToRetryOnAllErrors;
    this.fallback = baseRetryHandler;
    if (requestConfig != null) {
        requestConfig.getIfSet(CommonClientConfigKey.MaxAutoRetries).ifPresent(
                value -> retrySameServer = value
        );
        requestConfig.getIfSet(CommonClientConfigKey.MaxAutoRetriesNextServer).ifPresent(
                value -> retryNextServer = value
        );
    }
}
 
Example #24
Source File: ClientFactory.java    From ribbon with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to create client and load balancer (if enabled in client config) given the name and client config.
 * Instances are created using reflection (see {@link #instantiateInstanceWithClientConfig(String, IClientConfig)}
 *
 * @param restClientName
 * @param clientConfig
 * @throws ClientException if any errors occurs in the process, or if the client with the same name already exists
 */
public static synchronized IClient<?, ?> registerClientFromProperties(String restClientName, IClientConfig clientConfig) throws ClientException { 
	IClient<?, ?> client = null;
	ILoadBalancer loadBalancer = null;
	if (simpleClientMap.get(restClientName) != null) {
		throw new ClientException(
				ClientException.ErrorType.GENERAL,
				"A Rest Client with this name is already registered. Please use a different name");
	}
	try {
		String clientClassName = clientConfig.getOrDefault(CommonClientConfigKey.ClientClassName);
		client = (IClient<?, ?>) instantiateInstanceWithClientConfig(clientClassName, clientConfig);
		boolean initializeNFLoadBalancer = clientConfig.getOrDefault(CommonClientConfigKey.InitializeNFLoadBalancer);
		if (initializeNFLoadBalancer) {
			loadBalancer = registerNamedLoadBalancerFromclientConfig(restClientName, clientConfig);
		}
		if (client instanceof AbstractLoadBalancerAwareClient) {
			((AbstractLoadBalancerAwareClient) client).setLoadBalancer(loadBalancer);
		}
	} catch (Throwable e) {
		String message = "Unable to InitializeAndAssociateNFLoadBalancer set for RestClient:"
				+ restClientName;
		logger.warn(message, e);
		throw new ClientException(ClientException.ErrorType.CONFIGURATION, 
				message, e);
	}
	simpleClientMap.put(restClientName, client);

	Monitors.registerObject("Client_" + restClientName, client);

	logger.info("Client Registered:" + client.toString());
	return client;
}
 
Example #25
Source File: AbstractLoadBalancerAwareClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Deprecated
protected boolean isRetriable(S request) {
    if (request.isRetriable()) {
        return true;            
    } else {
        boolean retryOkayOnOperation = okToRetryOnAllOperations;
        IClientConfig overriddenClientConfig = request.getOverrideConfig();
        if (overriddenClientConfig != null) {
            retryOkayOnOperation = overriddenClientConfig.get(CommonClientConfigKey.RequestSpecificRetryOn, okToRetryOnAllOperations);
        }
        return retryOkayOnOperation;
    }
}
 
Example #26
Source File: BaseLoadBalancer.java    From ribbon with Apache License 2.0 5 votes vote down vote up
private LoadBalancerStats createLoadBalancerStatsFromConfig(IClientConfig clientConfig, Factory factory) {
    String loadBalancerStatsClassName = clientConfig.getOrDefault(CommonClientConfigKey.NFLoadBalancerStatsClassName);
    try {
        return (LoadBalancerStats) factory.create(loadBalancerStatsClassName, clientConfig);
    } catch (Exception e) {
        throw new RuntimeException(
                "Error initializing configured LoadBalancerStats class - " + loadBalancerStatsClassName,
                e);
    }
}
 
Example #27
Source File: BaseLoadBalancer.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Override
public void initWithNiwsConfig(IClientConfig clientConfig, Factory factory) {
    String ruleClassName = clientConfig.getOrDefault(CommonClientConfigKey.NFLoadBalancerRuleClassName);
    String pingClassName = clientConfig.getOrDefault(CommonClientConfigKey.NFLoadBalancerPingClassName);
    try {
        IRule rule = (IRule)factory.create(ruleClassName, clientConfig);
        IPing ping = (IPing)factory.create(pingClassName, clientConfig);
        LoadBalancerStats stats = createLoadBalancerStatsFromConfig(clientConfig, factory);
        initWithConfig(clientConfig, rule, ping, stats);
    } catch (Exception e) {
        throw new RuntimeException("Error initializing load balancer", e);
    }
}
 
Example #28
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectTimeout() throws Exception {
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(
            DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1"));
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://www.google.com:81/");
    Observable<HttpClientResponse<ByteBuf>> observable = observableClient.submit(new Server("www.google.com", 81), request);
    
    ObserverWithLatch<HttpClientResponse<ByteBuf>> observer = new ObserverWithLatch<HttpClientResponse<ByteBuf>>();
    observable.subscribe(observer);
    observer.await();
    assertNotNull(observer.error);
    assertTrue(observer.error instanceof io.netty.channel.ConnectTimeoutException);
}
 
Example #29
Source File: LoadBalancerBuilder.java    From ribbon with Apache License 2.0 5 votes vote down vote up
/**
 * Build a load balancer using the configuration from the {@link IClientConfig} only. It uses reflection to initialize necessary load balancer
 * components. 
 */
public ILoadBalancer buildLoadBalancerFromConfigWithReflection() {
    String loadBalancerClassName = config.get(CommonClientConfigKey.NFLoadBalancerClassName);
    if (loadBalancerClassName == null) {
        throw new IllegalArgumentException("NFLoadBalancerClassName is not specified in the IClientConfig");
    }
    ILoadBalancer lb;
    try {
        lb = (ILoadBalancer) factory.create(loadBalancerClassName, config);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return lb;
}
 
Example #30
Source File: DynamicServerListLoadBalancer.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Override
public void initWithNiwsConfig(IClientConfig clientConfig, Factory factory) {
    try {
        super.initWithNiwsConfig(clientConfig, factory);
        String niwsServerListClassName = clientConfig.getOrDefault(CommonClientConfigKey.NIWSServerListClassName);

        ServerList<T> niwsServerListImpl = (ServerList<T>) factory.create(niwsServerListClassName, clientConfig);
        this.serverListImpl = niwsServerListImpl;

        if (niwsServerListImpl instanceof AbstractServerList) {
            AbstractServerListFilter<T> niwsFilter = ((AbstractServerList) niwsServerListImpl)
                    .getFilterImpl(clientConfig);
            niwsFilter.setLoadBalancerStats(getLoadBalancerStats());
            this.filter = niwsFilter;
        }

        String serverListUpdaterClassName = clientConfig.getOrDefault(
                CommonClientConfigKey.ServerListUpdaterClassName);

        this.serverListUpdater = (ServerListUpdater) factory.create(serverListUpdaterClassName, clientConfig);

        restOfInit(clientConfig);
    } catch (Exception e) {
        throw new RuntimeException(
                "Exception while initializing NIWSDiscoveryLoadBalancer:"
                        + clientConfig.getClientName()
                        + ", niwsClientConfig:" + clientConfig, e);
    }
}