Search in sources :

Example 6 with LoadBalancer

use of io.fabric8.gateway.loadbalancer.LoadBalancer in project fabric8 by jboss-fuse.

the class FabricMQGateway method addGateway.

protected TcpGateway addGateway(List<TcpGateway> gateways, Vertx vertx, ServiceMap serviceMap, String protocolName, boolean enabled, int listenPort, LoadBalancer pathLoadBalancer, LoadBalancer serviceLoadBalancer) {
    if (enabled) {
        TcpGatewayHandler handler = new TcpGatewayHandler(vertx, serviceMap, protocolName, pathLoadBalancer, serviceLoadBalancer);
        TcpGateway gateway = new TcpGateway(vertx, serviceMap, listenPort, protocolName, handler);
        if (Strings.isNotBlank(host)) {
            gateway.setHost(host);
        }
        gateways.add(gateway);
        return gateway;
    } else {
        return null;
    }
}
Also used : TcpGateway(io.fabric8.gateway.handlers.tcp.TcpGateway) TcpGatewayHandler(io.fabric8.gateway.handlers.tcp.TcpGatewayHandler)

Example 7 with LoadBalancer

use of io.fabric8.gateway.loadbalancer.LoadBalancer in project fabric8 by jboss-fuse.

the class FabricMQGateway method createListener.

