Search in sources :

Example 1 with ClientRequestFacade

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

the class HttpProxyRule method chooseBackEndService.

/**
 * Chooses a back end service from the set of destination templates
 */
public UriTemplateDefinition chooseBackEndService(ClientRequestFacade requestFacade) {
    LoadBalancer loadBalancer = getLoadBalancer().getLoadBalancer();
    List<UriTemplateDefinition> uriDefList = new ArrayList<UriTemplateDefinition>(destinationUriTemplates);
    return loadBalancer.choose(uriDefList, requestFacade);
}
Also used : ArrayList(java.util.ArrayList) LoadBalancer(io.fabric8.gateway.loadbalancer.LoadBalancer)

Example 2 with ClientRequestFacade

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

the class DetectingGateway method route.

public void route(final SocketWrapper socket, ConnectionParameters params, final Buffer received) {
    NetClient client = null;
    if (params.protocolVirtualHost == null) {
        params.protocolVirtualHost = defaultVirtualHost;
    }
    HashSet<String> schemes = new HashSet<String>(Arrays.asList(params.protocolSchemes));
    if (params.protocolVirtualHost != null) {
        List<ServiceDetails> services = serviceMap.getServices(params.protocolVirtualHost);
        // Lets try again with the defaultVirtualHost
        if (services.isEmpty() && !params.protocolVirtualHost.equals(defaultVirtualHost)) {
            params.protocolVirtualHost = defaultVirtualHost;
            services = serviceMap.getServices(params.protocolVirtualHost);
        }
        LOG.debug(String.format("%d services match the virtual host", services.size()));
        if (!services.isEmpty()) {
            ClientRequestFacade clientRequestFacade = clientRequestFacadeFactory.create(socket, params);
            ServiceDetails serviceDetails = serviceLoadBalancer.choose(services, clientRequestFacade);
            if (serviceDetails != null) {
                List<String> urlStrings = serviceDetails.getServices();
                LOG.debug("Selected service exposes the following URLS: {}", urlStrings);
                for (String urlString : urlStrings) {
                    if (Strings.notEmpty(urlString)) {
                        // lets create a client for this request...
                        try {
                            URI uri = new URI(urlString);
                            // URL url = new URL(urlString);
                            String urlProtocol = uri.getScheme();
                            if (schemes.contains(urlProtocol)) {
                                if (!socket.remoteAddress().toString().equals(clientRequestFacade.getClientRequestKey())) {
                                    LOG.info(String.format("Connecting client from '%s' (with key '%s') requesting virtual host '%s' to '%s:%d' using the %s protocol", socket.remoteAddress(), clientRequestFacade.getClientRequestKey(), params.protocolVirtualHost, uri.getHost(), uri.getPort(), params.protocol));
                                } else {
                                    LOG.info(String.format("Connecting client from '%s' requesting virtual host '%s' to '%s:%d' using the %s protocol", socket.remoteAddress(), params.protocolVirtualHost, uri.getHost(), uri.getPort(), params.protocol));
                                }
                                client = createClient(params, socket, uri, received);
                                break;
                            }
                        } catch (URISyntaxException e) {
                            LOG.warn("Failed to parse URI: " + urlString + ". " + e, e);
                        }
                    }
                }
            }
        }
    }
    if (client == null) {
        // failed to route
        handleConnectFailure(socket, String.format("No endpoint available for virtual host '%s' and protocol %s", params.protocolVirtualHost, params.protocol));
    }
}
Also used : NetClient(org.vertx.java.core.net.NetClient) ServiceDetails(io.fabric8.gateway.ServiceDetails) ClientRequestFacade(io.fabric8.gateway.loadbalancer.ClientRequestFacade) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

ServiceDetails (io.fabric8.gateway.ServiceDetails)1 ClientRequestFacade (io.fabric8.gateway.loadbalancer.ClientRequestFacade)1 LoadBalancer (io.fabric8.gateway.loadbalancer.LoadBalancer)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 NetClient (org.vertx.java.core.net.NetClient)1