Search in sources :

Example 16 with Invocation

use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class TestConfiguration method testConfiguration.

@Test
public void testConfiguration() {
    assertNotNull(Configuration.INSTANCE);
    assertEquals("returnnull", Configuration.FALLBACKPOLICY_POLICY_RETURN);
    assertEquals("throwexception", Configuration.FALLBACKPOLICY_POLICY_THROW);
    Configuration c = Configuration.INSTANCE;
    Invocation invocation = Mockito.mock(Invocation.class);
    String test2 = invocation.getMicroserviceName();
    Mockito.when(invocation.getOperationMeta()).thenReturn(Mockito.mock(OperationMeta.class));
    Mockito.when(invocation.getOperationMeta().getMicroserviceName()).thenReturn("testqualify");
    int res = c.getIsolationTimeoutInMilliseconds("groupname", test2, "testqualify");
    assertEquals(30000, res);
    boolean b1 = c.getIsolationTimeoutEnabled("groupname", test2, "testqualify");
    assertFalse(b1);
    int res1 = c.getIsolationMaxConcurrentRequests("groupname", test2, "testqualify");
    assertEquals(10, res1);
    boolean b2 = c.isCircuitBreakerEnabled("groupname", test2, "testqualify");
    assertTrue(b2);
    String str = c.getFallbackPolicyPolicy("groupname", test2, "testqualify");
    assertEquals("throwexception", str);
}
Also used : Invocation(io.servicecomb.core.Invocation) OperationMeta(io.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 17 with Invocation

use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class GrpcCodec method decodeRequest.

public static Invocation decodeRequest(RoutingContext routingContext, OperationMeta operationMeta) throws Exception {
    OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(operationMeta);
    Buffer bodyBuffer = routingContext.getBody();
    Buffer protoBuffer = bodyBuffer.slice(1, bodyBuffer.length());
    Object[] args = operationProtobuf.getRequestSchema().readObject(protoBuffer);
    Invocation invocation = InvocationFactory.forProvider(grpcTransport.getEndpoint(), operationMeta, args);
    String strContext = routingContext.request().getHeader(Const.CSE_CONTEXT);
    @SuppressWarnings("unchecked") Map<String, String> context = JsonUtils.readValue(strContext.getBytes(StandardCharsets.UTF_8), Map.class);
    invocation.setContext(context);
    return invocation;
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) Buffer(io.vertx.core.buffer.Buffer) Invocation(io.servicecomb.core.Invocation) OperationProtobuf(io.servicecomb.codec.protobuf.definition.OperationProtobuf)

Example 18 with Invocation

use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class GrpcServerInvoke method runInExecutor.

public void runInExecutor() {
    try {
        Invocation invocation = GrpcCodec.decodeRequest(routingContext, operationMeta);
        invocation.next(response -> {
            onProviderResponse(invocation, response);
        });
    } catch (Throwable e) {
        LOGGER.error("grpc server onrequest error", e);
        sendFailResponse(e);
    }
}
Also used : Invocation(io.servicecomb.core.Invocation)

Example 19 with Invocation

use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class GrpcTransport method send.

@Override
public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    HttpClientWithContext httpClientWithContext = clientMgr.findThreadBindClientPool();
    OperationMeta operationMeta = invocation.getOperationMeta();
    OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(operationMeta);
    String cseContext = JsonUtils.writeValueAsString(invocation.getContext());
    // 在verticle之外的线程调用
    IpPort ipPort = (IpPort) invocation.getEndpoint().getAddress();
    Buffer requestBuf = GrpcCodec.encodeRequest(invocation, operationProtobuf);
    String url = "/" + invocation.getSchemaId() + "/" + operationMeta.getOperationId();
    Handler<HttpClientResponse> responseHandler = httpResponse -> {
        httpResponse.bodyHandler(responseBuf -> {
            invocation.getResponseExecutor().execute(() -> {
                try {
                    Response response = GrpcCodec.decodeResponse(invocation, operationProtobuf, httpResponse, responseBuf);
                    ResponseMeta responseMeta = operationMeta.findResponseMeta(response.getStatusCode());
                    for (String headerName : responseMeta.getHeaders().keySet()) {
                        for (String value : httpResponse.headers().getAll(headerName)) {
                            response.getHeaders().addHeader(headerName, value);
                        }
                    }
                    asyncResp.complete(response);
                } catch (Exception e) {
                    asyncResp.fail(invocation.getInvocationType(), e);
                }
            });
        });
    };
    // 从业务线程转移到网络线程中去发送
    httpClientWithContext.runOnContext(httpClient -> {
        HttpClientRequest httpClientRequest = httpClient.post(ipPort.getPort(), ipPort.getHostOrIp(), url, responseHandler);
        httpClientRequest.exceptionHandler(e -> {
            asyncResp.fail(invocation.getInvocationType(), e);
        });
        httpClientRequest.setTimeout(AbstractTransport.getRequestTimeout());
        httpClientRequest.putHeader(HEADER_CONTENT_TYPE, "application/grpc").putHeader(HEADER_TE, "trailers").putHeader(HEADER_USER_AGENT, "cse-client/1.0.0").putHeader(Const.CSE_CONTEXT, cseContext).putHeader(Const.DEST_MICROSERVICE, invocation.getMicroserviceName()).end(requestBuf);
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) JsonUtils(io.servicecomb.foundation.common.utils.JsonUtils) OperationMeta(io.servicecomb.core.definition.OperationMeta) OperationProtobuf(io.servicecomb.codec.protobuf.definition.OperationProtobuf) IpPort(io.servicecomb.foundation.common.net.IpPort) ResponseMeta(io.servicecomb.swagger.invocation.response.ResponseMeta) ProtobufManager(io.servicecomb.codec.protobuf.definition.ProtobufManager) JksOptions(io.vertx.core.net.JksOptions) HttpClientWithContext(io.servicecomb.foundation.vertx.client.http.HttpClientWithContext) AsyncResponse(io.servicecomb.core.AsyncResponse) Const(io.servicecomb.core.Const) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Component(org.springframework.stereotype.Component) VertxUtils(io.servicecomb.foundation.vertx.VertxUtils) Buffer(io.vertx.core.buffer.Buffer) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpVersion(io.vertx.core.http.HttpVersion) AbstractTransport(io.servicecomb.core.transport.AbstractTransport) Response(io.servicecomb.core.Response) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Handler(io.vertx.core.Handler) ClientPoolManager(io.servicecomb.foundation.vertx.client.ClientPoolManager) Invocation(io.servicecomb.core.Invocation) IpPort(io.servicecomb.foundation.common.net.IpPort) AsyncResponse(io.servicecomb.core.AsyncResponse) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Response(io.servicecomb.core.Response) HttpClientRequest(io.vertx.core.http.HttpClientRequest) OperationProtobuf(io.servicecomb.codec.protobuf.definition.OperationProtobuf) ResponseMeta(io.servicecomb.swagger.invocation.response.ResponseMeta) HttpClientResponse(io.vertx.core.http.HttpClientResponse) HttpClientWithContext(io.servicecomb.foundation.vertx.client.http.HttpClientWithContext) OperationMeta(io.servicecomb.core.definition.OperationMeta)

Example 20 with Invocation

use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class TestHighwayCodec method testDecodeRequest.

@Test
public void testDecodeRequest() {
    boolean status = true;
    try {
        commonMock();
        Invocation inv = HighwayCodec.decodeRequest(header, operationProtobuf, bodyBuffer);
        Assert.assertNotNull(inv);
    } catch (Exception e) {
        status = false;
    }
    Assert.assertTrue(status);
}
Also used : Invocation(io.servicecomb.core.Invocation) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Invocation (io.servicecomb.core.Invocation)70 Test (org.junit.Test)55 OperationMeta (io.servicecomb.core.definition.OperationMeta)29 Response (io.servicecomb.core.Response)20 AsyncResponse (io.servicecomb.core.AsyncResponse)19 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)10 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)9 OperationProtobuf (io.servicecomb.codec.protobuf.definition.OperationProtobuf)9 WrapSchema (io.servicecomb.codec.protobuf.utils.WrapSchema)9 MockUp (mockit.MockUp)9 InvocationException (io.servicecomb.core.exception.InvocationException)8 Endpoint (io.servicecomb.core.Endpoint)7 IpPort (io.servicecomb.foundation.common.net.IpPort)6 HttpClientRequest (io.vertx.core.http.HttpClientRequest)6 HttpClientResponse (io.vertx.core.http.HttpClientResponse)6 URLPathBuilder (io.servicecomb.common.rest.definition.path.URLPathBuilder)5 Buffer (io.vertx.core.buffer.Buffer)5 HttpClient (io.vertx.core.http.HttpClient)4 ArrayList (java.util.ArrayList)4 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)3