com.netflix.client.config.IClientConfigKey Java Examples

The following examples show how to use com.netflix.client.config.IClientConfigKey. 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: LoadBalancingHttpClient.java    From ribbon with Apache License 2.0 6 votes vote down vote up
protected LoadBalancingHttpClient(Builder<I, O> builder) {
    super(builder.lb, builder.config, new RequestSpecificRetryHandler(true, true, builder.retryHandler, null), builder.pipelineConfigurator, builder.poolCleanerScheduler);
    requestIdHeaderName = getProperty(IClientConfigKey.Keys.RequestIdHeaderName, null, null);
    requestIdProvider = (requestIdHeaderName != null) 
                      ? new HttpRequestIdProvider(requestIdHeaderName, RxContexts.DEFAULT_CORRELATOR)
                      : null;
    this.listeners = new CopyOnWriteArrayList<ExecutionListener<HttpClientRequest<I>, HttpClientResponse<O>>>(builder.listeners);
    defaultCommandBuilder = LoadBalancerCommand.<HttpClientResponse<O>>builder()
            .withLoadBalancerContext(lbContext)
            .withListeners(this.listeners)
            .withClientConfig(builder.config)
            .withRetryHandler(builder.retryHandler)
            .build();
    this.responseToErrorPolicy = builder.responseToErrorPolicy;
    this.backoffStrategy = builder.backoffStrategy;
}
 
Example #2
Source File: KubernetesConfigKey.java    From spring-cloud-kubernetes with Apache License 2.0 6 votes vote down vote up
public static IClientConfigKey valueOf(final String name) {
    for (IClientConfigKey key: keys()) {
        if (key.key().equals(name)) {
            return key;
        }
    }
    return new IClientConfigKey() {
        @Override
        public String key() {
            return name;
        }

        @Override
        public Class type() {
            return String.class;
        }
    };
}
 
Example #3
Source File: LoadBalancingTcpClient.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Override
protected RxClient<I, O> createRxClient(Server server) {
    ClientBuilder<I, O> builder = RxNetty.newTcpClientBuilder(server.getHost(), server.getPort());
    if (pipelineConfigurator != null) {
        builder.pipelineConfigurator(pipelineConfigurator);
    }
    Integer connectTimeout = getProperty(IClientConfigKey.Keys.ConnectTimeout, null, DefaultClientConfigImpl.DEFAULT_CONNECT_TIMEOUT);
    builder.channelOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
    if (isPoolEnabled()) {
        builder.withConnectionPoolLimitStrategy(poolStrategy)
        .withIdleConnectionsTimeoutMillis(idleConnectionEvictionMills)
        .withPoolIdleCleanupScheduler(poolCleanerScheduler);
    } else {
        builder.withNoConnectionPooling();
    }
    RxClient<I, O> client = builder.build();
    return client;
}
 
Example #4
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 #5
Source File: LoadBalancingRxClientWithPoolOptions.java    From ribbon with Apache License 2.0 6 votes vote down vote up
public LoadBalancingRxClientWithPoolOptions(ILoadBalancer lb, IClientConfig config,
        RetryHandler retryHandler,
        PipelineConfigurator<O, I> pipelineConfigurator, ScheduledExecutorService poolCleanerScheduler) {
    super(lb, config, retryHandler, pipelineConfigurator);
    poolEnabled = config.get(CommonClientConfigKey.EnableConnectionPool, 
            DefaultClientConfigImpl.DEFAULT_ENABLE_CONNECTION_POOL);
    if (poolEnabled) {
        this.poolCleanerScheduler = poolCleanerScheduler;
        int maxTotalConnections = config.get(IClientConfigKey.Keys.MaxTotalConnections,
                DefaultClientConfigImpl.DEFAULT_MAX_TOTAL_CONNECTIONS);
        int maxConnections = config.get(Keys.MaxConnectionsPerHost, DefaultClientConfigImpl.DEFAULT_MAX_CONNECTIONS_PER_HOST);
        MaxConnectionsBasedStrategy perHostStrategy = new DynamicPropertyBasedPoolStrategy(maxConnections,
                config.getClientName() + "." + config.getNameSpace() + "." + CommonClientConfigKey.MaxConnectionsPerHost);
        globalStrategy = new DynamicPropertyBasedPoolStrategy(maxTotalConnections, 
                config.getClientName() + "." + config.getNameSpace() + "." + CommonClientConfigKey.MaxTotalConnections);
        poolStrategy = new CompositePoolLimitDeterminationStrategy(perHostStrategy, globalStrategy);
        idleConnectionEvictionMills = config.get(Keys.ConnIdleEvictTimeMilliSeconds, DefaultClientConfigImpl.DEFAULT_CONNECTIONIDLE_TIME_IN_MSECS);
    }
}
 
