MacOS 上的 TIKV java 客户端:无法为 PD 集群初始化客户端

TIKV java client on MacOS: Failed to init client for PD cluster

我想在 MacOS 上使用其 java 客户端访问 TIKV。

我有一个 hello-world 应用程序:

package com.pv.app;
import org.tikv.common.TiConfiguration;
import org.tikv.common.TiSession;
import org.tikv.raw.RawKVClient;
import shade.com.google.common.base.Charsets;
import shade.com.google.protobuf.ByteString;
/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "1" );
        TiConfiguration conf = TiConfiguration.createRawDefault("0.0.0.0:32798");
        System.out.println( "2" );
        TiSession session = TiSession.create(conf);
        System.out.println( "3" );
        RawKVClient client = session.createRawClient();
        System.out.println( "4" );
        client.put(ByteString.copyFrom("hello-key", Charsets.UTF_8), ByteString.copyFrom("hello-message", Charsets.UTF_8));
        System.out.println(client.get(ByteString.copyFrom("hello-key", Charsets.UTF_8)));
        System.out.println( "Hello World!" );
    }
}

我有 docker-compose.yml(基于 https://github.com/pingcap/tidb-docker-compose/blob/master/docker-compose.yml ,我添加了 ports 并删除了所有多余的容器)

version: '2.1'
services:
  pd0:
    image: pingcap/pd:latest
    ports:
      - "2379"
    volumes:
      - ./config/pd.toml:/pd.toml:ro
      - ./data:/data
      - ./logs:/logs
    command:
      - --name=pd0
      - --client-urls=http://0.0.0.0:2379
      - --peer-urls=http://0.0.0.0:2380
      - --advertise-client-urls=http://pd0:2379
      - --advertise-peer-urls=http://pd0:2380
      - --initial-cluster=pd0=http://pd0:2380,pd1=http://pd1:2380,pd2=http://pd2:2380
      - --data-dir=/data/pd0
      - --config=/pd.toml
      - --log-file=/logs/pd0.log
    restart: on-failure
  pd1:
    image: pingcap/pd:latest
    ports:
      - "2379"
    volumes:
      - ./config/pd.toml:/pd.toml:ro
      - ./data:/data
      - ./logs:/logs
    command:
      - --name=pd1
      - --client-urls=http://0.0.0.0:2379
      - --peer-urls=http://0.0.0.0:2380
      - --advertise-client-urls=http://pd1:2379
      - --advertise-peer-urls=http://pd1:2380
      - --initial-cluster=pd0=http://pd0:2380,pd1=http://pd1:2380,pd2=http://pd2:2380
      - --data-dir=/data/pd1
      - --config=/pd.toml
      - --log-file=/logs/pd1.log
    restart: on-failure
  pd2:
    image: pingcap/pd:latest
    ports:
      - "2379"
    volumes:
      - ./config/pd.toml:/pd.toml:ro
      - ./data:/data
      - ./logs:/logs
    command:
      - --name=pd2
      - --client-urls=http://0.0.0.0:2379
      - --peer-urls=http://0.0.0.0:2380
      - --advertise-client-urls=http://pd2:2379
      - --advertise-peer-urls=http://pd2:2380
      - --initial-cluster=pd0=http://pd0:2380,pd1=http://pd1:2380,pd2=http://pd2:2380
      - --data-dir=/data/pd2
      - --config=/pd.toml
      - --log-file=/logs/pd2.log
    restart: on-failure
  tikv0:
    image: pingcap/tikv:latest
    ports:
      - "20160"
    volumes:
      - ./config/tikv.toml:/tikv.toml:ro
      - ./data:/data
      - ./logs:/logs
    command:
      - --addr=0.0.0.0:20160
      - --advertise-addr=tikv0:20160
      - --data-dir=/data/tikv0
      - --pd=pd0:2379,pd1:2379,pd2:2379
      - --config=/tikv.toml
      - --log-file=/logs/tikv0.log
    depends_on:
      - "pd0"
      - "pd1"
      - "pd2"
    restart: on-failure
  tikv1:
    image: pingcap/tikv:latest
    ports:
      - "20160"
    volumes:
      - ./config/tikv.toml:/tikv.toml:ro
      - ./data:/data
      - ./logs:/logs
    command:
      - --addr=0.0.0.0:20160
      - --advertise-addr=tikv1:20160
      - --data-dir=/data/tikv1
      - --pd=pd0:2379,pd1:2379,pd2:2379
      - --config=/tikv.toml
      - --log-file=/logs/tikv1.log
    depends_on:
      - "pd0"
      - "pd1"
      - "pd2"
    restart: on-failure
  tikv2:
    image: pingcap/tikv:latest
    ports:
      - "20160"    
    volumes:
      - ./config/tikv.toml:/tikv.toml:ro
      - ./data:/data
      - ./logs:/logs
    command:
      - --addr=0.0.0.0:20160
      - --advertise-addr=tikv2:20160
      - --data-dir=/data/tikv2
      - --pd=pd0:2379,pd1:2379,pd2:2379
      - --config=/tikv.toml
      - --log-file=/logs/tikv2.log
    depends_on:
      - "pd0"
      - "pd1"
      - "pd2"
    restart: on-failure

当我执行 curl 0.0.0.0:32810 时,我得到 @@。当我启动 java 代码时,我得到

1
2
Exception in thread "main" java.lang.NullPointerException: Failed to init client for PD cluster.
        at shade.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:228)
        at org.tikv.common.PDClient.initCluster(PDClient.java:351)
        at org.tikv.common.PDClient.createRaw(PDClient.java:378)
        at org.tikv.common.TiSession.<init>(TiSession.java:37)
        at org.tikv.common.TiSession.create(TiSession.java:45)
        at com.pv.app.App.main(App.java:21)

如何解决这个问题?

您必须使用与 docker 中相同的网络在 docker 中启动您的 java 应用程序-通过以下 ip 组合和连接:pd0:2379