I'm working on gRPC
and I want to run multiple services on same port
Server server = ServerBuilder.forPort(8080)
.addService(new HelloServiceImpl())
.addService(new ByeServiceImpl())
.build();
Is this the right way to run multiple GRPC services on same port? Full code below.
HelloWorld.proto
syntax = "proto3";
option java_multiple_files = true;
package proto3.rpc;
message HelloRequest {
string firstName = 1;
string lastName = 2;
}
message HelloResponse {
string greeting = 1;
}
service HelloService {
rpc hello(HelloRequest) returns (HelloResponse);
}
ByWorld.proto
syntax = "proto3";
option java_multiple_files = true;
package proto3.rpc;
message ByeRequest {
string firstName = 1;
string lastName = 2;
}
message ByeResponse {
string greeting = 1;
}
service ByeService {
rpc bye(ByeRequest) returns (ByeResponse);
}
HelloServiceImpl.java
public class HelloServiceImpl extends HelloServiceImplBase{
@Override
public void hello(
HelloRequest request,
StreamObserver<HelloResponse> responseObserver){
String greeting = new StringBuilder()
.append("Hello, ")
.append(request.getFirstName())
.append(" ")
.append(request.getLastName())
.toString();
HelloResponse helloResponse = HelloResponse.newBuilder()
.setGreeting(greeting)
.build();
responseObserver.onNext(helloResponse);
responseObserver.onCompleted();
}
}
ByeServiceImpl.java
public class ByeServiceImpl extends ByeServiceImplBase{
@Override
public void bye(
ByeRequest request,
StreamObserver<ByeResponse> responseObserver){
String greeting = new StringBuilder()
.append("Bye, ")
.append(request.getFirstName())
.append(" ")
.append(request.getLastName())
.toString();
ByeResponse byeResponse = ByeResponse.newBuilder()
.setGreeting(greeting)
.build();
responseObserver.onNext(byeResponse);
responseObserver.onCompleted();
}
}
GrpcServer.java
public class GrpcServer {
public static void main(String args[]) {
Server server = ServerBuilder.forPort(8080)
.addService(new HelloServiceImpl())
.addService(new ByeServiceImpl())
.build();
try {
server.start();
server.awaitTermination();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
GrpcClient.java
public class GrpcClient {
public static void main(String args[]){
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
.usePlaintext()
.build();
HelloServiceGrpc.HelloServiceBlockingStub stub
= HelloServiceGrpc.newBlockingStub(channel);
HelloResponse helloResponse = stub.hello(HelloRequest.newBuilder()
.setFirstName("Azeem")
.setLastName("Haider")
.build()
);
System.out.println("Hello Service...");
System.out.println(helloResponse.getGreeting());
ByeServiceGrpc.ByeServiceBlockingStub stubBye
= ByeServiceGrpc.newBlockingStub(channel);
ByeResponse byeResponse = stubBye.bye(ByeRequest.newBuilder()
.setFirstName("Azeem")
.setLastName("Haider")
.build()
);
System.out.println("Bye Service...");
System.out.println(byeResponse.getGreeting());
channel.shutdown();
}
}
OUPUT
Hello Service...
Hello Azeem Haider
Bye Service...
Bye Azeem Haider
I know both Services files look very similar but this is just for example how we can run multi services on same IP:PORT I'm using this way is it a good way or is there any preferred way?
What is the problem? You can add as much services as you like, they will all run at the same port.
Multiple gRPC clients can be created from a channel, including different types of clients. A channel and clients created from the channel can safely be used by multiple threads. Clients created from the channel can make multiple simultaneous calls.
To open a gRPC connection to a service so you can send gRPC messages, you need to specify the host domain, which is the URL of the Cloud Run service or the custom domain mapped to that service, along with the port 443, which is the port expected to be used by gRPC.
gRPC comes with three Transport implementations: The Netty-based transport is the main transport implementation based on Netty. It is for both the client and the server. The OkHttp-based transport is a lightweight transport based on OkHttp.
Your usage is correct. A Server listens on a socket, and dispatches to one or more Services.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With