Facing NoSuchMethodError for io.netty.util.Attribu

2019-03-21 14:38发布

问题:

After running GreetingServerTest.java tests I am getting below given errors. I am using grpc 1.1.0-SNAPSHOT libraries and trying to implement basic Helloword example of grpc given in there git repo. Can anybody please suggest which libraries I am missing or there's anything else I need to do.

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 file

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

回答1:

There could be multiple versions of netty jar in you class path during runtime. Use following command to check dependency tree.

mvn dependency:tree -Dverbose 

Or your container provides the netty jar might be clashing with the version of jar you packaging with your application.



回答2:

Here is what worked for me. I needed to shade io.netty and com.google.

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