How do I connect to a Cassandra cluster deployed on AWS with gocql?

huangapple go评论81阅读模式
英文:

How do I connect to a Cassandra cluster deployed on AWS with gocql?

问题

我已经设置了一个三节点的Cassandra集群。这个集群在AWS上,服务器端口是开放的。这三个服务器之间连接良好,工作正常。

我在我的cassandra.yaml文件中设置了

authenticator: AllowAllAuthenticator

我想用go cql建立连接,以下是连接代码:

cluster := gocql.NewCluster("x.xxx.xxx.xxx")
cluster.Keyspace = "keyspace_name"
cluster.Consistency = gocql.Quorum
cluster.ProtoVersion = 4
cluster.ConnectTimeout = time.Second * 10

session, err := cluster.CreateSession()
if err != nil {
    fmt.Println(err)
}

但是go cql返回了以下消息。

2020/09/26 09:53:44 gocql: unable to dial control conn x.xxx.xxx: dial tcp 127.0.0.1:9042: connectex: No connections could be made because the target machine actively refused them.
2020/09/26 09:53:44 gocql: unable to create session: control: unable to connect to initial hosts: dial tcp x.xxx.xxx.xxx:9042: connectex: No connections could be made because the target machine actively refused them.
panic: runtime error: invalid memory address or nil pointer dereference
英文:

I have done a setup of a three node Cassandra cluster. This one is on aws, and the server port are open. The three servers are well connected to each other and work perfectly.
I have set

authenticator: AllowAllAuthenticator

in my cassandra.yaml file

I would like to make the connection with go cql, here is the connection code

cluster := gocql.NewCluster("x.xxx.xxx.xxx")
cluster.Keyspace = "keyspace_name"
cluster.Consistency = gocql.Quorum
cluster.ProtoVersion = 4
cluster.ConnectTimeout = time.Second * 10

session, err := cluster.CreateSession()
if err != nil {
	fmt.Println(err)
}

but go cql sends me back this message.

2020/09/26 09:53:44 gocql: unable to dial control conn x.xxx.xxx: dial tcp 127.0.0.1:9042: connectex: No connections could be made because the target machine actively refused them.
2020/09/26 09:53:44 gocql: unable to create session: control: unable to connect to initial hosts: dial tcp x.xxx.xxx.xxx:9042: connectex: No connections could be made because the target machine actively refused them.
panic: runtime error: invalid memory address or nil pointer dereference

答案1

得分: 1

你的应用程序中很可能使用了私有IP地址来连接节点,但是按照定义,私有地址是无法从远程访问的。

你需要确保已经配置了节点使用以下设置:

listen_address: <ec2_private_ip>
rpc_address: <ec2_public_ip>

节点之间使用listen_address进行通信,而客户端使用rpc_address连接到集群,因此需要配置为EC2实例的公共可访问IP地址。

一旦正确配置了节点,就可以在应用程序中使用公共IP地址作为联系点进行配置:

cluster := gocql.NewCluster("public_ip1,public_ip2,...")

这样应该可以使你的应用程序连接到集群。祝好运!

英文:

There's a good chance that you're using the private IP address in your app to connect to the nodes but those are not accessible remotely because by definition, they are private addresses.

You need to make sure that you've configured your nodes to use:

listen_address: &lt;ec2_private_ip&gt;
rpc_address: &lt;ec2_public_ip&gt;

Nodes communicate with each other using the listen_address whereas clients connect to the cluster using the rpc_address which is why it needs to be configured with the publicly-accessible IP addresses of the EC2 instances.

Once you've got the nodes configured correctly, configure your app to use the public IPs as contact points in:

cluster := gocql.NewCluster(&quot;public_ip1,public_ip2,...&quot;)

This should allow your app to connect to the cluster. Cheers!

huangapple
  • 本文由 发表于 2021年10月1日 02:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/69397524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定