Java 如何使用所有四个节点连接到cassandra数据库

Java 如何使用所有四个节点连接到cassandra数据库,java,cassandra,astyanax,Java,Cassandra,Astyanax,我最近开始使用Cassandra数据库,我正在使用Netflix客户端填充和读取Cassandra数据库中的数据 我有一个有四个节点的集群。我已经创建了这样的键空间- create keyspace profilekeyspace with placement_strategy = 'NetworkTopologyStrategy' and strategy_options = {DC2 : 1, DC1 : 1} and dura

我最近开始使用Cassandra数据库,我正在使用Netflix客户端填充和读取Cassandra数据库中的数据

我有一个有四个节点的集群。我已经创建了这样的键空间-

        create keyspace profilekeyspace
        with placement_strategy = 'NetworkTopologyStrategy'
        and strategy_options = {DC2 : 1, DC1 : 1}
        and durable_writes = true;
而我的专栏族名称是-
profile\u columnfamily

这是我的四个节点-

      lp-host01.vip.slc.qa.host.com:9160
      lp-host02.vip.slc.qa.host.com:9160
      lp-host03.vip.phx.qa.host.com:9160
      lp-host04.vip.phx.qa.host.com:9160
现在,我只使用上面的一个节点连接到Cassandra数据库并填充数据。但我的DBA说,您需要使用所有四个节点来建立连接

private AstyanaxContext<Keyspace> context;

private CassandraAstyanaxConnection() {

    context = new AstyanaxContext.Builder()
    .forCluster("TEST CLUSTER")
    .forKeyspace("PROFILE")
    .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()     
        .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
    )
    .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
        .setPort(9160)
        .setMaxConnsPerHost(1)
        .setSeeds("lp-host01.vip.slc.qa.host.com:9160:9160")//using only node from above to make the connection
    )
    .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()     
        .setCqlVersion("3.0.0")
        .setTargetCassandraVersion("1.2"))
    .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
    .buildKeyspace(ThriftFamilyFactory.getInstance());

    context.start();
    keyspace = context.getEntity();

    emp_cf = ColumnFamily.newColumnFamily(
        "PROFILE_COLUMNFAMILY",
        StringSerializer.get(),
        StringSerializer.get());
}
专用AstyanXContext上下文;
私人CassandraAstyanaxConnection(){
context=new AstyanaxContext.Builder()
.forCluster(“测试集群”)
.forKeyspace(“配置文件”)
.withAstyanaxConfiguration(新的AstyanaxConfiguration-Impl())
.setDiscoveryType(NodeDiscoveryType.RING_描述)
)
.withConnectionPoolConfiguration(新的ConnectionPoolConfigurationMPL(“MyConnectionPool”)
.设置端口(9160)
.setMaxConnsPerHost(1)
.setSeeds(“lp-host01.vip.slc.qa.host.com:9160:9160”)//仅使用上面的节点建立连接
)
.withAstyanaxConfiguration(新的AstyanaxConfiguration-Impl())
.setCqlVersion(“3.0.0”)
.setTargetCassandraVersion(“1.2”))
.withConnectionPoolMonitor(新计数ConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance());
context.start();
keyspace=context.getEntity();
emp_cf=ColumnFamily.newColumnFamily(
“家庭简介”,
StringSerializer.get(),
StringSerializer.get());
}
现在我不知道如何使用Netflix客户端使用所有四个节点进行连接?有人能帮我吗


谢谢您的帮助。

种子列表是一个逗号分隔的列表。因此,您只需在setSeeds调用中添加其余部分:

setseed(“服务器1:9160、服务器2:9160、服务器3:9160”)

此外,Astyanax将发现环中的其他服务器。您只需列出一个即可发现所有其他服务器,但如果该服务器已关闭,则需要列出更多。这与cassandra.yaml中的种子列表非常相似

注意,您已在线路中复制了端口:

.setSeeds(“lp-host01.vip.slc.qa.host.com:9160:9160”)


是的,我会纠正的。错了。一般来说,你是说我们应该只使用一个节点来建立连接。但出于某种原因,假设特定节点已关闭?那么与Cassandra数据库的整个连接将丢失?因此,在这种情况下,最好的方法是连接到所有四个节点,如果其中任何一个节点断开,可以使用其他节点进行连接?对吗?给定给setSeeds的列表不是要连接到的节点列表,而是种子用来发现所有其余节点的节点列表。然后,Astyanax连接到所有这些设备。然而,如果你有一个单一的种子节点,但它关闭了,那么当你的应用程序启动时,它将找不到任何节点,因此将失败。因此,您希望将一些节点列为种子,但不必全部列出。谢谢Richard。这完全有道理。最后一个问题,您是否可以检查我连接Cassandra数据库的方式是否存在任何问题,这会妨碍读取数据时的性能?正如上面的代码片段,将在我的Singleton类中,因此我只创建一次连接,然后每次都重用它。这将创建一个连接池。只要将它与多线程访问结合使用,就会获得良好的性能。是的,我使用多线程代码,只创建一次连接,然后每次都重用它。你觉得应该没问题吧?