stranger
stranger

Reputation: 23

how to have both gRPC server and client at same process to achieve bi-directional communication(not server/client streaming)

I am pretty new to gRPC. I am thinking of using gRPC(Java) to do inter node(server) communication in my use case:

  1. I have my own app logic to do some bookkeeping work on each node;
  2. a node would need to communicate with others to reach some consensus(part of app logic) and this means a node need to both have client and server;
  3. so how could I achieve this? server seems to be blocking after I call server.awaitTerminate(), right? but do we also have the async version of the gRPC server in java? I bet yes, but I am not yet sure how could I leverage it.

for example, I have node A, B, C. I will need to have gRPC serverA, serverB, serverC start first, and for each server say A, I need client to connect to B and C. and in addition to communication part, app (say in node A)logic would be able to send out msg to other nodes(say B and C) via corresponding clients(to server B and C) if needed;and of course app logic would be notified when requests coming from B and C(because itself is a server).

I've been searching online for days and have gone through grpc/grpc-java related material and code example. however, i find there's not that much code example to show what is best practice and pattern to leverage gRPC...i'd really like to hear whatever suggestion you may have...

thanks in advance!

Upvotes: 2

Views: 3020

Answers (1)

Eric Anderson
Eric Anderson

Reputation: 26464

Calling server.awaitTermination() in your main() is not required. The examples do so because grpc-java uses daemon threads by default. Thus, in the examples the only non-daemon thread is the main thread, and you need at least one non-daemon thread to keep the JVM running. See the documentation for java.lang.Thread.

awaitTermination() is not a serve_forever() method that processes new requests; awaitTermination() simply blocks the current thread until the grpc server has terminated. Processing happens on other threads.

Upvotes: 2

Related Questions