The maven dependencies are same with server project.
Just copy those dependencies from server project to client project.
The proto file is the contract between client and server.
Just copy the proto file from server project to client project. So that, the client can communicate with the server apis with codes generated from the proto file.
The grpc library provides channel, that the client can connect the service and access the apis using channel.
ManagedChannelBuilder.forAddress("localhost", 9091)
with no security method usePlaintext()
Using newBlockingStub()
:
Whenever the client sends the request, the client will wait for the server response. It means that the client cannot do further process.
Using newFutureStub()
:
Creates a new ListenableFuture-style stub that supports unary calls on the service.
It is completable feature concept.
Using newStub()
:
It is a synchronous call. Once the client makes a request to the api, it will not wait for the response from the server. It will do the further process. Once the server sends the response it will capture the response from the server.
User.LoginRequest loginRequest = User.LoginRequest.newBuilder().setUsername("nick").setPassword("npass").build();
ser.APIResponse apiResponse = userBlockingStub.login(loginRequest);
If the server does not register the service in addService()
, it will get exception io.grpc.StatusRuntimeException: UNIMPLEMENTED: Method not found
Exception in thread "main" io.grpc.StatusRuntimeException: UNIMPLEMENTED: Method not found: user/login
at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:240)
at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:221)
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:140)
at nd.pemo.grpcclientsample.userGrpc$userBlockingStub.login(userGrpc.java:211)
at nd.pemo.client.GRPCClient.main(GRPCClient.java:31)
or from BloomRPC:
{
"error": "12 UNIMPLEMENTED: Method not found: user/login"
}
See Status status = Status.UNIMPLEMENTED.withDescription("Method not found: " + methodName);
in grpc-core-1.27.0.jar!/io/grpc/internal/ServerImpl.class
grpc client with springboot demo
grpc server with springboot demo
sprintboot with grpc server. Fork from nils