Search in sources :

Example 11 with ILoadBalancer

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);
}
Also used : Server(com.netflix.loadbalancer.Server) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer)

Example 12 with ILoadBalancer

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());
}
Also used : Configuration(org.apache.commons.configuration.Configuration) Server(com.netflix.loadbalancer.Server) PollingServerListUpdater(com.netflix.loadbalancer.PollingServerListUpdater) DynamicServerListLoadBalancer(com.netflix.loadbalancer.DynamicServerListLoadBalancer) ZoneAffinityServerListFilter(com.netflix.loadbalancer.ZoneAffinityServerListFilter) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) DummyPing(com.netflix.loadbalancer.DummyPing) IClientConfig(com.netflix.client.config.IClientConfig) RoundRobinRule(com.netflix.loadbalancer.RoundRobinRule) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with ILoadBalancer

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;
}
Also used : ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer)

Example 14 with ILoadBalancer

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);
}
Also used : ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer)

Example 15 with ILoadBalancer

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);
    }
}
Also used : ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer)

Aggregations

ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)25 Test (org.junit.Test)18 URI (java.net.URI)15 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)15 HttpMethod (org.springframework.http.HttpMethod)15 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)13 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)13 RequestConfig (org.apache.http.client.config.RequestConfig)12 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)11 StatusLine (org.apache.http.StatusLine)10 IOException (java.io.IOException)8 Server (com.netflix.loadbalancer.Server)6 ClientException (com.netflix.client.ClientException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)2 PollingServerListUpdater (com.netflix.loadbalancer.PollingServerListUpdater)2 ZoneAwareLoadBalancer (com.netflix.loadbalancer.ZoneAwareLoadBalancer)2 Locale (java.util.Locale)2 OkHttpClient (okhttp3.OkHttpClient)2