c# - 如何连接到 Cassandra 虚拟机

标签 c# .net .net-core cassandra

如果 Cassandra 和代码在同一台机器上,则以下代码有效:

using System;
using Cassandra;

namespace CassandraInsertTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var cluster = Cluster.Builder()
            .AddContactPoint("127.0.0.1")
            .Build();

            var session = cluster.Connect("test_keyspace");

            session.Execute("INSERT INTO test_table (id, col1, col2) VALUES (1, 'data1', 'data2')");

            Console.WriteLine($"Finished");
            Console.ReadKey();
        }
    }
}

如果代码在一台机器上而 cassandra 在另一台机器上(不同的 IP 地址),假设需要用户名和密码?所以我试过:
        var cluster = Cluster.Builder()
        .AddContactPoint("192.168.0.18") <- the ip address for the cassandra node
        .WithPort(9042)
        .WithCredentials("username to log into the cassandra node","password to log into the cassandra node")
        .Build();

我收到以下错误消息:
userone@desktop:~/Desktop/vsc$ dotnet run
Unhandled exception. Cassandra.NoHostAvailableException: All hosts tried for query failed (tried 192.168.0.18:9042: SocketException 'Connection refused')
   at Cassandra.Connections.ControlConnection.Connect(Boolean isInitializing)
   at Cassandra.Connections.ControlConnection.InitAsync()
   at Cassandra.Tasks.TaskHelper.WaitToCompleteAsync(Task task, Int32 timeout)
   at Cassandra.Cluster.Cassandra.SessionManagement.IInternalCluster.OnInitializeAsync()
   at Cassandra.ClusterLifecycleManager.InitializeAsync()
   at Cassandra.Cluster.Cassandra.SessionManagement.IInternalCluster.ConnectAsync[TSession](ISessionFactory`1 sessionFactory, String keyspace)
   at Cassandra.Cluster.ConnectAsync(String keyspace)
   at Cassandra.Tasks.TaskHelper.WaitToComplete(Task task, Int32 timeout)
   at Cassandra.Tasks.TaskHelper.WaitToComplete[T](Task`1 task, Int32 timeout)
   at Cassandra.Cluster.Connect(String keyspace)
   at HelloWorld.Program.Main(String[] args) in /home/userone/Desktop/vsc/Program.cs:line 17
userone@desktop:~/Desktop/vsc$ 

节点(cassandra 服务器)上的 iptables 目前设置如下:
node1@node1:~$ sudo iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -i lo -j ACCEPT
-A IMPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -s 192.168.0.73/32 -p tcp -m multiport --dports 7000,7001,7199,9042,9160,9142 -m state --state NEW,ESTABLISHED -j ACCEPT
node1@node1:~$

注意1:安装了应用程序的机器和安装了cassandra的机器都可以双向ping和traceroute。

注2:我已经测试了用户名和密码,直接在服务器上尝试可以登录cassandra服务器,没有任何问题。

注 3:Cassandra 在我今天刚刚创建的 VM 中运行。 VM 是运行代码的主机上的 guest 机器。

注 4:Host OS 和 Guest OS 都是 Linux。

最佳答案

.AddContactPoint("127.0.0.1")

如果这在同一台机器上有效,那么您可能已将 Cassandra 绑定(bind)到该 IP。如果您需要远程连接到您的节点,则需要将可路由 IP 绑定(bind)到该节点。

运行 nodetool status .如果您看到集群状态显示您的节点的 IP 为 127.0.0.1,那么从本地计算机连接到本地计算机是唯一可行的方案。

尝试在您的节点上运行以下命令:
grep _address cassandra.yaml

输出中返回的 IP 地址是唯一允许应用程序连接的 IP 地址。如果您希望能够连接到 192.168.0.18,那么 listenrpc地址应如下所示:
listen_address: 192.168.0.18
rpc_address: 192.168.0.18

请注意,您需要更改您的 seeds也列出来。

此外,如果您使用的虚拟机/提供程序同时具有内部和外部 IP 地址,那么您还需要设置 broadcast_外部 IP 地址:
broadcast_address: 10.6.5.5
broadcast_rpc_address: 10.6.5.5
listen_address: 192.168.0.18
rpc_address: 192.168.0.18

但尝试设置 listenrpc先到 192.168.0.18。

编辑 20191022

Just wanted to double check, do I add 192.168.0.18 as the listen_address and rpc_address to the cassandra node where the cassandra node has the ip address 192.168.0.18?



是的。还要确保您的节点的种子列表设置如下:
- seeds: "192.168.0.18"

Before I did that, the value of the listen_address and rpc_address were set to localhost



我是这么想的。

However, after making the changes you suggested, nodetool status now gives me


Failed to connect to 127.0.0.1:7199 - connection refused

具有讽刺意味的是,这与当 Cassandra 未运行时 nodetool 返回的消息相同。此时我会检查系统日志,看看它是否返回可能阻止它启动的错误。我怀疑种子列表仍然显示“127.0.0.1”。

tl;博士;

如果您打算远程连接到集群/节点,则不能使用将 Cassandra 绑定(bind)到主 IP (127.0.0.1/localhost) 的默认配置。这包括所有_address设置,以及您的 seeds列表。

关于c# - 如何连接到 Cassandra 虚拟机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58466825/

相关文章:

c# - 如何在 EF Core 2 中为 .ThenInclude 编写 Repository 方法

c# - 如何将程序集信息添加到由 dot net core 2.0 生成的 PrecompiledViews dll

c# - GraphQL 模型在 System.Text.Json.JsonException 中挣扎

c# - C# 中的 TraceRoute 和 Ping

c# - ImmutableArray<T>.As<TOther> 和 ImmutableArray<T>.CastArray<TOther> 方法之间有什么区别?

c# - 在属性上触发事件/函数? (C#)

c# - 在每个不同的线程上调用服务并等待所有服务响应

c# - 在 C# 中按字符反转字符串

c# - 将 VB 翻译成 C#

c# - 我将如何做一些简单的文件加密和解密?