Search in sources :

Example 6 with Payload

use of com.alibaba.nacos.api.grpc.auto.Payload in project nacos by alibaba.

the class GrpcUtils method convert.

/**
 * convert request to payload.
 *
 * @param request request.
 * @return payload.
 */
public static Payload convert(Request request) {
    Metadata newMeta = Metadata.newBuilder().setType(request.getClass().getSimpleName()).setClientIp(NetUtils.localIP()).putAllHeaders(request.getHeaders()).build();
    request.clearHeaders();
    String jsonString = toJson(request);
    Payload.Builder builder = Payload.newBuilder();
    return builder.setBody(Any.newBuilder().setValue(ByteString.copyFrom(jsonString, StandardCharsets.UTF_8))).setMetadata(newMeta).build();
}
Also used : Metadata(com.alibaba.nacos.api.grpc.auto.Metadata) Payload(com.alibaba.nacos.api.grpc.auto.Payload) ByteString(com.google.protobuf.ByteString)

Example 7 with Payload

use of com.alibaba.nacos.api.grpc.auto.Payload in project nacos by alibaba.

the class BaseGrpcServer method addServices.

private void addServices(MutableHandlerRegistry handlerRegistry, ServerInterceptor... serverInterceptor) {
    // unary common call register.
    final MethodDescriptor<Payload, Payload> unaryPayloadMethod = MethodDescriptor.<Payload, Payload>newBuilder().setType(MethodDescriptor.MethodType.UNARY).setFullMethodName(MethodDescriptor.generateFullMethodName(REQUEST_SERVICE_NAME, REQUEST_METHOD_NAME)).setRequestMarshaller(ProtoUtils.marshaller(Payload.getDefaultInstance())).setResponseMarshaller(ProtoUtils.marshaller(Payload.getDefaultInstance())).build();
    final ServerCallHandler<Payload, Payload> payloadHandler = ServerCalls.asyncUnaryCall((request, responseObserver) -> grpcCommonRequestAcceptor.request(request, responseObserver));
    final ServerServiceDefinition serviceDefOfUnaryPayload = ServerServiceDefinition.builder(REQUEST_SERVICE_NAME).addMethod(unaryPayloadMethod, payloadHandler).build();
    handlerRegistry.addService(ServerInterceptors.intercept(serviceDefOfUnaryPayload, serverInterceptor));
    // bi stream register.
    final ServerCallHandler<Payload, Payload> biStreamHandler = ServerCalls.asyncBidiStreamingCall((responseObserver) -> grpcBiStreamRequestAcceptor.requestBiStream(responseObserver));
    final MethodDescriptor<Payload, Payload> biStreamMethod = MethodDescriptor.<Payload, Payload>newBuilder().setType(MethodDescriptor.MethodType.BIDI_STREAMING).setFullMethodName(MethodDescriptor.generateFullMethodName(REQUEST_BI_STREAM_SERVICE_NAME, REQUEST_BI_STREAM_METHOD_NAME)).setRequestMarshaller(ProtoUtils.marshaller(Payload.newBuilder().build())).setResponseMarshaller(ProtoUtils.marshaller(Payload.getDefaultInstance())).build();
    final ServerServiceDefinition serviceDefOfBiStream = ServerServiceDefinition.builder(REQUEST_BI_STREAM_SERVICE_NAME).addMethod(biStreamMethod, biStreamHandler).build();
    handlerRegistry.addService(ServerInterceptors.intercept(serviceDefOfBiStream, serverInterceptor));
}
Also used : ServerServiceDefinition(io.grpc.ServerServiceDefinition) Payload(com.alibaba.nacos.api.grpc.auto.Payload)

Example 8 with Payload

use of com.alibaba.nacos.api.grpc.auto.Payload in project nacos by alibaba.

the class GrpcRequestAcceptorTest method testApplicationUnStarted.

@Test
public void testApplicationUnStarted() {
    RequestMeta metadata = new RequestMeta();
    metadata.setClientIp("127.0.0.1");
    metadata.setConnectionId(connectId);
    ServerCheckRequest serverCheckRequest = new ServerCheckRequest();
    serverCheckRequest.setRequestId(requestId);
    Payload request = GrpcUtils.convert(serverCheckRequest, metadata);
    StreamObserver<Payload> streamObserver = new StreamObserver<Payload>() {

        @Override
        public void onNext(Payload payload) {
            System.out.println("Receive data from server: " + payload);
            Object res = GrpcUtils.parse(payload);
            Assert.assertTrue(res instanceof ErrorResponse);
            ErrorResponse errorResponse = (ErrorResponse) res;
            Assert.assertEquals(errorResponse.getErrorCode(), NacosException.INVALID_SERVER_STATUS);
        }

        @Override
        public void onError(Throwable throwable) {
            Assert.fail(throwable.getMessage());
        }

        @Override
        public void onCompleted() {
            System.out.println("complete");
        }
    };
    streamStub.request(request, streamObserver);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCheckRequest(com.alibaba.nacos.api.remote.request.ServerCheckRequest) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) Payload(com.alibaba.nacos.api.grpc.auto.Payload) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) Test(org.junit.Test)

Example 9 with Payload