Example #6
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 #7
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 #8
Source File: ClientOptions.java    From ribbon with Apache License 2.0 5 votes vote down vote up
public static ClientOptions from(IClientConfig config) {
    ClientOptions options = new ClientOptions();
    for (IClientConfigKey key: IClientConfigKey.Keys.keys()) {
        Object value = config.get(key);
        if (value != null) {
            options.options.put(key, value);
        }
    }
    return options;
}
 
Example #9
Source File: LoadBalancingHttpClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
/** 
 * Construct an RxClient.ClientConfig from an IClientConfig
 * 
 * @param requestConfig
 * @return
 */
private RxClient.ClientConfig getRxClientConfig(IClientConfig requestConfig) {
    if (requestConfig == null) {
        return DEFAULT_RX_CONFIG;
    }
    int requestReadTimeout = getProperty(IClientConfigKey.Keys.ReadTimeout, requestConfig, 
                                         DefaultClientConfigImpl.DEFAULT_READ_TIMEOUT);
    Boolean followRedirect = getProperty(IClientConfigKey.Keys.FollowRedirects, requestConfig, null);
    HttpClientConfig.Builder builder = new HttpClientConfig.Builder().readTimeout(requestReadTimeout, TimeUnit.MILLISECONDS);
    if (followRedirect != null) {
        builder.setFollowRedirect(followRedirect);
    }
    return builder.build();        
}
 
Example #10
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 #11
Source File: SSEClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Override
protected HttpClient<I, ServerSentEvent> getOrCreateRxClient(Server server) {
    HttpClientBuilder<I, ServerSentEvent> clientBuilder =
            new HttpClientBuilder<I, ServerSentEvent>(server.getHost(), server.getPort()).pipelineConfigurator(pipelineConfigurator);
    int requestConnectTimeout = getProperty(IClientConfigKey.Keys.ConnectTimeout, null, DefaultClientConfigImpl.DEFAULT_CONNECT_TIMEOUT);
    RxClient.ClientConfig rxClientConfig = new HttpClientConfig.Builder().build();
    
    HttpClient<I, ServerSentEvent> client = clientBuilder.channelOption(
            ChannelOption.CONNECT_TIMEOUT_MILLIS, requestConnectTimeout).config(rxClientConfig).build();
    return client;
}
 
Example #12
Source File: LoadBalancingRxClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
public LoadBalancingRxClient(ILoadBalancer lb, IClientConfig config, RetryHandler defaultRetryHandler, PipelineConfigurator<O, I> pipelineConfigurator) {
    this.rxClientCache = new ConcurrentHashMap<Server, T>();
    this.lbContext = new LoadBalancerContext(lb, config, defaultRetryHandler);
    this.defaultRetryHandler = defaultRetryHandler;
    this.pipelineConfigurator = pipelineConfigurator;
    this.clientConfig = config;
    this.listener = createListener(config.getClientName());
    
    eventSubject = new MetricEventsSubject<ClientMetricsEvent<?>>();
    boolean isSecure = getProperty(IClientConfigKey.Keys.IsSecure, null, false); 
    if (isSecure) {
        final URL trustStoreUrl = getResourceForOptionalProperty(CommonClientConfigKey.TrustStore);
        final URL keyStoreUrl = getResourceForOptionalProperty(CommonClientConfigKey.KeyStore);
        boolean isClientAuthRequired = clientConfig.get(IClientConfigKey.Keys.IsClientAuthRequired, false);
        if (    // if client auth is required, need both a truststore and a keystore to warrant configuring
                // if client is not is not required, we only need a keystore OR a truststore to warrant configuring
                (isClientAuthRequired && (trustStoreUrl != null && keyStoreUrl != null))
                ||
                (!isClientAuthRequired && (trustStoreUrl != null || keyStoreUrl != null))
                ) {

            try {
                sslContextFactory = new URLSslContextFactory(trustStoreUrl,
                        clientConfig.get(CommonClientConfigKey.TrustStorePassword),
                        keyStoreUrl,
                        clientConfig.get(CommonClientConfigKey.KeyStorePassword));

            } catch (ClientSslSocketFactoryException e) {
                throw new IllegalArgumentException("Unable to configure custom secure socket factory", e);
            }
        } else {
            sslContextFactory = null;
        }
    } else {
        sslContextFactory = null;
    }

    addLoadBalancerListener();
}
 
