Datastax Cassandra Driver 始终尝试连接到本地主机,即使它未配置为这样做

Datastax Cassandra Driver always attempts to connect to localhost, even though it's not configured to do so

所以我有以下客户端代码:

def getCluster:Session = {
    import collection.JavaConversions._
    val endpoints = config.getStringList("cassandra.server")
    val keyspace = config.getString("cassandra.keyspace")

    val clusterBuilder = Cluster.builder

    endpoints.toTraversable.map { x =>
      clusterBuilder.addContactPoint(x)
    }
    val cluster = clusterBuilder.build
    cluster
      .getConfiguration
      .getProtocolOptions
      .setCompression(ProtocolOptions.Compression.LZ4)
    cluster.connect(keyspace)}

这是无耻地从 datastax 的驱动程序文档中的示例中借用的。

当我尝试用它执行代码时,它总是尝试连接到本地主机,即使它没有为此配置...

在某些情况下,它会连接(基本读取)但对于写入,我收到以下日志消息:

 2016-07-07 11:34:31 DEBUG Connection:157 - Connection[/127.0.0.1:9042-10, inFlight=0, closed=false] Error connecting to /127.0.0.1:9042 (Connection refused: /127.0.0.1:9042)
2016-07-07 11:34:31 DEBUG STATES:404 - Defuncting Connection[/127.0.0.1:9042-10, inFlight=0, closed=false] because: [/127.0.0.1] Cannot connect
2016-07-07 11:34:31 DEBUG STATES:108 - [/127.0.0.1:9042] Connection[/127.0.0.1:9042-10, inFlight=0, closed=false] failed, remaining = 0
2016-07-07 11:34:31 DEBUG Connection:629 - Connection[/127.0.0.1:9042-10, inFlight=0, closed=true] closing connection
2016-07-07 11:34:31 DEBUG Cluster:1802 - Aborting onDown because a reconnection is running on DOWN host /127.0.0.1:9042
2016-07-07 11:34:31 DEBUG Cluster:1872 - Failed reconnection to /127.0.0.1:9042 ([/127.0.0.1] Cannot connect), scheduling retry in 512000 milliseconds
2016-07-07 11:34:31 DEBUG STATES:196 - [/127.0.0.1:9042] next reconnection attempt in 512000 ms

我想不通where/what我需要在驱动程序端配置(没有本地客户端,它只是驱动程序)来纠正这个问题

我的猜测是,这是由您的 cassandra 节点上的 cassandra.yaml 文件配置引起的。影响这个的两个主要设置是 broadcast_rpc_addressrpc_address,来自 The cassandra.yaml configuration 参考:

broadcast_rpc_address

(Default: unset) RPC address to broadcast to drivers and other Cassandra nodes. This cannot be set to 0.0.0.0. If blank, it is set to the value of the rpc_address or rpc_interface. If rpc_address or rpc_interfaceis set to 0.0.0.0, this property must be set.

rpc_address

(Default: localhost) The listen address for client connections (Thrift RPC service and native transport).

如果您将这两个都保留为默认值,localhost 将是 cassandra 进行通信以进行连接的默认地址。

驱动程序能够连接到联系点后,它会查询联系点的system.localsystem.peers table以确定要连接到哪些主机,地址table 的通信来自 rpc_address/broadcast_rpc_address