Search in sources :

Example 51 with URIEndpointObject

use of org.apache.servicecomb.foundation.common.net.URIEndpointObject in project incubator-servicecomb-java-chassis by apache.

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)

Example 52 with URIEndpointObject

use of org.apache.servicecomb.foundation.common.net.URIEndpointObject in project incubator-servicecomb-java-chassis by apache.

the class TestTcpServer method testTcpServerSSL.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testTcpServerSSL(@Mocked Vertx vertx, @Mocked AsyncResultCallback<InetSocketAddress> callback, @Mocked NetServer netServer) {
    new Expectations() {

        {
            vertx.createNetServer((NetServerOptions) any);
            result = netServer;
            netServer.connectHandler((Handler) any);
            netServer.listen(anyInt, anyString, (Handler) any);
        }
    };
    URIEndpointObject endpointObject = new URIEndpointObject("highway://127.0.0.1:6663?sslEnabled=true");
    TcpServer server = new TcpServerForTest(endpointObject);
    // assert done in Expectations
    server.init(vertx, "", callback);
}
Also used : Expectations(mockit.Expectations) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject) Test(org.junit.Test)

Example 53 with URIEndpointObject

use of org.apache.servicecomb.foundation.common.net.URIEndpointObject in project incubator-servicecomb-java-chassis by apache.

the class VertxRestTransport method canInit.

@Override
public boolean canInit() {
    setListenAddressWithoutSchema(TransportConfig.getAddress());
    URIEndpointObject ep = (URIEndpointObject) getEndpoint().getAddress();
    if (ep == null) {
        return true;
    }
    if (!NetUtils.canTcpListen(ep.getSocketAddress().getAddress(), ep.getPort())) {
        LOGGER.warn("Can not start VertxRestTransport, the port:{} may have been occupied. You can ignore this message if you are using a web container like tomcat.", ep.getPort());
        return false;
    }
    return true;
}
Also used : URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject)

Example 54 with URIEndpointObject

use of org.apache.servicecomb.foundation.common.net.URIEndpointObject in project incubator-servicecomb-java-chassis by apache.

the class TestRestServerVerticle method testRestServerVerticleWithHttp2.

@Test
public void testRestServerVerticleWithHttp2(@Mocked Transport transport, @Mocked Vertx vertx, @Mocked Context context, @Mocked JsonObject jsonObject, @Mocked Promise<Void> startPromise) {
    URIEndpointObject endpointObject = new URIEndpointObject("http://127.0.0.1:8080?protocol=http2");
    new Expectations() {

        {
            transport.parseAddress("http://127.0.0.1:8080?protocol=http2");
            result = endpointObject;
        }
    };
    Endpoint endpiont = new Endpoint(transport, "http://127.0.0.1:8080?protocol=http2");
    new Expectations() {

        {
            context.config();
            result = jsonObject;
            jsonObject.getValue(AbstractTransport.ENDPOINT_KEY);
            result = endpiont;
        }
    };
    RestServerVerticle server = new RestServerVerticle();
    boolean status = false;
    try {
        server.init(vertx, context);
        server.start(startPromise);
    } catch (Exception e) {
        status = true;
    }
    Assertions.assertFalse(status);
}
Also used : Expectations(mockit.Expectations) Endpoint(org.apache.servicecomb.core.Endpoint) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject) Test(org.junit.Test)

Example 55 with URIEndpointObject

use of org.apache.servicecomb.foundation.common.net.URIEndpointObject in project incubator-servicecomb-java-chassis by apache.

the class RemoteHostAccessItem method appendClientFormattedItem.

@Override
public void appendClientFormattedItem(InvocationFinishEvent clientLogEvent, StringBuilder builder) {
    Endpoint endpoint = clientLogEvent.getInvocation().getEndpoint();
    if (null == endpoint || null == endpoint.getAddress() || StringUtils.isEmpty(((URIEndpointObject) endpoint.getAddress()).getHostOrIp())) {
        builder.append(EMPTY_RESULT);
        return;
    }
    builder.append(((URIEndpointObject) endpoint.getAddress()).getHostOrIp());
}
Also used : Endpoint(org.apache.servicecomb.core.Endpoint) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject)

Aggregations

URIEndpointObject (org.apache.servicecomb.foundation.common.net.URIEndpointObject)82 Endpoint (org.apache.servicecomb.core.Endpoint)32 Test (org.junit.Test)31 Expectations (mockit.Expectations)26 Invocation (org.apache.servicecomb.core.Invocation)14 RequestOptions (io.vertx.core.http.RequestOptions)10 HttpClientWithContext (org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext)10 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)9 HttpMethod (io.vertx.core.http.HttpMethod)8 Handler (io.vertx.core.Handler)6 RoutingContext (io.vertx.ext.web.RoutingContext)6 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)6 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)5 VersionedCache (org.apache.servicecomb.foundation.common.cache.VersionedCache)5 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)5 Context (io.vertx.core.Context)4 HttpClient (io.vertx.core.http.HttpClient)4 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)4 ConcurrentCompositeConfiguration (com.netflix.config.ConcurrentCompositeConfiguration)3 DynamicPropertyFactory (com.netflix.config.DynamicPropertyFactory)3