Example #13
Source File: LoadBalancingRxClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
public int getResponseTimeOut() {
    int maxRetryNextServer = 0;
    int maxRetrySameServer = 0;
    if (defaultRetryHandler != null) {
        maxRetryNextServer = defaultRetryHandler.getMaxRetriesOnNextServer();
        maxRetrySameServer = defaultRetryHandler.getMaxRetriesOnSameServer();
    } else {
        maxRetryNextServer = clientConfig.get(IClientConfigKey.Keys.MaxAutoRetriesNextServer, DefaultClientConfigImpl.DEFAULT_MAX_AUTO_RETRIES_NEXT_SERVER);
        maxRetrySameServer = clientConfig.get(IClientConfigKey.Keys.MaxAutoRetries, DefaultClientConfigImpl.DEFAULT_MAX_AUTO_RETRIES);
    }
    int readTimeout = getProperty(IClientConfigKey.Keys.ReadTimeout, null, DefaultClientConfigImpl.DEFAULT_READ_TIMEOUT);
    int connectTimeout = getProperty(IClientConfigKey.Keys.ConnectTimeout, null, DefaultClientConfigImpl.DEFAULT_CONNECT_TIMEOUT);
    return (maxRetryNextServer + 1) * (maxRetrySameServer + 1) * (readTimeout + connectTimeout);
}
 
Example #14
Source File: RestClient.java    From s2g-zuul with MIT License 5 votes vote down vote up
private URL getResourceForOptionalProperty(final IClientConfigKey configKey) {
    final String propValue = (String) ncc.getProperty(configKey);
    URL result = null;

    if (propValue != null) {
        result = getResource(propValue);
        if (result == null) {
            throw new IllegalArgumentException("No resource found for " + configKey + ": "
                    + propValue);
        }
    }
    return result;
}
 
Example #15
Source File: LoadBalancingRxClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
protected URL getResourceForOptionalProperty(final IClientConfigKey<String> configKey) {
    final String propValue = clientConfig.get(configKey);
    URL result = null;

    if (propValue != null) {
        result = Resources.getResource(propValue);
        if (result == null) {
            throw new IllegalArgumentException("No resource found for " + configKey + ": "
                    + propValue);
        }
    }
    return result;
}
 
Example #16
Source File: ResourceGroup.java    From ribbon with Apache License 2.0 5 votes vote down vote up
protected ResourceGroup(String name, ClientOptions options, ClientConfigFactory configFactory, RibbonTransportFactory transportFactory) {
    this.name = name;
    clientConfig = configFactory.newConfig();
    clientConfig.loadProperties(name);
    if (options != null) {
        for (IClientConfigKey key: options.getOptions().keySet()) {
            clientConfig.set(key, options.getOptions().get(key));
        }
    }
    this.configFactory = configFactory;
    this.transportFactory = transportFactory;
}
 
