使用 grpc 和 protobuf Hello world 示例面对 io.netty.util.AttributeKey.valueOf() 方法的 NoSuchMethodError

Facing NoSuchMethodError for io.netty.util.AttributeKey.valueOf() method with grpc and protobuf Hello world example

在 运行 GreetingServerTest.java 测试后,我得到低于给定的错误。我正在使用 grpc 1.1.0-SNAPSHOT 库并尝试实现 git 存储库中给出的 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)

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>InternodeCommunication</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <!-- Spring -->
    <spring-framework.version>4.3.1.RELEASE</spring-framework.version>

    <!-- Logging -->
    <logback.version>1.0.13</logback.version>
    <slf4j.version>1.7.5</slf4j.version>

    <!-- Kundera JPA -->
    <kundera-version>3.4</kundera-version>

    <!-- Hibernate jpa -->
    <hibernate.version>4.2.1.Final</hibernate.version>


    <!-- Test -->
    <junit.version>4.12</junit.version>
    <grpc.version>1.1.0-SNAPSHOT</grpc.version>
    <!-- Generic properties -->
</properties>

<dependencies>
    <!-- Spring and Transactions -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <!-- kafka -->
    <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka_2.10 -->
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_2.10</artifactId>
        <version>0.10.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
        </exclusions>

    </dependency>
    <dependency>
        <groupId>com.xyz</groupId>
        <artifactId>Common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.5.Final</version>
    </dependency>
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-netty</artifactId>
        <version>${grpc.version}</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-protobuf</artifactId>
        <version>${grpc.version}</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-stub</artifactId>
        <version>${grpc.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.grpc/grpc-all -->
    <!-- https://mvnrepository.com/artifact/io.netty/netty-codec -->
    <!-- https://mvnrepository.com/artifact/io.netty/netty-all -->




    <!-- Test Artifacts -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring-framework.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
    </dependency>
    <dependency>
        <groupId>com.xyz</groupId>
        <artifactId>Common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>test-jar</type>
        <scope>test</scope>
    </dependency>

</dependencies>
<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.4.1.Final</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.5.0</version>
            <configuration>
                <!-- The version of protoc must match protobuf-java. If you don't depend 
                    on protobuf-java directly, you will be transitively depending on the protobuf-java 
                    version that grpc depends on. -->
                <protocArtifact>com.google.protobuf:protoc:3.0.0:exe:${os.detected.classifier}</protocArtifact>
                <pluginId>grpc-java</pluginId>
                <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.1.0-SNAPSHOT:exe:${os.detected.classifier}</pluginArtifact>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

hello_world.proto 文件

syntax = "proto3";
option java_multiple_files = true;
package com.xyz.communication.example;
message HelloRequest {
 string name = 1;
}
message HelloResponse {
  string greeting = 1;
}
service GreetingService {
   rpc greeting(HelloRequest) returns (HelloResponse);
}

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

在运行期间,您的 class 路径中可能有多个版本的 netty jar。使用以下命令检查依赖树。

mvn dependency:tree -Dverbose 

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

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

   <relocation>
      <pattern>io.netty</pattern>
      <shadedPattern>myshade.io.netty</shadedPattern>
    </relocation>
    <relocation>
       <pattern>com.google</pattern>
       <shadedPattern>myshade.com.google</shadedPattern>
   </relocation>