Java Code Examples for com.netflix.client.config.IClientConfig#getOrDefault()

The following examples show how to use com.netflix.client.config.IClientConfig#getOrDefault() . 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: 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 2
Source File: AcceptAllSocketFactory.java    From ribbon with Apache License 2.0 5 votes vote down vote up
/**
 * In the case of this factory the intent is to ensure that a truststore is not set,
 * as this does not make sense in the context of an accept-all policy
 */
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
    if (clientConfig == null) {
        return;
    }

    if (clientConfig.getOrDefault(CommonClientConfigKey.TrustStore) != null) {
        throw new IllegalArgumentException("Client configured with an AcceptAllSocketFactory cannot utilize a truststore");
    }
}
 
Example 3
Source File: ZoneAffinityServerListFilter.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Override
public void initWithNiwsConfig(IClientConfig niwsClientConfig) {
    zoneAffinity = niwsClientConfig.getOrDefault(CommonClientConfigKey.EnableZoneAffinity);
    zoneExclusive = niwsClientConfig.getOrDefault(CommonClientConfigKey.EnableZoneExclusivity);
    zone = niwsClientConfig.getGlobalProperty(ZONE).getOrDefault();
    zoneAffinityPredicate = new ZoneAffinityPredicate(zone);

    activeReqeustsPerServerThreshold = niwsClientConfig.getDynamicProperty(MAX_LOAD_PER_SERVER);
    blackOutServerPercentageThreshold = niwsClientConfig.getDynamicProperty(MAX_BLACKOUT_SERVER_PERCENTAGE);
    availableServersThreshold = niwsClientConfig.getDynamicProperty(MIN_AVAILABLE_SERVERS);

    overrideCounter = Monitors.newCounter("ZoneAffinity_OverrideCounter");

    Monitors.registerObject("NIWSServerListFilter_" + niwsClientConfig.getClientName());
}
 
Example 4
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);
    }
}
 
Example 5
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 6
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 7
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 8
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 9
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 10
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 11
Source File: DefaultLoadBalancerRetryHandler.java    From ribbon with Apache License 2.0 4 votes vote down vote up
public DefaultLoadBalancerRetryHandler(IClientConfig clientConfig) {
    this.retrySameServer = clientConfig.getOrDefault(CommonClientConfigKey.MaxAutoRetries);
    this.retryNextServer = clientConfig.getOrDefault(CommonClientConfigKey.MaxAutoRetriesNextServer);
    this.retryEnabled = clientConfig.getOrDefault(CommonClientConfigKey.OkToRetryOnAllOperations);
}