有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我可以在同一端口上运行多个GRPC服务吗

我正在处理gRPC,我想在同一端口上运行多个服务

Server server = ServerBuilder.forPort(8080)
    .addService(new HelloServiceImpl())
    .addService(new ByeServiceImpl())
    .build();

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

地狱世界。原型

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。原型

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

我知道两个服务文件看起来非常相似,但这只是一个例子,我们可以在同一IP:PORT上运行多个服务。我正在使用这种方法。这是一种好方法还是有任何首选方法


共 (1) 个答案

  1. # 1 楼答案

    你的用法是正确的。服务器侦听套接字,并向一个或多个服务发送