Search in sources :

Example 1 with LoadBalancer

use of org.apache.servicecomb.loadbalance.LoadBalancer in project java-chassis by ServiceComb.

the class CommonHttpEdgeDispatcher method onRequest.

protected void onRequest(RoutingContext context) {
    URLMappedConfigurationItem configurationItem = findConfigurationItem(context.request().uri());
    if (configurationItem == null) {
        context.next();
        return;
    }
    String uri = Utils.findActualPath(context.request().uri(), configurationItem.getPrefixSegmentCount());
    Invocation invocation = new Invocation() {

        @Override
        public String getConfigTransportName() {
            return "rest";
        }

        @Override
        public String getMicroserviceName() {
            return configurationItem.getMicroserviceName();
        }
    };
    LoadBalancer loadBalancer = getOrCreateLoadBalancer(invocation, configurationItem.getMicroserviceName(), configurationItem.getVersionRule());
    ServiceCombServer server = loadBalancer.chooseServer(invocation);
    if (server == null) {
        LOG.warn("no available server for service {}", configurationItem.getMicroserviceName());
        serverNotReadyResponse(context);
        return;
    }
    URIEndpointObject endpointObject = new URIEndpointObject(server.getEndpoint().getEndpoint());
    RequestOptions requestOptions = new RequestOptions();
    requestOptions.setHost(endpointObject.getHostOrIp()).setPort(endpointObject.getPort()).setSsl(endpointObject.isSslEnabled()).setMethod(context.request().method()).setURI(uri);
    HttpClient httpClient;
    if (endpointObject.isHttp2Enabled()) {
        httpClient = HttpClients.getClient(Http2TransportHttpClientOptionsSPI.CLIENT_NAME, false).getHttpClient();
    } else {
        httpClient = HttpClients.getClient(HttpTransportHttpClientOptionsSPI.CLIENT_NAME, false).getHttpClient();
    }
    context.request().pause();
    httpClient.request(requestOptions).compose(httpClientRequest -> {
        context.request().headers().forEach((header) -> httpClientRequest.headers().set(header.getKey(), header.getValue()));
        context.request().resume();
        context.request().handler(httpClientRequest::write);
        context.request().endHandler((v) -> httpClientRequest.end());
        return httpClientRequest.response().compose(httpClientResponse -> {
            context.response().setStatusCode(httpClientResponse.statusCode());
            httpClientResponse.headers().forEach((header) -> context.response().headers().set(header.getKey(), header.getValue()));
            httpClientResponse.handler(this.responseHandler(context));
            httpClientResponse.endHandler((v) -> context.response().end());
            return Future.succeededFuture();
        });
    }).onFailure(failure -> {
        LOG.warn("send request to target {}:{} failed, cause {}", endpointObject.getHostOrIp(), endpointObject.getPort(), failure.getMessage());
        serverNotReadyResponse(context);
    });
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) ServerDiscoveryFilter(org.apache.servicecomb.loadbalance.filter.ServerDiscoveryFilter) ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) HttpTransportHttpClientOptionsSPI(org.apache.servicecomb.transport.rest.client.HttpTransportHttpClientOptionsSPI) LoggerFactory(org.slf4j.LoggerFactory) Router(io.vertx.ext.web.Router) HashMap(java.util.HashMap) RequestOptions(io.vertx.core.http.RequestOptions) RoutingContext(io.vertx.ext.web.RoutingContext) HttpClients(org.apache.servicecomb.foundation.vertx.client.http.HttpClients) DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) Map(java.util.Map) LoadbalanceHandler(org.apache.servicecomb.loadbalance.LoadbalanceHandler) ConcurrentHashMapEx(org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx) RuleExt(org.apache.servicecomb.loadbalance.RuleExt) RegistrationManager(org.apache.servicecomb.registry.RegistrationManager) LoadBalancer(org.apache.servicecomb.loadbalance.LoadBalancer) Logger(org.slf4j.Logger) Http2TransportHttpClientOptionsSPI(org.apache.servicecomb.transport.rest.client.Http2TransportHttpClientOptionsSPI) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) Future(io.vertx.core.Future) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) Invocation(org.apache.servicecomb.core.Invocation) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration) Buffer(io.vertx.core.buffer.Buffer) DiscoveryTree(org.apache.servicecomb.registry.discovery.DiscoveryTree) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject) Handler(io.vertx.core.Handler) ExtensionsManager(org.apache.servicecomb.loadbalance.ExtensionsManager) HttpClient(io.vertx.core.http.HttpClient) Invocation(org.apache.servicecomb.core.Invocation) RequestOptions(io.vertx.core.http.RequestOptions) HttpClient(io.vertx.core.http.HttpClient) LoadBalancer(org.apache.servicecomb.loadbalance.LoadBalancer) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject)

Aggregations

ConcurrentCompositeConfiguration (com.netflix.config.ConcurrentCompositeConfiguration)1 DynamicPropertyFactory (com.netflix.config.DynamicPropertyFactory)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Buffer (io.vertx.core.buffer.Buffer)1 HttpClient (io.vertx.core.http.HttpClient)1 RequestOptions (io.vertx.core.http.RequestOptions)1 Router (io.vertx.ext.web.Router)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Invocation (org.apache.servicecomb.core.Invocation)1 VersionedCache (org.apache.servicecomb.foundation.common.cache.VersionedCache)1 ConcurrentHashMapEx (org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx)1 URIEndpointObject (org.apache.servicecomb.foundation.common.net.URIEndpointObject)1 HttpClients (org.apache.servicecomb.foundation.vertx.client.http.HttpClients)1 ExtensionsManager (org.apache.servicecomb.loadbalance.ExtensionsManager)1 LoadBalancer (org.apache.servicecomb.loadbalance.LoadBalancer)1 LoadbalanceHandler (org.apache.servicecomb.loadbalance.LoadbalanceHandler)1 RuleExt (org.apache.servicecomb.loadbalance.RuleExt)1