Search in sources :

Example 26 with URIEndpointObject

use of org.apache.servicecomb.foundation.common.net.URIEndpointObject in project 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 27 with URIEndpointObject

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

the class GateRestTemplate method getUrlPrefix.

private String getUrlPrefix(String gateName, String producerName, String schemaId) {
    MicroserviceVersionRule microserviceVersionRule = DiscoveryManager.INSTANCE.getAppManager().getOrCreateMicroserviceVersionRule(RegistrationManager.INSTANCE.getMicroservice().getAppId(), gateName, DefinitionConst.VERSION_RULE_ALL);
    MicroserviceInstance microserviceInstance = microserviceVersionRule.getInstances().values().stream().findFirst().get();
    URIEndpointObject edgeAddress = new URIEndpointObject(microserviceInstance.getEndpoints().get(0));
    String urlSchema = "http";
    if (edgeAddress.isSslEnabled()) {
        urlSchema = "https";
    }
    MicroserviceReferenceConfig microserviceReferenceConfig = SCBEngine.getInstance().createMicroserviceReferenceConfig(producerName);
    MicroserviceMeta microserviceMeta = microserviceReferenceConfig.getLatestMicroserviceMeta();
    SchemaMeta schemaMeta = microserviceMeta.ensureFindSchemaMeta(schemaId);
    return String.format("%s://%s:%d/rest/%s%s", urlSchema, edgeAddress.getHostOrIp(), edgeAddress.getPort(), producerName, schemaMeta.getSwagger().getBasePath());
}
Also used : MicroserviceVersionRule(org.apache.servicecomb.registry.consumer.MicroserviceVersionRule) MicroserviceReferenceConfig(org.apache.servicecomb.core.provider.consumer.MicroserviceReferenceConfig) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject)

Example 28 with URIEndpointObject

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

the class RemoteHostItemTest method initStrBuilder.

@Before
public void initStrBuilder() {
    routingContext = mock(RoutingContext.class);
    finishEvent = mock(InvocationFinishEvent.class);
    invocation = mock(Invocation.class);
    serverRequest = mock(HttpServerRequest.class);
    endpoint = mock(Endpoint.class);
    uriEndpointObject = mock(URIEndpointObject.class);
    socketAddress = mock(SocketAddress.class);
    accessLogEvent = new ServerAccessLogEvent();
    accessLogEvent.setRoutingContext(routingContext);
    strBuilder = new StringBuilder();
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) InvocationFinishEvent(org.apache.servicecomb.core.event.InvocationFinishEvent) ServerAccessLogEvent(org.apache.servicecomb.core.event.ServerAccessLogEvent) Invocation(org.apache.servicecomb.core.Invocation) Endpoint(org.apache.servicecomb.core.Endpoint) HttpServerRequest(io.vertx.core.http.HttpServerRequest) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject) SocketAddress(io.vertx.core.net.SocketAddress) Before(org.junit.Before)

Example 29 with URIEndpointObject

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

the class TestTcpServer method testTcpServerNonSSL.

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

        {
            vertx.createNetServer();
            result = netServer;
            netServer.connectHandler((Handler) any);
            netServer.listen(anyInt, anyString, (Handler) any);
        }
    };
    URIEndpointObject endpointObject = new URIEndpointObject("highway://127.0.0.1:6663");
    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 30 with URIEndpointObject

use of org.apache.servicecomb.foundation.common.net.URIEndpointObject in project 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)

Aggregations

URIEndpointObject (org.apache.servicecomb.foundation.common.net.URIEndpointObject)79 Endpoint (org.apache.servicecomb.core.Endpoint)29 Test (org.junit.Test)28 Expectations (mockit.Expectations)23 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