title: Can I run multiple GRPC services on same port
body: <p>I'm working on <code>gRPC</code> and I want to run multiple services on same <strong>port</strong></p>
  
  <pre><code>Server server = ServerBuilder.forPort(8080)
      .addService(new HelloServiceImpl())
      .addService(new ByeServiceImpl())
      .build();
  </code></pre>
  
  <p>Is this the right way to run multiple GRPC services on same port? Full code below.</p>
  
  <p><strong>HelloWorld.proto</strong></p>
  
  <pre class="lang-proto prettyprint-override"><code>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);
  }
  </code></pre>
  
  <p><strong>ByWorld.proto</strong></p>
  
  <pre class="lang-proto prettyprint-override"><code>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);
  }
  </code></pre>
  
  <p><strong>HelloServiceImpl.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class HelloServiceImpl extends HelloServiceImplBase{
  
      @Override
      public void hello(
          HelloRequest request,
          StreamObserver&lt;HelloResponse&gt; 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();  
      }
  
  }
  </code></pre>
  
  <p><strong>ByeServiceImpl.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class ByeServiceImpl extends ByeServiceImplBase{
  
      @Override
      public void bye(
          ByeRequest request,
          StreamObserver&lt;ByeResponse&gt; 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();  
      }
  
  }
  </code></pre>
  
  <p><strong>GrpcServer.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>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();
          }
  
      }
  
  }
  </code></pre>
  
  <p><strong>GrpcClient.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>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(); 
      }
  
  }
  </code></pre>
  
  <p><strong>OUPUT</strong></p>
  
  <pre><code>Hello Service...
  Hello Azeem Haider
  
  Bye Service...
  Bye Azeem Haider
  </code></pre>
  
  <p>I know both <strong>Services</strong> files look very similar but this is just for example how we can run multi services on same <strong>IP:PORT</strong> I'm using this way is it a good way or is there any preferred way?</p>