use of com.alibaba.nacos.api.grpc.auto.Payload in project nacos by alibaba.

the class GrpcRequestAcceptorTest method testNoRequestHandler.

@Test
public void testNoRequestHandler() {
    ApplicationUtils.setStarted(true);
    RequestMeta metadata = new RequestMeta();
    metadata.setClientIp("127.0.0.1");
    metadata.setConnectionId(connectId);
    InstanceRequest instanceRequest = new InstanceRequest();
    instanceRequest.setRequestId(requestId);
    Payload request = GrpcUtils.convert(instanceRequest, metadata);
    StreamObserver<Payload> streamObserver = new StreamObserver<Payload>() {

        @Override
        public void onNext(Payload payload) {
            System.out.println("Receive data from server: " + payload);
            Object res = GrpcUtils.parse(payload);
            Assert.assertTrue(res instanceof ErrorResponse);
            ErrorResponse errorResponse = (ErrorResponse) res;
            Assert.assertEquals(errorResponse.getErrorCode(), NacosException.NO_HANDLER);
        }

        @Override
        public void onError(Throwable throwable) {
            Assert.fail(throwable.getMessage());
        }

        @Override
        public void onCompleted() {
            System.out.println("complete");
        }
    };
    streamStub.request(request, streamObserver);
    ApplicationUtils.setStarted(false);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) Payload(com.alibaba.nacos.api.grpc.auto.Payload) InstanceRequest(com.alibaba.nacos.api.naming.remote.request.InstanceRequest) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) Test(org.junit.Test)

Example 10 with Payload

use of com.alibaba.nacos.api.grpc.auto.Payload in project nacos by alibaba.

the class GrpcRequestAcceptorTest method testHandleRequestSuccess.

@Test
public void testHandleRequestSuccess() {
    ApplicationUtils.setStarted(true);
    Mockito.when(requestHandlerRegistry.getByRequestType(Mockito.anyString())).thenReturn(mockHandler);
    Mockito.when(connectionManager.checkValid(Mockito.any())).thenReturn(true);
    String ip = "1.1.1.1";
    ConnectionMeta connectionMeta = new ConnectionMeta(connectId, ip, ip, 8888, 9848, "GRPC", "", "", new HashMap<>());
    Connection connection = new GrpcConnection(connectionMeta, null, null);
    Mockito.when(connectionManager.getConnection(Mockito.any())).thenReturn(connection);
    RequestMeta metadata = new RequestMeta();
    metadata.setClientIp("127.0.0.1");
    metadata.setConnectionId(connectId);
    HealthCheckRequest mockRequest = new HealthCheckRequest();
    Payload payload = GrpcUtils.convert(mockRequest, metadata);
    StreamObserver<Payload> streamObserver = new StreamObserver<Payload>() {

        @Override
        public void onNext(Payload payload) {
            System.out.println("Receive data from server: " + payload);
            Object res = GrpcUtils.parse(payload);
            Assert.assertTrue(res instanceof HealthCheckResponse);
        }

        @Override
        public void onError(Throwable throwable) {
            Assert.fail(throwable.getMessage());
        }

        @Override
        public void onCompleted() {
            System.out.println("complete");
        }
    };
    streamStub.request(payload, streamObserver);
    ApplicationUtils.setStarted(false);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) HealthCheckResponse(com.alibaba.nacos.api.remote.response.HealthCheckResponse) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) Connection(com.alibaba.nacos.core.remote.Connection) ConnectionMeta(com.alibaba.nacos.core.remote.ConnectionMeta) Payload(com.alibaba.nacos.api.grpc.auto.Payload) HealthCheckRequest(com.alibaba.nacos.api.remote.request.HealthCheckRequest) Test(org.junit.Test)

Aggregations

Payload (com.alibaba.nacos.api.grpc.auto.Payload)22 ErrorResponse (com.alibaba.nacos.api.remote.response.ErrorResponse)12 Response (com.alibaba.nacos.api.remote.response.Response)9 StreamObserver (io.grpc.stub.StreamObserver)9 NacosException (com.alibaba.nacos.api.exception.NacosException)8 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)8 Test (org.junit.Test)8 ServerCheckRequest (com.alibaba.nacos.api.remote.request.ServerCheckRequest)5 ServerCheckResponse (com.alibaba.nacos.api.remote.response.ServerCheckResponse)5 ConnectionSetupRequest (com.alibaba.nacos.api.remote.request.ConnectionSetupRequest)4 InstanceRequest (com.alibaba.nacos.api.naming.remote.request.InstanceRequest)3 Connection (com.alibaba.nacos.core.remote.Connection)3 Metadata (com.alibaba.nacos.api.grpc.auto.Metadata)2 ConnectResetRequest (com.alibaba.nacos.api.remote.request.ConnectResetRequest)2 Request (com.alibaba.nacos.api.remote.request.Request)2 ConnectionMeta (com.alibaba.nacos.core.remote.ConnectionMeta)2 ByteString (com.google.protobuf.ByteString)2 CancellationException (java.util.concurrent.CancellationException)2 TimeoutException (java.util.concurrent.TimeoutException)2 BiRequestStreamGrpc (com.alibaba.nacos.api.grpc.auto.BiRequestStreamGrpc)1