protected GatewayServiceTreeCache createListener() {
    String zkPath = getZooKeeperPath();
    // TODO we should discover the broker group configuration here using the same
    // mq-create / mq-client profiles so that we only listen to a subset of the available brokers here?
    ServiceMap serviceMap = new ServiceMap();
    VertxService vertxService = getVertxService();
    Vertx vertx = vertxService.getVertx();
    CuratorFramework curator = getCurator();
    LoadBalancer pathLoadBalancer = LoadBalancers.createLoadBalancer(loadBalancerType, stickyLoadBalancerCacheSize);
    LoadBalancer serviceLoadBalancer = LoadBalancers.createLoadBalancer(loadBalancerType, stickyLoadBalancerCacheSize);
    LOG.info("activating MQ mapping ZooKeeper path: " + zkPath + " host: " + host + " with load balancer: " + pathLoadBalancer);
    List<TcpGateway> gateways = new ArrayList<TcpGateway>();
    addGateway(gateways, vertx, serviceMap, "tcp", isOpenWireEnabled(), getOpenWirePort(), pathLoadBalancer, serviceLoadBalancer);
    addGateway(gateways, vertx, serviceMap, "stomp", isStompEnabled(), getStompPort(), pathLoadBalancer, serviceLoadBalancer);
    addGateway(gateways, vertx, serviceMap, "amqp", isAmqpEnabled(), getAmqpPort(), pathLoadBalancer, serviceLoadBalancer);
    addGateway(gateways, vertx, serviceMap, "mqtt", isMqttEnabled(), getMqttPort(), pathLoadBalancer, serviceLoadBalancer);
    addGateway(gateways, vertx, serviceMap, "ws", isWebsocketEnabled(), getWebsocketPort(), pathLoadBalancer, serviceLoadBalancer);
    if (gateways.isEmpty()) {
        return null;
    }
    return new GatewayServiceTreeCache(curator, zkPath, serviceMap, gateways);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ServiceMap(io.fabric8.gateway.ServiceMap) TcpGateway(io.fabric8.gateway.handlers.tcp.TcpGateway) VertxService(io.fabric8.gateway.fabric.support.vertx.VertxService) ArrayList(java.util.ArrayList) LoadBalancer(io.fabric8.gateway.loadbalancer.LoadBalancer) Vertx(org.vertx.java.core.Vertx)

Example 8 with LoadBalancer

use of io.fabric8.gateway.loadbalancer.LoadBalancer in project fabric8 by jboss-fuse.

the class LoadBalancerTest method testRandomLoadBalancer.

@Test
public void testRandomLoadBalancer() throws Exception {
    LoadBalancer loadBalancer = new RandomLoadBalancer();
    assertLoadBalancerWorksOnEmptyOrSingletonServices(loadBalancer);
    List<String> results = performRequests(loadBalancer);
    Set<String> set = asSet(results);
    assertTrue("Should have most of the values but was: " + set, set.size() > 1);
}
Also used : RoundRobinLoadBalancer(io.fabric8.gateway.loadbalancer.RoundRobinLoadBalancer) StickyLoadBalancer(io.fabric8.gateway.loadbalancer.StickyLoadBalancer) RandomLoadBalancer(io.fabric8.gateway.loadbalancer.RandomLoadBalancer) LoadBalancer(io.fabric8.gateway.loadbalancer.LoadBalancer) RandomLoadBalancer(io.fabric8.gateway.loadbalancer.RandomLoadBalancer) Test(org.junit.Test)

Example 9 with LoadBalancer

use of io.fabric8.gateway.loadbalancer.LoadBalancer in project fabric8 by jboss-fuse.

the class LoadBalancerTest method testStickyLoadBalancer.

@Test
public void testStickyLoadBalancer() throws Exception {
    assertLoadBalancerWorksOnEmptyOrSingletonServices(new StickyLoadBalancer());
    LoadBalancer loadBalancer = new StickyLoadBalancer();
    Set<String> allRequests = new HashSet<String>();
    int numberOfClients = 10;
    for (int i = 0; i < numberOfClients; i++) {
        clientRequestKey = "newClient:" + i;
        List<String> results = performRequests(loadBalancer);
        Set<String> set = asSet(results);
        assertTrue("All values should be the same for client: " + clientRequestKey + " but got: " + set, set.size() == 1);
        allRequests.addAll(set);
    }
    // now we should have a reasonable number of different overall answers.
    assertTrue("Across " + numberOfClients + " we should have most of the values: " + allRequests, allRequests.size() > 1);
}
Also used : RoundRobinLoadBalancer(io.fabric8.gateway.loadbalancer.RoundRobinLoadBalancer) StickyLoadBalancer(io.fabric8.gateway.loadbalancer.StickyLoadBalancer) RandomLoadBalancer(io.fabric8.gateway.loadbalancer.RandomLoadBalancer) LoadBalancer(io.fabric8.gateway.loadbalancer.LoadBalancer) StickyLoadBalancer(io.fabric8.gateway.loadbalancer.StickyLoadBalancer) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with LoadBalancer

use of io.fabric8.gateway.loadbalancer.LoadBalancer in project fabric8 by jboss-fuse.

the class HttpMappingRuleBase method updateMappingRules.

/**
 * Given a path being added or removed, update the services.
 *
 * @param remove        whether to remove (if true) or add (if false) this mapping
 * @param path          the path that this mapping is bound
 * @param services      the HTTP URLs of the services to map to
 * @param defaultParams the default parameters to use in the URI templates such as for version and container
 * @param serviceDetails
 */
public void updateMappingRules(boolean remove, String path, List<String> services, Map<String, String> defaultParams, ServiceDetails serviceDetails) {
    SimplePathTemplate pathTemplate = getUriTemplate();
    if (pathTemplate != null) {
        boolean versionSpecificUri = pathTemplate.getParameterNames().contains("version");
        String versionId = defaultParams.get("version");
        if (!remove && Strings.isNotBlank(versionId) && !versionSpecificUri && gatewayVersion != null) {
            // lets ignore this mapping if the version does not match
            if (!gatewayVersion.equals(versionId)) {
                remove = true;
            }
        }
        Map<String, String> params = new HashMap<String, String>();
        if (defaultParams != null) {
            params.putAll(defaultParams);
        }
        params.put("servicePath", path);
        if (!versionSpecificUri && Strings.isNotBlank(this.enabledVersion)) {
            if (!serviceDetails.getVersion().equals(this.enabledVersion)) {
                remove = true;
            }
        }
        for (String service : services) {
            populateUrlParams(params, service);
            String fullPath = pathTemplate.bindByNameNonStrict(params);
            if (remove) {
                MappedServices rule = mappingRules.get(fullPath);
                if (rule != null) {
                    List<String> serviceUrls = rule.getServiceUrls();
                    serviceUrls.remove(service);
                    if (serviceUrls.isEmpty()) {
                        mappingRules.remove(fullPath);
                    }
                }
            } else {
                MappedServices mappedServices = new MappedServices(service, serviceDetails, loadBalancer, reverseHeaders);
                MappedServices oldRule = mappingRules.put(fullPath, mappedServices);
                if (oldRule != null) {
                    mappedServices.getServiceUrls().addAll(oldRule.getServiceUrls());
                }
            }
        }
    }
    fireMappingRulesChanged();
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MappedServices(io.fabric8.gateway.handlers.http.MappedServices) SimplePathTemplate(io.fabric8.zookeeper.internal.SimplePathTemplate)

Aggregations

LoadBalancer (io.fabric8.gateway.loadbalancer.LoadBalancer)14 RoundRobinLoadBalancer (io.fabric8.gateway.loadbalancer.RoundRobinLoadBalancer)8 ArrayList (java.util.ArrayList)7 InetSocketAddress (java.net.InetSocketAddress)5 AmqpProtocol (io.fabric8.gateway.handlers.detecting.protocol.amqp.AmqpProtocol)4 HttpProtocol (io.fabric8.gateway.handlers.detecting.protocol.http.HttpProtocol)4 MqttProtocol (io.fabric8.gateway.handlers.detecting.protocol.mqtt.MqttProtocol)4 OpenwireProtocol (io.fabric8.gateway.handlers.detecting.protocol.openwire.OpenwireProtocol)4 SslConfig (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslConfig)4 SslProtocol (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslProtocol)4 StompProtocol (io.fabric8.gateway.handlers.detecting.protocol.stomp.StompProtocol)4 MappedServices (io.fabric8.gateway.handlers.http.MappedServices)4 Service (io.fabric8.kubernetes.api.model.Service)4 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)4 Test (org.junit.Test)4 HttpGateway (io.fabric8.gateway.handlers.http.HttpGateway)3 HttpGatewayHandler (io.fabric8.gateway.handlers.http.HttpGatewayHandler)3 HttpGatewayServer (io.fabric8.gateway.handlers.http.HttpGatewayServer)3 HttpMappingRule (io.fabric8.gateway.handlers.http.HttpMappingRule)3 RandomLoadBalancer (io.fabric8.gateway.loadbalancer.RandomLoadBalancer)3