使用Node.js进行Cassandra状态检查 [英] Cassandra status check using nodejs
问题描述
我在三个环境中使用nodejs,而Cassandra在所有三个节点中运行。
I use the nodejs in three environments and the Cassandra is running in all the three nodes.
我完全理解使用 nodetool status
我将能够获取每个节点的状态。但是问题是如果我的当前节点关闭,那么我将无法在当前节点中执行nodetool状态,那么有没有办法使用nodejs Cassandra驱动程序来获取状态?
I totally understand using nodetool status
I will be able to get the status of each node. But the problem is If my current node is down then I will not be able to perform nodetool status in the current node, So Is there a way to get the status using nodejs Cassandra driver?
我们将不胜感激。
已编辑:
EDITED :
按照dilsingi的建议,我使用了client.hosts,但问题是,在以下群集中,172.30.56.61仍然关闭,但仍显示为可用。
如何获取每个节点的状态?
As per dilsingi's suggestion, I used the client.hosts but the problem is, In the following cluster, 172.30.56.61 is down still it is showing as available. How to get the status of each node?
const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['172.30.56.60','172.30.56.61','172.30.56.62'], keyspace: 'test', policies : { loadBalancing : new cassandra.policies.loadBalancing.RoundRobinPolicy }});
async function read() {
client.connect().then(function () {
console.log('Connected to cluster with %d host(s): %j', client.hosts.length, client.hosts.keys());
client.hosts.forEach(function (host) {
console.log(host.address, host.datacenter, host.rack);
});
});
}
read();
nodeTool状态输出:
nodeTool status output :
Datacenter: newyork
===================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 172.30.56.62 1.13 MiB 256 34.8% e93827b7-ba43-4fba-8a51-4876832b5b22 rack1
DN 172.30.56.60 1.61 MiB 256 33.4% e385af22-803e-4313-bee2-16219f73c213 rack1
UN 172.30.56.61 779.4 KiB 256 31.8% be7fc52e-c45d-4380-85a3-4cbf6d007a5d rack1
Node Js Code :
const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['172.30.56.60','172.30.56.61','172.30.56.62'], keyspace: 'qcs', policies : { loadBalancing : new cassandra.policies.loadBalancing.RoundRobinPolicy }});
async function read() {
client.connect().then(function () {
console.log('Connected to cluster with %d host(s): %j', client.hosts.length, client.hosts.keys());
client.hosts.forEach(function (host) {
console.log(host.address, host.datacenter, host.rack, host.isUp(), host.canBeConsideredAsUp());
});
});
}
read();
NodeJs输出:
Connected to cluster with 3 host(s): ["172.30.56.60:9042","172.30.56.61:9042","172.30.56.62:9042"]
172.30.56.60:9042 newyork rack1 true true
172.30.56.61:9042 newyork rack1 true true
172.30.56.62:9042 newyork rack1 true true
推荐答案
包括(nodejs)在内的一般驱动程序都知道整个Cassandra群集拓扑。首次与连接字符串中的一个或多个节点ip地址联系后,驱动程序可以自动识别组成cassandra环的所有节点ip。它足够智能,可以知道何时节点出现故障或新节点加入集群。它甚至可以继续使用比最初的节点更多的全新节点(ips)。
The drivers in general including (nodejs) are aware of the entire Cassandra cluster Topology. Upon the initial contact with the one or more node ip address in connection string, driver can automatically identify all the node ips that make up the cassandra ring. Its intelligent enough to know when a node goes down or a new node joins the cluster. It can even continue working with completely new nodes (ips) than what it began with.
因此不需要为节点状态编码,因为驱动程序会自动为您处理。建议在连接字符串中提供大于1 ip的值,以便在进行初始连接时提供冗余。
So there isn't a requirement to code for node status, as driver automatically handles that for you. Its recommended to provide more than 1 ip in the connection string, so as to provide redundancy while making initial connection.
这是nodejs驱动程序文档和此部分描述自动节点发现功能和集群和架构元数据。
Here is the nodejs driver documentation and this section describe the "Auto node discovery" feature and "Cluster & Schema Metadata".
这篇关于使用Node.js进行Cassandra状态检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!