Java-grpc and tikv-java: NoSuchFieldError: CONTEXT_SPAN_KEY

Java-grpc and tikv-java: NoSuchFieldError: CONTEXT_SPAN_KEY

我正在使用 java-grpc 和 tikv-java (它们单独工作正常)。但我一起努力解决以下错误:

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)

我的代码客户端:

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


我的代码服务器:

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

  }
}

我的代码实现:

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

我的原型:

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

}

我要部署的内容:

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
<dependency>
  <groupId>org.tikv</groupId>
  <artifactId>tikv-client-java</artifactId>
  <version>2.0-SNAPSHOT</version>
</dependency>
mvn -DskipTests package exec:java -Dexec.mainClass=com.pv.app.App  
mvn -DskipTests package exec:java -Dexec.mainClass=com.pv.app.Client 

有没有人建议如何修复?

完整代码可用here

我进行了搜索,试图排除 grpc 和 opencensus,切换版本 - 没有帮助。

问题是由 io.opencensus 版本冲突引起的。我能够通过在 tikv/client-java 项目中对其进行着色来修复它。

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

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

更新

我刚刚发现昨天合并到master的pom.xml有变化,所以如果你还没有更新它可能想更新它。

更新 2

我刚刚用 tikv/client-java 的最新版本检查了您的项目。 NoSuchFieldError: CONTEXT_SPAN_KEY 不见了。还有其他错误 (java.net.UnknownHostException),但它们并不相关。