2

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

}

我要做的部署:

  • 在下载的 repo client-java我做mvn clean install -Dmaven.test.skip=true
  • 在我的项目文件夹中
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
  • 在我的项目 pom.xml
<dependency>
  <groupId>org.tikv</groupId>
  <artifactId>tikv-client-java</artifactId>
  <version>2.0-SNAPSHOT</version>
</dependency>
  • 运行java 8:
mvn -DskipTests package exec:java -Dexec.mainClass=com.pv.app.App  
mvn -DskipTests package exec:java -Dexec.mainClass=com.pv.app.Client 

有没有人建议如何解决?

完整代码可在此处获得

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

4

1 回答 1

3

该问题是由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),但它们并不相关。

于 2019-12-28T18:10:52.457 回答