我已经设法在Microsoft Azure中设置Cassandra集群。目前,Azure中的2个虚拟机上包含2个节点。我一直在使用OpsCenter来检查群集的状态,并且一切都看起来很有前景。不过,我已经为集群创建了一个简单的C#测试客户端,使用DataStax C#Driver连接到实际正在工作但真的非常慢的集群。使用DataStax C#驱动程序执行Cassandra的速度缓慢
static class SimpleClient
{
private static Session _session;
public static Session Session
{
get
{
return _session;
}
}
private static Cluster _cluster;
public static Cluster Cluster
{
get
{
return _cluster;
}
}
public static void Connect(String node)
{
Console.WriteLine("Connecting to " + node);
_cluster = Cluster.Builder().AddContactPoint(node).Build();
_session = _cluster.Connect();
Metadata metadata = _cluster.Metadata;
Console.WriteLine("Connected to cluster: " + metadata.ClusterName.ToString());
}
public static void Close()
{
_cluster.Shutdown();
}
public static void CreateTable()
{
Console.WriteLine("Creating table with name test1");
_session.Execute(" CREATE TABLE kstt.test1 (identifier text PRIMARY KEY, name text); ");
Console.WriteLine("Table created with name test1");
}
public static void InsertToTable()
{
Console.WriteLine("Inserting data into test1");
_session.Execute(" INSERT INTO kstt.test1 (identifier, name) VALUES ('" + "hello" + "', '" + "man" + "');");
Console.WriteLine("Data inserted into test1");
}
public static void ReadFromTable(int times)
{
Console.WriteLine("Reading data from test1");
for (int i = 0; i < times; i++)
{
RowSet results = _session.Execute(" SELECT * FROM kstt.test1; ");
foreach (CqlColumn cqlColumn in results.Columns)
{
Console.WriteLine("Keyspace: " + cqlColumn.Keyspace + " # Table: " + cqlColumn.Table + " # Name: " + cqlColumn.Name);
}
}
Console.WriteLine("Data was read from test1");
}
public static void DropTable()
{
Console.WriteLine("Dropping table test1");
try
{
_session.Execute(" DROP TABLE kstt.test1; ");
}
catch { }
Console.WriteLine("Dropped table test1");
}
}
此代码实际上可以工作。但它的速度很慢,大约需要10秒钟才能连接,并且需要10秒钟左右才能执行查询。我认为这与使用cassandra.yaml设置在Azure中构建的Load Balancer有关。
我也注意到集群正在返回2个IP。一个是集群的外部IP,另一个是一个特定节点的内部IP,当然这是从外部无法访问的。
这是我们的设置:在端口9042上的端口
负载平衡器9160
卡桑德拉节点1与外部IP 66.55.44.33有内部IP 33.44.33.44
负载平衡器
cassandra-node2与外部IP 66.55.44.33与内部IP 11.22.11.22
卡桑德拉YAML
监听地址卡桑德拉节点1:33.44.33.44 RPC地址卡桑德拉节点1:33.44.33.44
监听地址卡桑德拉节点2:11.22.11.22 RPC地址cassandra- node2:11.22.11.22
有时候,程序在执行查询时甚至会以WriteTimeoutException结束。
什么是你的ping时间,无论你正在运行的代码卡桑德拉节点有所改善? 10秒显然是不合理的,我没有看到你的代码有什么问题。 –