使用datastax java驱动程序连接到本地cassandra节点? [英] connect to local cassandra nodes using datastax java driver?

查看:492
本文介绍了使用datastax java驱动程序连接到本地cassandra节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用datastax java驱动程序3.1.0连接到cassandra集群,而我的cassandra集群版本是2.0.10。

I am using datastax java driver 3.1.0 to connect to cassandra cluster and my cassandra cluster version is 2.0.10.

以下是我正在使用的单例类连接到cassandra集群。

Below is the singleton class I am using to connect to cassandra cluster.

public class CassUtil {
  private static final Logger LOGGER = Logger.getInstance(CassUtil.class);

  private Session session;
  private Cluster cluster;

  private static class Holder {
    private static final CassUtil INSTANCE = new CassUtil();
  }

  public static CassUtil getInstance() {
    return Holder.INSTANCE;
  }

  private CassUtil() {
    List<String> servers = TestUtils.HOSTNAMES;
    String username =
        TestUtils.loadCredentialFile().getProperty(TestUtils.USERNAME);
    String password =
        TestUtils.loadCredentialFile().getProperty(TestUtils.PASSWORD);

    // is this right setting?
    PoolingOptions poolingOptions = new PoolingOptions();
    poolingOptions.setConnectionsPerHost(HostDistance.LOCAL, 4, 10).setConnectionsPerHost(
        HostDistance.REMOTE, 2, 4);

    Builder builder = Cluster.builder();
    cluster =
        builder
            .addContactPoints(servers.toArray(new String[servers.size()]))
            .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
            .withPoolingOptions(poolingOptions)
            .withReconnectionPolicy(new ConstantReconnectionPolicy(100L))
            .withLoadBalancingPolicy(
                DCAwareRoundRobinPolicy
                    .builder()
                    .withLocalDc(
                        !TestUtils.isProduction() ? "DC2" : TestUtils.getCurrentLocation()
                            .get().name().toLowerCase()).build())
            .withCredentials(username, password).build();

    try {
      session = cluster.connect("testkeyspace");
      StringBuilder sb = new StringBuilder();
      Set<Host> allHosts = cluster.getMetadata().getAllHosts();
      for (Host host : allHosts) {
        sb.append("[");
        sb.append(host.getDatacenter());
        sb.append(host.getRack());
        sb.append(host.getAddress());
        sb.append("]");
      }
      LOGGER.logInfo("connected: " + sb.toString());
    } catch (NoHostAvailableException ex) {
      LOGGER.logError("error= ", ExceptionUtils.getStackTrace(ex));
    } catch (Exception ex) {
      LOGGER.logError("error= " + ExceptionUtils.getStackTrace(ex));
    }
  }

  public void shutdown() {
    LOGGER.logInfo("Shutting down the whole cassandra cluster");
    if (null != session) {
      session.close();
    }
    if (null != cluster) {
      cluster.close();
    }
  }

  public Session getSession() {
    if (session == null) {
      throw new IllegalStateException("No connection initialized");
    }
    return session;
  }

  public Cluster getCluster() {
    return cluster;
  }
}

我需要使用什么设置才能连接到首先是本地cassandra节点,如果它们已关闭,则只与远程节点通信。我在上面的代码中使用了我的池配置选项吗?

What is the settings I need to use to connect to local cassandra nodes first and if they are down, then only talk to remote nodes. Also my pooling configuration options is right here which I am using in the above code?

推荐答案

默认情况下,数据存储驱动程序只会连接到本地DC中的节点。如果您不使用 withLocalDc ,它将尝试从其能够连接的联系点的DC识别本地数据中心。

By default the datastax drivers will only connect to nodes in the local DC. If you do not use withLocalDc it will attempt to discern the local datacenter from the DC of the contact point it is able to connect to.

如果您希望驱动程序故障转移到远程数据中心托管,则应使用 withUsedHostsPerRemoteDc ,即:

If you want the driver to fail over to host in remote data center(s) you should use withUsedHostsPerRemoteDc, i.e.:

cluster.builder()        
  .withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder()
    .withLocalDc("DC1")
    .withUsedHostsPerRemoteDc(3).build())

使用此配置,驱动程序将建立与每个远程DC中3个主机的连接,并且只有在本地数据中心中的所有主机都关闭时才向它们发送查询。

With this configuration, the driver will establish connections to 3 hosts in each remote DC, and only send queries to them if all hosts in the local datacenter is down.

还有其他策略可以故障转移到远程数据ce nters。例如,您可以在与C *数据中心相同的物理数据中心中运行应用程序客户端,然后当物理数据中心发生故障时,您可以在更高级别进行故障转移(如负载均衡器)。

There are other strategies for failover to remote data centers. For example, you could run your application clients in each same physical data center as your C* data centers, and then when a physical data center fails, you can fail over at a higher level (like your load balancer).


我的池配置选项就在这里,我在上面的代码中使用?

Also my pooling configuration options is right here which I am using in the above code?

我觉得你的一切都很好。默认值也很好。

I think what you have is fine. The defaults are fine too.

这篇关于使用datastax java驱动程序连接到本地cassandra节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