Search in sources :

Example 1 with Payload

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

the class GrpcClient method connectToServer.

@Override
public Connection connectToServer(ServerInfo serverInfo) {
    try {
        if (grpcExecutor == null) {
            this.grpcExecutor = createGrpcExecutor(serverInfo.getServerIp());
        }
        int port = serverInfo.getServerPort() + rpcPortOffset();
        RequestGrpc.RequestFutureStub newChannelStubTemp = createNewChannelStub(serverInfo.getServerIp(), port);
        if (newChannelStubTemp != null) {
            Response response = serverCheck(serverInfo.getServerIp(), port, newChannelStubTemp);
            if (response == null || !(response instanceof ServerCheckResponse)) {
                shuntDownChannel((ManagedChannel) newChannelStubTemp.getChannel());
                return null;
            }
            BiRequestStreamGrpc.BiRequestStreamStub biRequestStreamStub = BiRequestStreamGrpc.newStub(newChannelStubTemp.getChannel());
            GrpcConnection grpcConn = new GrpcConnection(serverInfo, grpcExecutor);
            grpcConn.setConnectionId(((ServerCheckResponse) response).getConnectionId());
            // create stream request and bind connection event to this connection.
            StreamObserver<Payload> payloadStreamObserver = bindRequestStream(biRequestStreamStub, grpcConn);
            // stream observer to send response to server
            grpcConn.setPayloadStreamObserver(payloadStreamObserver);
            grpcConn.setGrpcFutureServiceStub(newChannelStubTemp);
            grpcConn.setChannel((ManagedChannel) newChannelStubTemp.getChannel());
            // send a  setup request.
            ConnectionSetupRequest conSetupRequest = new ConnectionSetupRequest();
            conSetupRequest.setClientVersion(VersionUtils.getFullClientVersion());
            conSetupRequest.setLabels(super.getLabels());
            conSetupRequest.setAbilities(super.clientAbilities);
            conSetupRequest.setTenant(super.getTenant());
            grpcConn.sendRequest(conSetupRequest);
            // wait to register connection setup
            Thread.sleep(100L);
            return grpcConn;
        }
        return null;
    } catch (Exception e) {
        LOGGER.error("[{}]Fail to connect to server!,error={}", GrpcClient.this.getName(), e);
    }
    return null;
}
Also used : ServerCheckResponse(com.alibaba.nacos.api.remote.response.ServerCheckResponse) Response(com.alibaba.nacos.api.remote.response.Response) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) ServerCheckResponse(com.alibaba.nacos.api.remote.response.ServerCheckResponse) BiRequestStreamGrpc(com.alibaba.nacos.api.grpc.auto.BiRequestStreamGrpc) Payload(com.alibaba.nacos.api.grpc.auto.Payload) ConnectionSetupRequest(com.alibaba.nacos.api.remote.request.ConnectionSetupRequest) RequestGrpc(com.alibaba.nacos.api.grpc.auto.RequestGrpc) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 2 with Payload

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

the class GrpcConnection method sendResponse.

public void sendResponse(Response response) {
    Payload convert = GrpcUtils.convert(response);
    payloadStreamObserver.onNext(convert);
}
Also used : Payload(com.alibaba.nacos.api.grpc.auto.Payload)

Example 3 with Payload

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

the class GrpcConnection method requestFuture.

@Override
public RequestFuture requestFuture(Request request) throws NacosException {
    Payload grpcRequest = GrpcUtils.convert(request);
    final ListenableFuture<Payload> requestFuture = grpcFutureServiceStub.request(grpcRequest);
    return new RequestFuture() {

        @Override
        public boolean isDone() {
            return requestFuture.isDone();
        }

        @Override
        public Response get() throws Exception {
            Payload grpcResponse = requestFuture.get();
            Response response = (Response) GrpcUtils.parse(grpcResponse);
            if (response instanceof ErrorResponse) {
                throw new NacosException(response.getErrorCode(), response.getMessage());
            }
            return response;
        }

        @Override
        public Response get(long timeout) throws Exception {
            Payload grpcResponse = requestFuture.get(timeout, TimeUnit.MILLISECONDS);
            Response response = (Response) GrpcUtils.parse(grpcResponse);
            if (response instanceof ErrorResponse) {
                throw new NacosException(response.getErrorCode(), response.getMessage());
            }
            return response;
        }
    };
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) RequestFuture(com.alibaba.nacos.api.remote.RequestFuture) Payload(com.alibaba.nacos.api.grpc.auto.Payload) NacosException(com.alibaba.nacos.api.exception.NacosException) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse)

Example 4 with Payload

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

the class GrpcConnection method request.

@Override
public Response request(Request request, long timeouts) throws NacosException {
    Payload grpcRequest = GrpcUtils.convert(request);
    ListenableFuture<Payload> requestFuture = grpcFutureServiceStub.request(grpcRequest);
    Payload grpcResponse;
    try {
        grpcResponse = requestFuture.get(timeouts, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        throw new NacosException(NacosException.SERVER_ERROR, e);
    }
    return (Response) GrpcUtils.parse(grpcResponse);
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) Payload(com.alibaba.nacos.api.grpc.auto.Payload) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 5 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.
 * @param meta    request meta.
 * @return payload.
 */
public static Payload convert(Request request, RequestMeta meta) {
    // meta.
    Payload.Builder payloadBuilder = Payload.newBuilder();
    Metadata.Builder metaBuilder = Metadata.newBuilder();
    if (meta != null) {
        metaBuilder.putAllHeaders(request.getHeaders()).setType(request.getClass().getSimpleName());
    }
    metaBuilder.setClientIp(NetUtils.localIP());
    payloadBuilder.setMetadata(metaBuilder.build());
    // request body .
    request.clearHeaders();
    String jsonString = toJson(request);
    return payloadBuilder.setBody(Any.newBuilder().setValue(ByteString.copyFrom(jsonString, StandardCharsets.UTF_8))).build();
}
Also used : Metadata(com.alibaba.nacos.api.grpc.auto.Metadata) Payload(com.alibaba.nacos.api.grpc.auto.Payload) ByteString(com.google.protobuf.ByteString)

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