无法从主机连接到cassandra

时间:2016-03-01 05:51:19

标签: docker cassandra

我已经使用docker在我的mac book pro上配置了一个cassandra节点,如下所示

VBoxManage modifyvm "default" --natpf1 "tcp-port7191,tcp,,7191,,7191"
VBoxManage modifyvm "default" --natpf1 "tcp-port7000,tcp,,7000,,7000"
VBoxManage modifyvm "default" --natpf1 "tcp-port7001,tcp,,7001,,7001"
VBoxManage modifyvm "default" --natpf1 "tcp-port9160,tcp,,7160,,7160"
VBoxManage modifyvm "default" --natpf1 "tcp-port9042,tcp,,9042,,9042"

(重启机器)

docker run --name c1 -v /Users/MyProjects/scripts/:/script -d cassandra:latest -p "7191:7191" -p "7000:7000" -p "7001:7001" -p "9160:9160" -p "9042:9042"

我可以轻松做到

docker exec -it c1 cqlsh

它说

Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.0.1 | CQL spec 3.3.1 | Native protocol v4]
Use HELP for help.

现在我使用

获取虚拟盒vm的IP地址
docker-machine env default

我可以看到192.168.99.100

的IP地址

但是当我运行我的java程序以使用上面的IP地址连接到同一个cassandra实例时。我收到错误

    00:00:56.611 [cluster1-nio-worker-0] DEBUG com.datastax.driver.core.Connection - Connection[/192.168.99.100:9042-1, inFlight=0, closed=false] Error connecting to /192.168.99.100:9042 (Connection refused: /192.168.99.100:9042)
00:00:56.615 [cluster1-nio-worker-0] DEBUG com.datastax.driver.core.Host.STATES - Defuncting Connection[/192.168.99.100:9042-1, inFlight=0, closed=false] because: [/192.168.99.100] Cannot connect
00:00:56.616 [cluster1-nio-worker-0] DEBUG com.datastax.driver.core.Connection - Connection[/192.168.99.100:9042-1, inFlight=0, closed=true] closing connection
00:00:56.617 [cluster1-nio-worker-0] DEBUG com.datastax.driver.core.Host.STATES - [/192.168.99.100:9042] preventing new connections for the next 1000 ms
00:00:56.617 [cluster1-nio-worker-0] DEBUG com.datastax.driver.core.Host.STATES - [/192.168.99.100:9042] Connection[/192.168.99.100:9042-1, inFlight=0, closed=true] failed, remaining = 0
00:00:56.624 [run-main-0] DEBUG c.d.driver.core.ControlConnection - [Control connection] error on /192.168.99.100:9042 connection, no more host to try
com.datastax.driver.core.exceptions.TransportException: [/192.168.99.100] Cannot connect
    at com.datastax.driver.core.Connection$1.operationComplete(Connection.java:157) ~[cassandra-driver-core-3.0.0.jar:na]
    at com.datastax.driver.core.Connection$1.operationComplete(Connection.java:140) ~[cassandra-driver-core-3.0.0.jar:na]
    at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:680) ~[netty-common-4.0.33.Final.jar:4.0.33.Final]
    at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:603) ~[netty-common-4.0.33.Final.jar:4.0.33.Final]
    at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:563) ~[netty-common-4.0.33.Final.jar:4.0.33.Final]
    at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:424) ~[netty-common-4.0.33.Final.jar:4.0.33.Final]
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:276) ~[netty-transport-4.0.33.Final.jar:4.0.33.Final]
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:292) ~[netty-transport-4.0.33.Final.jar:4.0.33.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:528) ~[netty-transport-4.0.33.Final.jar:4.0.33.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) ~[netty-transport-4.0.33.Final.jar:4.0.33.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) ~[netty-transport-4.0.33.Final.jar:4.0.33.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) ~[netty-transport-4.0.33.Final.jar:4.0.33.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112) ~[netty-common-4.0.33.Final.jar:4.0.33.Final]
    at java.lang.Thread.run(Thread.java:745) ~[na:1.7.0_79]
