以下代码有什么问题,为什么Cassandra中的负载平衡不起作用?
const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['172.30.56.63','172.30.36.129'], keyspace: 'qnapstat',
policies : { loadBalancing : new cassandra.policies.loadBalancing.RoundRobinPolicy } });
const query = 'SELECT * FROM nodedata WHERE partitionkey = ?';
async function read() {
console.log(client.options.policies.loadBalancing);
const params = ['testdata'];
var response = await client.execute(query, params, { prepare: true, consistency: cassandra.types.consistencies.quorum });
console.log(response.info);
process.exit();
}
read();
我打印了尝试过的主机,我能够看到它总是试图将172.30.36.129作为协调器,而不是另一个。
答案 0 :(得分:1)
我误解了,只有使用相同的客户端连接才能使用该策略,为了明确以下代码按预期工作,
const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['172.30.56.60','172.30.56.61,172.30.56.62'], keyspace: 'qnapstat',
policies : { loadBalancing : new cassandra.policies.loadBalancing.RoundRobinPolicy
} });
var index = 0;
var totalcount = 10;
const query = 'SELECT readiops FROM nodedata where nodeip = ?';
async function read() {
console.log(client.options.policies.loadBalancing);
console.log(client.options.policies.retry);
while(index < totalcount) {
const params = ['testdata'];
var response = await client.execute(query, params, { prepare: true, consistency: cassandra.types.consistencies.quorum });
console.log(response.info);
index = index + 1;
}
process.exit();
}
read();
谢谢,
哈利