Search in sources :

Example 1 with ServerCheckRequest

use of com.alibaba.nacos.api.remote.request.ServerCheckRequest 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 2 with ServerCheckRequest

use of com.alibaba.nacos.api.remote.request.ServerCheckRequest in project nacos by alibaba.

the class GrpcRequestAcceptorTest method testServerCheckRequest.

@Test
public void testServerCheckRequest() {
    ApplicationUtils.setStarted(true);
    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 ServerCheckResponse);
        }

        @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) ServerCheckResponse(com.alibaba.nacos.api.remote.response.ServerCheckResponse) ServerCheckRequest(com.alibaba.nacos.api.remote.request.ServerCheckRequest) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) Payload(com.alibaba.nacos.api.grpc.auto.Payload) Test(org.junit.Test)

Example 3 with ServerCheckRequest

use of com.alibaba.nacos.api.remote.request.ServerCheckRequest in project nacos by alibaba.

the class GrpcClient method serverCheck.

/**
 * check server if success.
 *
 * @param requestBlockingStub requestBlockingStub used to check server.
 * @return success or not
 */
private Response serverCheck(String ip, int port, RequestGrpc.RequestFutureStub requestBlockingStub) {
    try {
        if (requestBlockingStub == null) {
            return null;
        }
        ServerCheckRequest serverCheckRequest = new ServerCheckRequest();
        Payload grpcRequest = GrpcUtils.convert(serverCheckRequest);
        ListenableFuture<Payload> responseFuture = requestBlockingStub.request(grpcRequest);
        Payload response = responseFuture.get(3000L, TimeUnit.MILLISECONDS);
        // receive connection unregister response here,not check response is success.
        return (Response) GrpcUtils.parse(response);
    } catch (Exception e) {
        LoggerUtils.printIfErrorEnabled(LOGGER, "Server check fail, please check server {} ,port {} is available , error ={}", ip, port, 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) ServerCheckRequest(com.alibaba.nacos.api.remote.request.ServerCheckRequest) Payload(com.alibaba.nacos.api.grpc.auto.Payload) NacosException(com.alibaba.nacos.api.exception.NacosException)

Aggregations

Payload (com.alibaba.nacos.api.grpc.auto.Payload)3 ServerCheckRequest (com.alibaba.nacos.api.remote.request.ServerCheckRequest)3 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)2 ErrorResponse (com.alibaba.nacos.api.remote.response.ErrorResponse)2 ServerCheckResponse (com.alibaba.nacos.api.remote.response.ServerCheckResponse)2 StreamObserver (io.grpc.stub.StreamObserver)2 Test (org.junit.Test)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 Response (com.alibaba.nacos.api.remote.response.Response)1