使用 datastax java 驱动程序连接到本地 cassandra 节点?
connect to local cassandra nodes using datastax java driver?
我正在使用 datastax java 驱动程序 3.1.0 连接到 cassandra 集群,我的 cassandra 集群版本是 2.0.10。
下面是我用来连接cassandra集群的单例class。
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 节点,如果它们已关闭,则仅与远程节点通信。另外,我在上面的代码中使用的池配置选项就在这里?
默认情况下,datastax 驱动程序将仅连接到本地 DC 中的节点。如果您不使用 withLocalDc
,它将尝试从它能够连接到的接触点的 DC 中辨别本地数据中心。
如果您希望驱动程序故障转移到远程数据中心的主机,您应该使用 withUsedHostsPerRemoteDc
,即:
cluster.builder()
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder()
.withLocalDc("DC1")
.withUsedHostsPerRemoteDc(3).build())
使用此配置,驱动程序将与每个远程 DC 中的 3 台主机建立连接,并且仅在本地数据中心中的所有主机都关闭时才向它们发送查询。
还有其他策略可以将故障转移到远程数据中心。例如,您可以 运行 您的应用程序客户端位于与 C* 数据中心相同的每个物理数据中心,然后当物理数据中心发生故障时,您可以在更高级别(如负载均衡器)进行故障转移。
Also my pooling configuration options is right here which I am using in the above code?
我觉得你的东西很好。默认值也很好。
我正在使用 datastax java 驱动程序 3.1.0 连接到 cassandra 集群,我的 cassandra 集群版本是 2.0.10。
下面是我用来连接cassandra集群的单例class。
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 节点,如果它们已关闭,则仅与远程节点通信。另外,我在上面的代码中使用的池配置选项就在这里?
默认情况下,datastax 驱动程序将仅连接到本地 DC 中的节点。如果您不使用 withLocalDc
,它将尝试从它能够连接到的接触点的 DC 中辨别本地数据中心。
如果您希望驱动程序故障转移到远程数据中心的主机,您应该使用 withUsedHostsPerRemoteDc
,即:
cluster.builder()
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder()
.withLocalDc("DC1")
.withUsedHostsPerRemoteDc(3).build())
使用此配置,驱动程序将与每个远程 DC 中的 3 台主机建立连接,并且仅在本地数据中心中的所有主机都关闭时才向它们发送查询。
还有其他策略可以将故障转移到远程数据中心。例如,您可以 运行 您的应用程序客户端位于与 C* 数据中心相同的每个物理数据中心,然后当物理数据中心发生故障时,您可以在更高级别(如负载均衡器)进行故障转移。
Also my pooling configuration options is right here which I am using in the above code?
我觉得你的东西很好。默认值也很好。