Caused by: java.net.ConnectException: Connection refused: /192.168.99.100:9042
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.7.0_79]
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:739) ~[na:1.7.0_79]
    at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:224) ~[netty-transport-4.0.33.Final.jar:4.0.33.Final]
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:289) ~[netty-transport-4.0.33.Final.jar:4.0.33.Final]
    ... 6 common frames omitted
00:00:56.625 [run-main-0] DEBUG com.datastax.driver.core.Cluster - Shutting down
[error] (run-main-0) java.lang.ExceptionInInitializerError
java.lang.ExceptionInInitializerError
    at com.abhi.connector.CassandraConnector$class.$init$(CassandraConnector.scala:8)
    at com.abhi.models.Movies$.<init>(Movies.scala:25)
    at com.abhi.models.Movies$.<clinit>(Movies.scala)
    at com.abhi.MovieLensDataPreperation$$anonfun$storeInCassandra$1.apply(MovieLensDataPreperation.scala:55)
    at com.abhi.MovieLensDataPreperation$$anonfun$storeInCassandra$1.apply(MovieLensDataPreperation.scala:55)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at com.abhi.MovieLensDataPreperation$.storeInCassandra(MovieLensDataPreperation.scala:55)
    at com.abhi.MovieLensDataPreperation$.main(MovieLensDataPreperation.scala:51)
    at com.abhi.MovieLensDataPreperation.main(MovieLensDataPreperation.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /192.168.99.100:9042 (com.datastax.driver.core.exceptions.TransportException: [/192.168.99.100] Cannot connect))
    at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:231)
    at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:77)
    at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1414)
    at com.datastax.driver.core.Cluster.init(Cluster.java:162)
    at com.datastax.driver.core.Cluster.connectAsync(Cluster.java:333)
    at com.datastax.driver.core.Cluster.connect(Cluster.java:283)
    at com.abhi.connector.Connector$.<init>(CassandraConnector.scala:22)
    at com.abhi.connector.Connector$.<clinit>(CassandraConnector.scala)
    at com.abhi.connector.CassandraConnector$class.$init$(CassandraConnector.scala:8)
    at com.abhi.models.Movies$.<init>(Movies.scala:25)
    at com.abhi.models.Movies$.<clinit>(Movies.scala)
    at com.abhi.MovieLensDataPreperation$$anonfun$storeInCassandra$1.apply(MovieLensDataPreperation.scala:55)
    at com.abhi.MovieLensDataPreperation$$anonfun$storeInCassandra$1.apply(MovieLensDataPreperation.scala:55)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at com.abhi.MovieLensDataPreperation$.storeInCassandra(MovieLensDataPreperation.scala:55)
    at com.abhi.MovieLensDataPreperation$.main(MovieLensDataPreperation.scala:51)
    at com.abhi.MovieLensDataPreperation.main(MovieLensDataPreperation.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

这是我配置java连接的代码

Cluster.builder()
  .addContactPoints("192.168.99.100").withPort(9042)
  .build()

编辑:我还用docker guest虚拟机的IP地址替换了上面的IP地址......但是没有解决问题。

1 个答案:

答案 0 :(得分:3)

我在创建docker容器时使用CASSANDRA_BROADCAST_ADDRESS解决了这个问题

这是我使用的命令

docker run --name c1 -v /Users/MyProjects/scripts/:/script -d -p "7191:7191" 
-p "7000:7000" -p "7001:7001" -p "9160:9160" -p "9042:9042" -e 
CASSANDRA_BROADCAST_ADDRESS=192.168.99.100 cassandra:latest

像这样创建容器之后。我可以使用此代码从我的Scala应用程序连接

  val keyspace: KeySpace = new KeySpace("foo")

  val cluster =
    Cluster.builder()
      .addContactPoint("192.168.99.100").withPort(9042)
      .build()

  cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(100000);
  val session: Session = cluster.connect(keyspace.name)