Example #17
Source File: LoadBalancerBuilder.java    From ribbon with Apache License 2.0 5 votes vote down vote up
private static ServerListUpdater createServerListUpdaterFromConfig(IClientConfig config, IClientConfigAware.Factory factory) {
    String serverListUpdaterClassName = config.getOrDefault(IClientConfigKey.Keys.ServerListUpdaterClassName);
    if (serverListUpdaterClassName == null) {
        throw new IllegalArgumentException("NIWSServerListClassName is not specified in the config");
    }
    ServerListUpdater updater;
    try {
        updater = (ServerListUpdater) factory.create(serverListUpdaterClassName, config);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return updater;
}
 
Example #18
Source File: ProxyEndpoint.java    From zuul with Apache License 2.0 5 votes vote down vote up
private byte[] preCacheBodyForRetryingSslRequests() {
    // Netty SSL handler clears body ByteBufs, so we need to cache the body if we want to retry POSTs
    if (ENABLE_CACHING_SSL_BODIES.get() && origin != null &&
            // only cache requests if already buffered
            origin.getClientConfig().get(IClientConfigKey.Keys.IsSecure, false) && zuulRequest.hasCompleteBody()) {
        return zuulRequest.getBody();
    }
    return null;
}
 
Example #19
Source File: ProxyEndpoint.java    From zuul with Apache License 2.0 5 votes vote down vote up
protected Integer getReadTimeout(IClientConfig requestConfig, int attemptNum) {
    Integer originTimeout = parseReadTimeout(origin.getClientConfig().getProperty(IClientConfigKey.Keys.ReadTimeout, null));
    Integer requestTimeout = parseReadTimeout(requestConfig.getProperty(IClientConfigKey.Keys.ReadTimeout, null));

    if (originTimeout == null && requestTimeout == null) {
        return MAX_OUTBOUND_READ_TIMEOUT.get();
    }
    else if (originTimeout == null || requestTimeout == null) {
        return originTimeout == null ? requestTimeout : originTimeout;
    }
    else {
        // return the greater of two timeouts
        return originTimeout > requestTimeout ? originTimeout : requestTimeout;
    }
}
 
Example #20
Source File: RestClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
private URL getResourceForOptionalProperty(final IClientConfigKey configKey) {
    final String propValue = (String) ncc.get(configKey);
    URL result = null;

    if (propValue != null) {
        result = getResource(propValue);
        if (result == null) {
            throw new IllegalArgumentException("No resource found for " + configKey + ": "
                    + propValue);
        }
    }
    return result;
}
 
Example #21
Source File: FollowRedirectTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testRedirectFollowed() throws Exception {
    IClientConfig config = DefaultClientConfigImpl
            .getClientConfigWithDefaultValues("myclient2")
            .set(IClientConfigKey.Keys.FollowRedirects, Boolean.TRUE);
    ClientFactory.registerClientFromProperties("myclient2", config);
    com.netflix.niws.client.http.RestClient client = (com.netflix.niws.client.http.RestClient) ClientFactory.getNamedClient("myclient2");
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("http://localhost:" + redirectingServer.getPort())).build();
    HttpResponse response = client.execute(request);
    assertEquals(200, response.getStatus());      
}
 
Example #22
Source File: ExecutionContext.java    From ribbon with Apache License 2.0 5 votes vote down vote up
public <S> S getClientProperty(IClientConfigKey<S> key) {
    S value;
    if (requestConfig != null) {
        value = requestConfig.get(key);
        if (value != null) {
            return value;
        }
    }
    value = clientConfig.get(key);
    return value;
}
 
Example #23
Source File: ExecutionContextListenerInvoker.java    From ribbon with Apache License 2.0 5 votes vote down vote up
public ExecutionContextListenerInvoker(ExecutionContext<I> context, List<ExecutionListener<I, O>> listeners, IClientConfig config) {
    this.listeners = Collections.unmodifiableList(listeners);
    this.context = context;
    this.clientConfig = config;
    if (clientConfig != null) {
        classConfigKeyMap = new ConcurrentHashMap<String, IClientConfigKey>();
    } else {
        classConfigKeyMap = null;
    }
}
 
