无法从主机连接到 Docker Aerospike
Couldn't connect to Docker Aerospike from host
我在 运行ning aerospike 服务器 docker。
$ docker run -d --name aerospike aerospike/aerospike-server
0ad3b2df67bd17f896e87ed119758d9af7fcdd9b82a8632828e01072e2c5673f
启动成功
$docker ps
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
0ad3b2df67bd aerospike/aerospike-server "/entrypoint.sh asd"
4 seconds ago Up 2 seconds 3000-3003/tcp aerospike
我使用以下命令找到了 docker 的 IP 地址。
$ docker inspect -f '{{.NetworkSettings.IPAddress }}' aerospike
172.17.0.2
当我尝试使用以下命令连接到 aql 时,它也成功了。
$ docker run -it aerospike/aerospike-tools aql -h $(docker inspect -f
'{{.NetworkSettings.IPAddress }}' aerospike)
Aerospike Query Client
Version 3.15.0.3
C Client Version 4.2.0
Copyright 2012-2017 Aerospike. All rights reserved.
aql> select * from test.person
0 rows in set (0.002 secs)
现在我正在尝试使用主机中的 java 客户端连接到 docker 中的 aerospike 服务器。
public class AerospikeDemo {
public static void main(String []args) {
AerospikeClient client = new AerospikeClient("172.17.0.2", 3000);
Key key = new Key("test", "demo", "putgetkey");
//Key key2 = new Key("1", "2", "3");
Bin bin1 = new Bin("bin1", "value1");
Bin bin2 = new Bin("bin2", "value2");
Bin bin3 = new Bin("bin2", "value3");
// Write a record
client.put(null, key, bin1, bin2, bin3);
// Read a record
Record record = client.get(null, key);
System.out.println("record is "+ record);
System.out.println("record bins is " + record.bins);
client.close();
}
}
当我 运行 上述程序时,出现以下错误 -
objc[3446]: Class JavaLaunchHelper is implemented in both
/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java (0x10f7b14c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10f8794e0). One of the two will be used. Which one is undefined.
Exception in thread "main" com.aerospike.client.AerospikeException$Connection:
Error Code 11: Failed to connect to host(s): 172.17.0.2 3000 Error Code 11: java.net.SocketTimeoutException: connect timed out
at com.aerospike.client.cluster.Cluster.seedNodes(Cluster.java:413)
at com.aerospike.client.cluster.Cluster.tend(Cluster.java:306)
at com.aerospike.client.cluster.Cluster.waitTillStabilized(Cluster.java:271)
at com.aerospike.client.cluster.Cluster.initTendThread(Cluster.java:181)
at com.aerospike.client.AerospikeClient.<init>(AerospikeClient.java:210)
at com.aerospike.client.AerospikeClient.<init>(AerospikeClient.java:151)
at com.demo.aerospike.AerospikeDemo.main(AerospikeDemo.java:12)
AerospikeClient("172.17.0.2", 3000)
和 AerospikeClient("localhost", 3000)
我都试过了
我在 Dockerfile 中看到端口 3000 暴露给主机,但我不确定为什么我无法在 docker 中使用 aerospike 服务器。
IP 172.17.0.2
只能在 Docker 内访问(因此您可以使用另一个容器进行连接)。如果您想从您的主机连接,您需要映射相应的端口。
docker run -d --name aerospike -p 3000:3000 aerospike/aerospike-server
之后您可以使用:
AerospikeClient client = new AerospikeClient("localhost", 3000);
我在 运行ning aerospike 服务器 docker。
$ docker run -d --name aerospike aerospike/aerospike-server
0ad3b2df67bd17f896e87ed119758d9af7fcdd9b82a8632828e01072e2c5673f
启动成功
$docker ps
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
0ad3b2df67bd aerospike/aerospike-server "/entrypoint.sh asd"
4 seconds ago Up 2 seconds 3000-3003/tcp aerospike
我使用以下命令找到了 docker 的 IP 地址。
$ docker inspect -f '{{.NetworkSettings.IPAddress }}' aerospike
172.17.0.2
当我尝试使用以下命令连接到 aql 时,它也成功了。
$ docker run -it aerospike/aerospike-tools aql -h $(docker inspect -f
'{{.NetworkSettings.IPAddress }}' aerospike)
Aerospike Query Client
Version 3.15.0.3
C Client Version 4.2.0
Copyright 2012-2017 Aerospike. All rights reserved.
aql> select * from test.person
0 rows in set (0.002 secs)
现在我正在尝试使用主机中的 java 客户端连接到 docker 中的 aerospike 服务器。
public class AerospikeDemo {
public static void main(String []args) {
AerospikeClient client = new AerospikeClient("172.17.0.2", 3000);
Key key = new Key("test", "demo", "putgetkey");
//Key key2 = new Key("1", "2", "3");
Bin bin1 = new Bin("bin1", "value1");
Bin bin2 = new Bin("bin2", "value2");
Bin bin3 = new Bin("bin2", "value3");
// Write a record
client.put(null, key, bin1, bin2, bin3);
// Read a record
Record record = client.get(null, key);
System.out.println("record is "+ record);
System.out.println("record bins is " + record.bins);
client.close();
}
}
当我 运行 上述程序时,出现以下错误 -
objc[3446]: Class JavaLaunchHelper is implemented in both
/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java (0x10f7b14c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10f8794e0). One of the two will be used. Which one is undefined.
Exception in thread "main" com.aerospike.client.AerospikeException$Connection:
Error Code 11: Failed to connect to host(s): 172.17.0.2 3000 Error Code 11: java.net.SocketTimeoutException: connect timed out
at com.aerospike.client.cluster.Cluster.seedNodes(Cluster.java:413)
at com.aerospike.client.cluster.Cluster.tend(Cluster.java:306)
at com.aerospike.client.cluster.Cluster.waitTillStabilized(Cluster.java:271)
at com.aerospike.client.cluster.Cluster.initTendThread(Cluster.java:181)
at com.aerospike.client.AerospikeClient.<init>(AerospikeClient.java:210)
at com.aerospike.client.AerospikeClient.<init>(AerospikeClient.java:151)
at com.demo.aerospike.AerospikeDemo.main(AerospikeDemo.java:12)
AerospikeClient("172.17.0.2", 3000)
和 AerospikeClient("localhost", 3000)
我在 Dockerfile 中看到端口 3000 暴露给主机,但我不确定为什么我无法在 docker 中使用 aerospike 服务器。
IP 172.17.0.2
只能在 Docker 内访问(因此您可以使用另一个容器进行连接)。如果您想从您的主机连接,您需要映射相应的端口。
docker run -d --name aerospike -p 3000:3000 aerospike/aerospike-server
之后您可以使用:
AerospikeClient client = new AerospikeClient("localhost", 3000);