简体   繁体   English

Java-grpc 和 tikv-java: NoSuchFieldError: CONTEXT_SPAN_KEY

[英]Java-grpc and tikv-java: NoSuchFieldError: CONTEXT_SPAN_KEY

I am using java-grpc together with tikv-java (separately they work OK).我将 java-grpc 与 tikv-java 一起使用(单独它们工作正常)。 But together I am struggling with the following error:但我一起努力解决以下错误:

Exception in thread "main" java.lang.NoSuchFieldError: CONTEXT_SPAN_KEY
        at io.grpc.internal.CensusTracingModule$TracingClientInterceptor.interceptCall(CensusTracingModule.java:327)
        at io.grpc.ClientInterceptors$InterceptorChannel.newCall(ClientInterceptors.java:104)
        at io.grpc.internal.ManagedChannelImpl.newCall(ManagedChannelImpl.java:551)
        at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:113)
        at com.pv.app.GetInsertServiceGrpc$GetInsertServiceBlockingStub.insert(GetInsertServiceGrpc.java:195)
        at com.pv.app.Client.main(Client.java:55)

My code-client:我的代码客户端:

package com.pv.app;

import io.grpc.*;

public class Client {
  public static void main(String[] args) throws Exception {
    // Channel is the abstraction to connect to a service endpoint
    // Let's use plaintext communication because we don't have certs
    final ManagedChannel channel =
        ManagedChannelBuilder.forTarget("0.0.0.0:8080").usePlaintext().build();


    GetInsertServiceGrpc.GetInsertServiceBlockingStub stub =
        GetInsertServiceGrpc.newBlockingStub(channel);
    GetInsertServiceOuterClass.HelloMessage request =
        GetInsertServiceOuterClass.HelloMessage.newBuilder().setName("hello").build();

    System.out.println(request);

    System.out.println("b4 req");

    // Finally, make the call using the stub
    stub.insert(request);

    channel.shutdownNow();
  }
}


My code-server:我的代码服务器:

package com.pv.app;

import io.grpc.Server;
import io.grpc.ServerBuilder;



/** Hello world! */
public class App {
  public static void main(String[] args) throws Exception {

    System.out.println("Hello-start");
    Server server = ServerBuilder.forPort(8080).addService(new GetInsertServiceImpl()).build();

    // Start the server
    server.start();

    // Server threads are running in the background.
    System.out.println("Server started");
    // Don't exit the main thread. Wait until server is terminated.
    server.awaitTermination();

  }
}

My code-implementation:我的代码实现:

package com.pv.app;


import org.tikv.common.TiConfiguration;
import org.tikv.common.TiSession;
import org.tikv.raw.RawKVClient;

public class GetInsertServiceImpl
    extends GetInsertServiceGrpc.GetInsertServiceImplBase {

  @Override
  public void insert(
      GetInsertServiceOuterClass.HelloMessage request,
      io.grpc.stub.StreamObserver<com.google.protobuf.Empty> responseObserver) {
    // HelloRequest has toString auto-generated.
    System.out.println("insert");
    System.out.println(request);
    TiConfiguration conf = TiConfiguration.createRawDefault("pd0:2379");
    System.out.println(1);
    System.out.println("2");
    System.out.println(conf);
    TiSession session = TiSession.create(conf);
    System.out.println("3");
    RawKVClient client = session.createRawClient();
    System.out.println("4");



    // When you are done, you must call onCompleted.
    responseObserver.onCompleted();
  }
}

My proto:我的原型:

syntax = "proto3";
import "google/protobuf/empty.proto";


option java_package = "com.pv.app";
// Request payload


message HelloMessage {

    string name = 1;

}

// Defining a Service, a Service can have multiple RPC operations
service GetInsertService {
    // Define a RPC operation
    rpc insert (HelloMessage) returns (google.protobuf.Empty) {
    };

}

What I do to deploy:我做什么来部署:

  • In downloaded repo client-java I do mvn clean install -Dmaven.test.skip=true在下载的 repo client-java我做mvn clean install -Dmaven.test.skip=true
  • In my project folder在我的项目文件夹中
mvn install:install-file \
-Dfile=../client-java/target/tikv-client-java-2.0-SNAPSHOT.jar  \
-DgroupId=org.tikv \
-DartifactId=tikv-client-java \
-Dversion=2.0-SNAPSHOT \
-Dpackaging=jar
  • In my project pom.xml在我的项目 pom.xml
<dependency>
  <groupId>org.tikv</groupId>
  <artifactId>tikv-client-java</artifactId>
  <version>2.0-SNAPSHOT</version>
</dependency>
  • Running java 8:运行 Java 8:
mvn -DskipTests package exec:java -Dexec.mainClass=com.pv.app.App  
mvn -DskipTests package exec:java -Dexec.mainClass=com.pv.app.Client 

Does anyone have a suggestion how to fix?有没有人有建议如何解决?

Full code is available here完整代码可在此处获得

I did my searching, tried to exclude grpc and opencensus, switch versions - did not help.我做了我的搜索,试图排除 grpc 和 opencensus,切换版本 - 没有帮助。

The problem is caused by conflicting io.opencensus versions.该问题是由io.opencensus版本冲突引起的。 I was able to fix it by shading it in the tikv/client-java project.我能够通过在tikv/client-java项目中对其进行着色来修复它。

In the tikv/client-java, pom.xml, maven-shade-plugin configuration:在tikv/client-java、pom.xml、maven-shade-plugin配置中:

<relocations>
    ...
    <relocation>
        <pattern>io.opencensus</pattern>
        <shadedPattern>shade.io.opencensus</shadedPattern>
    </relocation>
<relocations>

UPDATE更新

I have just realized that there were changes to the pom.xml merged to the master yesterday, so you may want to update it if you haven't yet.我刚刚意识到昨天合并到 master 的 pom.xml 发生了变化,所以如果你还没有更新它,你可能想要更新它。

UPDATE 2更新 2

I have just checked your project with the recent version of the tikv/client-java .我刚刚用最新版本的tikv/client-java检查了你的项目。 The NoSuchFieldError: CONTEXT_SPAN_KEY is gone. NoSuchFieldError: CONTEXT_SPAN_KEY消失了。 There are other errors (java.net.UnknownHostException) but they are rather not related.还有其他错误 (java.net.UnknownHostException) 但它们并不相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM