使用datastax java驱动程序连接到本地cassandra节点?
问题描述:
我正在使用datastax java驱动程序3.1.0连接到cassandra群集,我的cassandra群集版本是2.0.10。使用datastax java驱动程序连接到本地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);
// 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;
}
}
什么是我需要使用连接到本地节点卡桑德拉第一,如果他们都放下,然后只谈论到远程节点的设置。另外我的池配置选项就在这里,我在上面的代码中使用?
答
默认情况下,datastax驱动程序将只连接到本地DC中的节点。如果您不使用withLocalDc
,它将尝试从它能够连接的联系点的DC中辨别本地数据中心。
如果你想在驱动程序无法在远程数据中心(S)主持,你应该使用withUsedHostsPerRemoteDc
,即:
cluster.builder()
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder()
.withLocalDc("DC1")
.withUsedHostsPerRemoteDc(3).build())
利用这种结构,驱动程序将建立在每个远程3个主机的连接DC,并且只在本地数据中心的所有主机关闭时才向它们发送查询。
还有其他策略可用于故障转移到远程数据中心。例如,您可以在与C *数据中心相同的物理数据中心中运行应用程序客户端,然后当物理数据中心发生故障时,可以在更高级别(例如负载均衡器)进行故障转移。
另外我的池配置选项就在这里,我在上面的代码中使用?
我认为你有什么是好的。默认值也很好。
假设我们在每个数据中心没有3个节点,那么如果我们在那里指定'3',会发生什么?或者它必须是数据中心中的机器数量? – john
3是一个天花板,所以如果你只有2个节点在远程DC,它将连接到2. –
我有一个与datastax java驱动程序相关的更多问题[here](http://stackoverflow.com/questions/41757029 /如何对绑定值到绑定的语句-IN-A-通用路 - 使用 - datastax-java的驱动器)。想看看你能帮我吗? – john