19

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?

Eloff
  • 20,828
  • 17
  • 83
  • 112
Azeem Haider
  • 1,443
  • 4
  • 23
  • 41
  • What is the problem? You can add as much services as you like, they will all run at the same port. – Oleg May 23 '19 at 05:42
  • The question is how to run multiple services on the same port, but I do not see in your code where you create a server and add services. – Anar Sultanov May 23 '19 at 06:49
  • 1
    @AnarSultanov Sorry, I forget to add this now I update the question. I'm just asking it is a preferred way or not – Azeem Haider May 23 '19 at 07:16
  • @AzeemHaider Yes, this is a completely standard way of adding your services and starting a server, at least if you do not use any other framework in your application that can handle it for you (e.g. Spring Boot). – Anar Sultanov May 23 '19 at 08:02
  • @AnarSultanov in future I planed to use **Google Guice** framework, any suggestions? – Azeem Haider May 23 '19 at 10:48
  • @AzeemHaider as far as I know, Google Guice does not have any additional features other than dependency injection, so I don’t think that this will have any impact. The only thing you are likely to do is create the singletons of your services and inject them where needed. – Anar Sultanov May 23 '19 at 12:05
  • Yes because I only need **Dependency Injection** Thanks for the suggestion of **Singletons** approch – Azeem Haider May 24 '19 at 06:10

1 Answers1

17

Your usage is correct. A Server listens on a socket, and dispatches to one or more Services.

Carl Mastrangelo
  • 5,970
  • 1
  • 28
  • 37