use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.
the class RibbonLoadBalancerClient method execute.
@Override
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
ILoadBalancer loadBalancer = getLoadBalancer(serviceId);
Server server = getServer(loadBalancer);
if (server == null) {
throw new IllegalStateException("No instances available for " + serviceId);
}
RibbonServer ribbonServer = new RibbonServer(serviceId, server, isSecure(server, serviceId), serverIntrospector(serviceId).getMetadata(server));
return execute(serviceId, ribbonServer, request);
}
use of com.netflix.loadbalancer.ILoadBalancer in project ribbon by Netflix.
the class LBBuilderTest method testBuildWithArchaiusProperties.
@Test
public void testBuildWithArchaiusProperties() {
Configuration config = ConfigurationManager.getConfigInstance();
config.setProperty("client1.niws.client." + Keys.DeploymentContextBasedVipAddresses, "dummy:7001");
config.setProperty("client1.niws.client." + Keys.InitializeNFLoadBalancer, "true");
config.setProperty("client1.niws.client." + Keys.NFLoadBalancerClassName, DynamicServerListLoadBalancer.class.getName());
config.setProperty("client1.niws.client." + Keys.NFLoadBalancerRuleClassName, RoundRobinRule.class.getName());
config.setProperty("client1.niws.client." + Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());
config.setProperty("client1.niws.client." + Keys.NIWSServerListFilterClassName, ZoneAffinityServerListFilter.class.getName());
config.setProperty("client1.niws.client." + Keys.ServerListUpdaterClassName, PollingServerListUpdater.class.getName());
IClientConfig clientConfig = IClientConfig.Builder.newBuilder(NiwsClientConfig.class, "client1").build();
ILoadBalancer lb = LoadBalancerBuilder.newBuilder().withClientConfig(clientConfig).buildLoadBalancerFromConfigWithReflection();
assertNotNull(lb);
assertEquals(DynamicServerListLoadBalancer.class.getName(), lb.getClass().getName());
DynamicServerListLoadBalancer<Server> dynamicLB = (DynamicServerListLoadBalancer<Server>) lb;
assertTrue(dynamicLB.getServerListUpdater() instanceof PollingServerListUpdater);
assertTrue(dynamicLB.getFilter() instanceof ZoneAffinityServerListFilter);
assertTrue(dynamicLB.getRule() instanceof RoundRobinRule);
assertTrue(dynamicLB.getPing() instanceof DummyPing);
assertEquals(Lists.newArrayList(expected), lb.getAllServers());
}
use of com.netflix.loadbalancer.ILoadBalancer in project ribbon by Netflix.
the class ClientFactory method registerClientFromProperties.
/**
* 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;
}
use of com.netflix.loadbalancer.ILoadBalancer in project ribbon by Netflix.
the class RestClient method shutdown.
public void shutdown() {
ILoadBalancer lb = this.getLoadBalancer();
if (lb instanceof BaseLoadBalancer) {
((BaseLoadBalancer) lb).shutdown();
}
NFHttpClientFactory.shutdownNFHttpClient(restClientName);
}
use of com.netflix.loadbalancer.ILoadBalancer in project ribbon by Netflix.
the class ClientFactory method registerNamedLoadBalancerFromclientConfig.
/**
* Create and register a load balancer with the name and given the class of configClass.
*
* @throws ClientException if load balancer with the same name already exists or any error occurs
* @see #instantiateInstanceWithClientConfig(String, IClientConfig)
*/
public static ILoadBalancer registerNamedLoadBalancerFromclientConfig(String name, IClientConfig clientConfig) throws ClientException {
if (namedLBMap.get(name) != null) {
throw new ClientException("LoadBalancer for name " + name + " already exists");
}
ILoadBalancer lb = null;
try {
String loadBalancerClassName = clientConfig.getOrDefault(CommonClientConfigKey.NFLoadBalancerClassName);
lb = (ILoadBalancer) ClientFactory.instantiateInstanceWithClientConfig(loadBalancerClassName, clientConfig);
namedLBMap.put(name, lb);
logger.info("Client: {} instantiated a LoadBalancer: {}", name, lb);
return lb;
} catch (Throwable e) {
throw new ClientException("Unable to instantiate/associate LoadBalancer with Client:" + name, e);
}
}
Aggregations