【问题标题】:Can I run multiple GRPC services on same port [closed]我可以在同一个端口上运行多个 GRPC 服务吗?
【发布时间】:2019-10-09 15:02:50
【问题描述】:

我正在处理gRPC,我想在同一个端口

上运行多个服务
Server server = ServerBuilder.forPort(8080)
    .addService(new HelloServiceImpl())
    .addService(new ByeServiceImpl())
    .build();

这是在同一端口上运行多个 GRPC 服务的正确方法吗?完整代码如下。

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(); 
    }

}

输出

Hello Service...
Hello Azeem Haider

Bye Service...
Bye Azeem Haider

我知道两个 Services 文件看起来非常相似,但这只是我们如何在同一个 IP:PORT 上运行多个服务的示例,我正在使用这种方式是吗好的方法还是有什么首选的方法?

【问题讨论】:

  • 有什么问题?您可以添加任意数量的服务,它们都将在同一个端口上运行。
  • 问题是如何在同一个端口上运行多个服务,但是我在你的代码中没有看到你创建服务器和添加服务的地方。
  • @AnarSultanov 对不起,我忘了添加这个,现在我更新了问题。我只是问这是不是首选方式
  • @AzeemHaider 是的,这是添加服务和启动服务器的完全标准方法,至少如果您在应用程序中不使用任何其他可以为您处理它的框架(例如 Spring Boot )。
  • @AnarSultanov 将来我打算使用 Google Guice 框架,有什么建议吗?

标签: java grpc grpc-java


【解决方案1】:

你的用法是正确的。 Server 监听一个套接字,并分派给一个或多个 Services

【讨论】:

    猜你喜欢
    • 2021-12-11
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 2020-05-17
    • 2013-04-04
    • 1970-01-01
    相关资源
    最近更新 更多