【发布时间】:2016-11-23 21:55:27
【问题描述】:
我正在使用 datastax java driver 3.1.0 连接到 cassandra。我想始终连接到本地 cassandra 节点,而不是连接到远程 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<String> servers = TestUtils.HOSTNAMES;
String username =
TestUtils.loadCredentialFile().getProperty(TestUtils.USERNAME);
String password =
TestUtils.loadCredentialFile().getProperty(TestUtils.PASSWORD);
// this code throws exception
PoolingOptions opts = new PoolingOptions();
opts.setCoreConnectionsPerHost(HostDistance.LOCAL,
opts.getCoreConnectionsPerHost(HostDistance.LOCAL));
Builder builder = Cluster.builder();
cluster =
builder
.addContactPoints(servers.toArray(new String[servers.size()]))
.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
.withPoolingOptions(opts)
.withReconnectionPolicy(new ConstantReconnectionPolicy(100L))
.withLoadBalancingPolicy(
DCAwareRoundRobinPolicy
.builder()
.withLocalDc(
!TestUtils.isProduction() ? "ABC2" : TestUtils.getCurrentLocation()
.get().name().toLowerCase()).build())
.withCredentials(username, password).build();
try {
session = cluster.connect("testkeyspace");
} catch (NoHostAvailableException ex) {
LOGGER.logError("error= ", ExceptionUtils.getStackTrace(ex));
} catch (Exception ex) {
LOGGER.logError("error= " + ExceptionUtils.getStackTrace(ex));
}
}
}
每当我在上面运行代码时,它都会抛出异常:
Caused by: java.lang.IllegalArgumentException: core number of connections must be positive
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)
at com.datastax.driver.core.PoolingOptions.setCoreConnectionsPerHost(PoolingOptions.java:199)
我还想确保我的客户端始终与本地 cassandra 节点连接,而不是与任何远程 cassandra 节点连接。
【问题讨论】:
标签: java cassandra datastax-java-driver