如何在Google Cloud上连接到Cassandra?

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

How to connect to Cassandra on Google Cloud?

问题

我目前正在一个使用Go API的项目上工作。它与部署在Google Cloud服务器上的Cassandra进行通信。

问题是,当我尝试向Cassandra发出请求时,Go返回了多个错误。经过多次测试,我认为问题出在Go和Cassandra之间的连接上,连接无法正常工作。

在连接时是否有安全检查?还是我在代码中犯了错误?

我使用gocql来连接Cassandra。以下是Go连接脚本和调用Cassandra的请求。

    type DBConnection struct {
    	cluster *gocql.ClusterConfig
    	session *gocql.Session
    }
    
    var connection DBConnection
    
    func SetupDBConnection() {
    	connection.cluster = gocql.NewCluster(google cloud server address)
    	connection.cluster.Consistency = gocql.Quorum
    	connection.cluster.Keyspace = "keyspace name"
    	connection.session, _ = connection.cluster.CreateSession()
    }
    
    func ExecuteQuery(query string, values ...interface{}) {
    	connection.session.Query(query).Bind(values...).Exec()
    }

Go CQL返回以下错误:

> gocql: impossible de créer la session: contrôle: impossible de se
> connecter aux hôtes initiaux: composez tcp xx.xxx.xxx.xxx:9042:
> délai d'attente d'e/s

这是我的Google Cloud配置截图:

如何在Google Cloud上连接到Cassandra?

如何在Google Cloud上连接到Cassandra?

这是我打开端口的截图:

如何在Google Cloud上连接到Cassandra?

英文:

I am currently working on a project with a Go api. It communicates with Cassandra which is on a Google Cloud server.

The problem is that when I try to make a request to Cassandra, Go returns me several errors. After many tests I think that the problem comes from the connection between Go and Cassandra which does not work.

Is there a security check to make the connection? Or did I just make a mistake in my code?

I use gocql to connect to Cassandra. Here is the Go connection script and the request to make call to Cassandra.

    type DBConnection struct {
    	cluster *gocql.ClusterConfig
    	session *gocql.Session
    }
    
    var connection DBConnection
    
    func SetupDBConnection() {
    	connection.cluster = gocql.NewCluster(google cloud server adress)
    	connection.cluster.Consistency = gocql.Quorum
    	connection.cluster.Keyspace = "keyspace name"
    	connection.session, _ = connection.cluster.CreateSession()
    }
    
    func ExecuteQuery(query string, values ...interface{}) {
    	connection.session.Query(query).Bind(values...).Exec()
    }

go cql retrun this error :

> gocql : impossible de créer la session : contrôle : impossible de se
> connecter aux hôtes initiaux : composez tcp xx.xxx.xxx.xxx:9042 :
> délai d'attente d'e/s

Here are the screens of my google cloud configuration
如何在Google Cloud上连接到Cassandra?

如何在Google Cloud上连接到Cassandra?

And here the screen of my box open port
如何在Google Cloud上连接到Cassandra?

答案1

得分: 1

不知道你遇到了什么错误,所以很难给出准确的答案。

如果你的Go应用程序运行在与Cassandra节点不在同一子网的机器上,你需要配置Cassandra以使用计算实例的公共IP地址,以便应用程序可以访问它们。以下是cassandra.yaml中相关的属性:

listen_address: private_ip
rpc_address: public_ip

客户端/应用程序实例使用rpc_address在端口rpc_port(默认为9042)上连接到Cassandra节点,因此它需要是一个公共可访问的IP地址。

你还需要确保GCP访问规则允许从运行应用程序的机器向计算实例发送流量。

英文:

It's difficult to answer without knowing what error you are getting.

If your Go app is running on a machine that is not on the same subnet as the Cassandra nodes, you need to configure Cassandra to with public IPs of the compute instances so they can be accessed by your app. The are the relevant properties in cassandra.yaml:

listen_address: private_ip
rpc_address: public_ip

Clients/app instances connect to the Cassandra nodes using the rpc_address on port rpc_port (default is 9042) so it needs to be a publicly accessible IP address.

You'll also need to make sure that the GCP access rules allow traffic to the compute instances from the machine where your app is running.

huangapple
  • 本文由 发表于 2021年9月15日 23:27:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/69195930.html
匿名

发表评论

匿名网友

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

确定