Java 通过grpc和protobuf Hello world示例,为io.netty.util.AttributeKey.valueOf()方法面对NoSuchMethodError

Java 通过grpc和protobuf Hello world示例,为io.netty.util.AttributeKey.valueOf()方法面对NoSuchMethodError,java,maven-2,netty,protocol-buffers,grpc-java,Java,Maven 2,Netty,Protocol Buffers,Grpc Java,在运行GreetingServerTest.java测试之后,我发现以下错误。我正在使用GRPC1.1.0-SNAPSHOT库,并试图实现GitRepo中给出的grpc的基本Helloword示例。谁能告诉我我缺少哪些图书馆,或者我还需要做些什么 java.lang.NoSuchMethodError: io.netty.util.AttributeKey.valueOf(Ljava/lang/Class;Ljava/lang/String;)Lio/netty/util/AttributeKe

在运行GreetingServerTest.java测试之后,我发现以下错误。我正在使用GRPC1.1.0-SNAPSHOT库,并试图实现GitRepo中给出的grpc的基本Helloword示例。谁能告诉我我缺少哪些图书馆,或者我还需要做些什么

java.lang.NoSuchMethodError: io.netty.util.AttributeKey.valueOf(Ljava/lang/Class;Ljava/lang/String;)Lio/netty/util/AttributeKey;
at io.grpc.netty.Utils.<clinit>(Utils.java:87)
at io.grpc.netty.NettyServer.allocateSharedGroups(NettyServer.java:187)
at io.grpc.netty.NettyServer.start(NettyServer.java:116)
at io.grpc.internal.ServerImpl.start(ServerImpl.java:156)
at io.grpc.internal.ServerImpl.start(ServerImpl.java:83)
at com.xyz.communication.datacollection.GrpcServerTest.test(GrpcServerTest.java:23)
GreetingServerTest.java

@ContextConfiguration("/communication-test-beans.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class GrpcServerTest {
    @Test
    public void test() throws IOException, InterruptedException {
        io.grpc.Server server = ServerBuilder.forPort(8080).addService(new GrpcServerImpl().bindService()).build();
        server.start();
        server.awaitTermination();
    }
}

class GrpcServerImpl extends GreetingServiceGrpc.GreetingServiceImplBase {
    @Override
    public void greeting(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
        HelloResponse response = HelloResponse.newBuilder().setGreeting("Hello " + request.getName()).build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    @Override
    public ServerServiceDefinition bindService() {
        return super.bindService();
    }
}
@ContextConfiguration(“/communicationtestbean.xml”)
@RunWith(SpringJUnit4ClassRunner.class)
公共类GrpcServerTest{
@试验
public void test()引发IOException、InterruptedException{
io.grpc.Server Server=ServerBuilder.forPort(8080).addService(新的GrpcServerImpl().bindService()).build();
server.start();
等待终止();
}
}
类GrpcServerImpl扩展了GreetingServiceGrpc.GreetingServiceImplBase{
@凌驾
公共无效问候语(HelloRequest请求、StreamObserver响应Observer){
HelloResponse response=HelloResponse.newBuilder().setGreeting(“Hello”+request.getName()).build();
responseObserver.onNext(响应);
responseObserver.onCompleted();
}
@凌驾
公共服务器服务定义bindService(){
返回super.bindService();
}
}

在运行时,类路径中可能有多个版本的netty jar。使用以下命令检查依赖关系树

mvn dependency:tree -Dverbose 

或者您的容器提供的netty jar可能与您的应用程序打包的jar版本发生冲突。

以下是对我有效的方法。我需要遮住io.netty和com.google


伊奥·内蒂
迈沙德·伊奥·内蒂
谷歌
myshade.com.google

到底是什么问题?您是如何修复的?有不同的库,其中一些库使用不同版本的netty。使用mvn dependency:tree-Dverbose,您将知道哪个组件使用哪个netty版本。根据这些信息,找出您需要告诉他们不要下载netty jar的组件@navige@nikhilbhr你能分享你的pom文件吗?您可能添加的特定排除项会有所帮助。
@ContextConfiguration("/communication-test-beans.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class GrpcServerTest {
    @Test
    public void test() throws IOException, InterruptedException {
        io.grpc.Server server = ServerBuilder.forPort(8080).addService(new GrpcServerImpl().bindService()).build();
        server.start();
        server.awaitTermination();
    }
}

class GrpcServerImpl extends GreetingServiceGrpc.GreetingServiceImplBase {
    @Override
    public void greeting(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
        HelloResponse response = HelloResponse.newBuilder().setGreeting("Hello " + request.getName()).build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    @Override
    public ServerServiceDefinition bindService() {
        return super.bindService();
    }
}
mvn dependency:tree -Dverbose