Example #24
Source File: LoadBalancerBuilder.java    From ribbon with Apache License 2.0 5 votes vote down vote up
private static IRule createRuleFromConfig(IClientConfig config, IClientConfigAware.Factory factory) {
    String ruleClassName = config.getOrDefault(IClientConfigKey.Keys.NFLoadBalancerRuleClassName);
    if (ruleClassName == null) {
        throw new IllegalArgumentException("NFLoadBalancerRuleClassName is not specified in the config");
    }
    IRule rule;
    try {
        rule = (IRule) factory.create(ruleClassName, config);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return rule;
}
 
Example #25
Source File: LoadBalancerBuilder.java    From ribbon with Apache License 2.0 5 votes vote down vote up
private static ServerList<Server> createServerListFromConfig(IClientConfig config, IClientConfigAware.Factory factory) {
    String serverListClassName = config.get(IClientConfigKey.Keys.NIWSServerListClassName);
    if (serverListClassName == null) {
        throw new IllegalArgumentException("NIWSServerListClassName is not specified in the config");
    }
    ServerList<Server> list;
    try {
        list = (ServerList<Server>) factory.create(serverListClassName, config);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return list;
}
 
Example #26
Source File: RequestAttempt.java    From zuul with Apache License 2.0 4 votes vote down vote up
public RequestAttempt(final Server server, final IClientConfig clientConfig, int attemptNumber, int readTimeout) {
    this.status = -1;
    this.attempt = attemptNumber;
    this.readTimeout = readTimeout;

    if (server != null) {
        this.host = server.getHost();
        this.port = server.getPort();
        this.availabilityZone = server.getZone();

        if (server instanceof DiscoveryEnabledServer) {
            InstanceInfo instanceInfo = ((DiscoveryEnabledServer) server).getInstanceInfo();
            this.app = instanceInfo.getAppName().toLowerCase();
            this.asg = instanceInfo.getASGName();
            this.instanceId = instanceInfo.getInstanceId();
            this.host = instanceInfo.getHostName();
            this.port = instanceInfo.getPort();

            if (server.getPort() == instanceInfo.getSecurePort()) {
                this.vip = instanceInfo.getSecureVipAddress();
            }
            else {
                this.vip = instanceInfo.getVIPAddress();
            }
            if (instanceInfo.getDataCenterInfo() instanceof AmazonInfo) {
                this.availabilityZone = ((AmazonInfo) instanceInfo.getDataCenterInfo()).getMetadata().get("availability-zone");
            }
        }
        else {
            final Server.MetaInfo metaInfo = server.getMetaInfo();
            if (metaInfo != null) {
                this.asg = metaInfo.getServerGroup();
                this.vip = metaInfo.getServiceIdForDiscovery();
                this.instanceId = metaInfo.getInstanceId();
            }
        }
        // HACK - get region by just removing the last char from zone.
        if (availabilityZone != null && availabilityZone.length() > 0) {
            region = availabilityZone.substring(0, availabilityZone.length() - 1);
        }
    }

    if (clientConfig != null) {
        this.connectTimeout = clientConfig.get(IClientConfigKey.Keys.ConnectTimeout);
    }
}
 
Example #27
Source File: ConnectionPoolConfigImpl.java    From zuul with Apache License 2.0 4 votes vote down vote up
@Override
public int getConnectTimeout() {
    return clientConfig.getPropertyAsInteger(IClientConfigKey.Keys.ConnectTimeout, DEFAULT_CONNECT_TIMEOUT);
}
 
Example #28
Source File: ConnectionPoolConfigImpl.java    From zuul with Apache License 2.0 4 votes vote down vote up
@Override
public int maxConnectionsPerHost()
{
    return clientConfig.getPropertyAsInteger(IClientConfigKey.Keys.MaxConnectionsPerHost, DEFAULT_MAX_CONNS_PER_HOST);
}
 
Example #29
Source File: ConnectionPoolConfigImpl.java    From zuul with Apache License 2.0 4 votes vote down vote up
@Override
public int getIdleTimeout() {
    return clientConfig.getPropertyAsInteger(IClientConfigKey.Keys.ConnIdleEvictTimeMilliSeconds, DEFAULT_IDLE_TIMEOUT);
}
 
Example #30
Source File: ConnectionPoolConfigImpl.java    From zuul with Apache License 2.0 4 votes vote down vote up
@Override
public int getTcpReceiveBufferSize() {
    return clientConfig.getPropertyAsInteger(IClientConfigKey.Keys.ReceiveBufferSize, DEFAULT_BUFFER_SIZE);
}