use of io.servicecomb.codec.protobuf.definition.OperationProtobuf 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;
}
use of io.servicecomb.codec.protobuf.definition.OperationProtobuf 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);
});
}
use of io.servicecomb.codec.protobuf.definition.OperationProtobuf in project java-chassis by ServiceComb.
the class HighwayClient method send.
public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
TcpClientPool tcpClientPool = clientMgr.findThreadBindClientPool();
OperationMeta operationMeta = invocation.getOperationMeta();
OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(operationMeta);
TcpOutputStream os = HighwayCodec.encodeRequest(invocation, operationProtobuf);
tcpClientPool.send(invocation.getEndpoint().getEndpoint(), os, ar -> {
invocation.getResponseExecutor().execute(() -> {
if (ar.failed()) {
asyncResp.consumerFail(ar.cause());
return;
}
try {
Response response = HighwayCodec.decodeResponse(invocation, operationProtobuf, ar.result());
asyncResp.complete(response);
} catch (Throwable e) {
asyncResp.consumerFail(e);
}
});
});
}
use of io.servicecomb.codec.protobuf.definition.OperationProtobuf in project java-chassis by ServiceComb.
the class MockUtil method mockMicroserviceMetaManager.
public void mockMicroserviceMetaManager() {
new MockUp<MicroserviceMetaManager>() {
@Mock
public SchemaMeta ensureFindSchemaMeta(String schemaId) {
SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
Mockito.when(schemaMeta.ensureFindOperation(null)).thenReturn(operationMeta);
Method method = this.getClass().getMethods()[0];
Mockito.when(operationMeta.getMethod()).thenReturn(method);
OperationProtobuf operationProtobuf = Mockito.mock(OperationProtobuf.class);
Mockito.when(operationMeta.getExtData("protobuf")).thenReturn(operationProtobuf);
Executor lExecutor = Mockito.mock(Executor.class);
Mockito.when(operationMeta.getExecutor()).thenReturn(lExecutor);
return schemaMeta;
}
};
}
use of io.servicecomb.codec.protobuf.definition.OperationProtobuf in project java-chassis by ServiceComb.
the class TestGrpcCodec method testDecodeResponse.
@Test
public void testDecodeResponse() {
boolean status = false;
try {
RoutingContext routingContext = Mockito.mock(RoutingContext.class);
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
OperationProtobuf operationProtobuf = Mockito.mock(OperationProtobuf.class);
WrapSchema schema = ProtobufSchemaUtils.getOrCreateSchema(int.class);
Mockito.when(operationProtobuf.getRequestSchema()).thenReturn(schema);
Mockito.when(operationMeta.getExtData("protobuf")).thenReturn(operationProtobuf);
Buffer bodyBuffer = Mockito.mock(Buffer.class);
Mockito.when(routingContext.getBody()).thenReturn(bodyBuffer);
HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
Mockito.when(routingContext.request()).thenReturn(request);
Mockito.when(request.getHeader(Const.CSE_CONTEXT)).thenReturn("{\"name\":\"test\"}");
Invocation invocation = Mockito.mock(Invocation.class);
Mockito.when(operationProtobuf.getResponseSchema()).thenReturn(Mockito.mock(WrapSchema.class));
HttpClientResponse httpResponse = Mockito.mock(HttpClientResponse.class);
Buffer buffer = Mockito.mock(Buffer.class);
GrpcCodec.decodeResponse(invocation, operationProtobuf, httpResponse, buffer);
} catch (Exception e) {
status = true;
}
Assert.assertFalse(status);
}
Aggregations