本文介绍了面对带有grpc和protobuf的io.netty.util.AttributeKey.valueOf()方法的NoSuchMethodError Hello world示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行GreetingServerTest.java测试后,我遇到了错误。我正在使用grpc 1.1.0-SNAPSHOT库并尝试实现git repo中给出的grpc的基本Helloword示例。任何人都可以建议我缺少哪些库或者我需要做什么。

  java.lang.NoSuchMethodError:io。 netty.util.AttributeKey.valueOf(Ljava /郎/类; Ljava /郎/字符串;)LIO /网状/ 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。在com.xyz.communication.datacollection.GrpcServerTest.test(GrpcServerTest.java:23)
$ b

pom.xml

 < project xmlns =http://maven.apache .org / POM / 4.0.0xmlns: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-framework.version> 4.3.1.RELEASE< /spring-framework.version>

<! - 记录 - >
< 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>


<! - 测试 - >
< junit.version> 4.12< /junit.version>
< grpc.version> 1.1.0-SNAPSHOT< /grpc.version>
<! - 通用属性 - >
< / 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 - >




<! - 测试工件 - >
< 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>
<! - protoc的版本必须与protobuf-java匹配。如果你不直接在protobuf-java上依赖
,那么你将依赖于grpc所依赖的protobuf-java
版本。 - >
< 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; 
选项java_multiple_files = true;
package com.xyz.communication.example;
消息HelloRequest {
string name = 1;
}
消息HelloResponse {
string greeting = 1;
}
服务GreetingService {
rpc greeting(HelloRequest)返回(HelloResponse);
}

GreetingServerTest.java

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

类GrpcServerImpl扩展GreetingServiceGrpc.GreetingServiceImplBase {
@Override
public void greeting(HelloRequest request,StreamObserver< HelloResponse> responseObserver){
HelloResponse response = HelloResponse.newBuilder()。setGreeting(Hello+ request.getName())。build();
responseObserver.onNext(回复);
responseObserver.onCompleted();
}

@Override
public ServerServiceDefinition bindService(){
return super.bindService();
}
}


解决方案

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

  mvn dependency:tree -Dverbose 

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


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();
    }
}
解决方案

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.

这篇关于面对带有grpc和protobuf的io.netty.util.AttributeKey.valueOf()方法的NoSuchMethodError Hello world示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:58