java grpc服务器未启动

n3schb8v  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(626)

我试图在我的代码中实现grpc服务,条件是使用服务器类的一个示例成功地构建代码,但是我得到了一个运行时错误。
如何修复此错误?

Exception in thread "main" java.lang.NullPointerException: bindableService
   at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:897)
   at io.grpc.internal.AbstractServerImplBuilder.addService(AbstractServerImplBuilder.java:120)
   at io.grpc.internal.AbstractServerImplBuilder.addService(AbstractServerImplBuilder.java:56)
   at com.mypackage.cu.comms.CURequestHandlerService.<init>(Unknown Source)
   at com.mypackage.cu.comms.CURequestHandlerService.start(Unknown Source)
   at com.mypackage.cu.entities.ControlUnit.initSystem(Unknown Source)
   at com.mypackage.cu.entities.ControlUnit.run(Unknown Source)
   at com.mypackage.cu.entities.ControlUnit.main(Unknown Source)

生成的grpc java类是curequesthandlergrpc。
服务器类curequesthandlerservice:

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
\\ ..

public class CURequestHandlerService extends CURequestHandlerGrpc.CURequestHandlerImplBase {
  private final Server server;
  private static CURequestHandlerService INSTANCE = null;
  private CURequestHandlerService(int port) {
    this.server = ServerBuilder.forPort(port)
            .addService(INSTANCE)
            .build();
  }

  public static void start(int port) {
    if (INSTANCE == null)
      INSTANCE = new CURequestHandlerService(port);

    try {
      INSTANCE.server.start();
    } catch (IOException e) {
      System.out.println("Can't start GRPC server!");
    }
  }

在主课堂上

CURequestHandlerService.start(16440);

我正在使用ant,因为它是一个老项目,我正在维护它。我将所有grpc依赖项jar添加到依赖项目录。
我的jar:

amqp-client-5.7.3.jar
failureaccess-1.0.1.jar
grpc-all-1.27.0.jar
grpc-api-1.27.0.jar
grpc-auth-1.27.0.jar
grpc-context-1.27.0.jar
grpc-core-1.27.0.jar
grpc-netty-1.27.0.jar
grpc-netty-shaded-1.27.0.jar
grpc-protobuf-1.27.0.jar
grpc-protobuf-lite-1.27.0.jar
grpc-stub-1.27.0.jar
grpc-testing-1.27.0.jar
guava-28.2-jre.jar
json-smart-1.2.jar
lettuce-4.3.0.Final-shaded.jar
libthrift-0.9.3.jar
opencensus-api-0.20.0.jar
opencensus-contrib-grpc-metrics-0.20.0.jar
perfmark-api-0.21.0.jar
protobuf-java-3.11.3.jar
protobuf-java-util-3.11.3.jar
protoc-gen-grpc-java-1.27.0-linux-x86_64.exe
slf4j-api-1.7.22.jar
slf4j-simple-1.7.22.jar
dy2hfwbg

dy2hfwbg1#

在阅读grpcjava存储库中的服务器示例之后,我发现问题不是依赖性问题。我私下告诉你,你必须把服务器类从实现类中分离出来,实现类从grpc生成的类中扩展出来。
服务器类

/**
 * Grpc instance class that used to initiate the server and call implementation class
 */
public class GrpcServer {
    private final Server server;
    private static GrpcServer INSTANCE = null;
    private GrpcServer(int port) {
        this.server = ServerBuilder.forPort(port)
                .addService(new CURequestHandlerService())
                .build();
    }

    public static void start(int port) {
        if (INSTANCE == null)
            INSTANCE = new GrpcServer(port);

        try {
            INSTANCE.server.start();
            System.out.println("GRPC server started");
        } catch (IOException e) {
            System.out.println("Can't start GRPC server!");
        }
    }
}

实现类


**

 * Request Handler class contains the implementation for all functions initiated in proto files
 * The implemented function 
 * */
public class CURequestHandlerService extends CURequestHandlerGrpc.CURequestHandlerImplBase{

  public synchronized void Method1(Messages.Method1Input input,
                                         StreamObserver<Messages.Method1Output> output) {
    //...
    output.onNext(outputResponse);
    output.onCompleted();
  }

相关问题