ServerBuilder.forPort(端口)-java.lang.NoSuchMethodError

时间:2019-12-27 16:02:10

标签: java grpc nosuchmethoderror grpc-java

我正在尝试在Java中创建grpc服务。问题出在服务器创建中:

    public static void main(String[] args) throws IOException, InterruptedException {

        Server server = ServerBuilder.forPort(50051)
                .addService(new AccountServiceGRPC())
                .addService(ProtoReflectionService.newInstance()) // added Reflection
                .build();
server.start();
        System.out.println("Started");

        // Shutdown: Using Runtime shutdown server [Imp: before await Termination]
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("Received Shutdown Request");
            server.shutdown();
            System.out.println("Successfully, Stopped Shutdown the server");
        }));
        server.awaitTermination();
    }
}

我收到错误消息:

Exception in thread "main" java.lang.NoSuchMethodError:
com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V
    at io.grpc.Metadata$Key.validateName(Metadata.java:629)
    at io.grpc.Metadata$Key.<init>(Metadata.java:637)
    at io.grpc.Metadata$Key.<init>(Metadata.java:567)
    at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:742)
    at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:737)
    at io.grpc.Metadata$Key.of(Metadata.java:593)
    at io.grpc.Metadata$Key.of(Metadata.java:589)
    at io.grpc.internal.GrpcUtil.<clinit>(GrpcUtil.java:86)
    at io.grpc.internal.AbstractServerImplBuilder.<clinit>(AbstractServerImplBuilder.java:60)
    at io.grpc.netty.shaded.io.grpc.netty.NettyServerProvider.builderForPort(NettyServerProvider.java:39)
    at io.grpc.netty.shaded.io.grpc.netty.NettyServerProvider.builderForPort(NettyServerProvider.java:24)
    at io.grpc.ServerBuilder.forPort(ServerBuilder.java:41)
    at com.example.demo.DemoApplication.main(DemoApplication.java:19)

Process finished with exit code 1

感谢答案。

1 个答案:

答案 0 :(得分:1)

看来番石榴被降级了。我假设您使用的是Maven,因为Maven使用"nearest definition"机制来选择依赖版本,该版本会稍微降低依赖的级别。 grpc-java小组建议始终使用Maven Enforcer的requireUpperBoundDeps来检测Maven何时选择较差版本的依赖项。

    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.4.1</version>
        <executions>
          <execution>
            <id>enforce</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireUpperBoundDeps/>
              </rules>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>

当检测到软件包降级时,请选择最大的版本并将该依赖项显式添加到您的项目中,这将是“最近的定义”。

<dependencies>
  ...
  <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>23.6-jre</version>
  </dependency>
</dependencies>