cassandra本地连接失败_使用datastax java驱动程序连接到本地cassandra节点?

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

下面是我用来连接cassandra集群的单例类。

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 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 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节点,如果它们已关闭,则仅与远程节点通信。我的池配置选项也就在这里,上面的代码中正在使用该选项?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值