0

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 enter image description here

enter image description here

And here the screen of my box open port enter image description here

Jesver
  • 83
  • 1
  • 6
  • 1
    Cassandra is running on a Compute Engine VM, yeah? Is the application running outside of GCP? If so, did you alter the firewall rules for the VM to open up the ports Cassandra needs? – Gabe Weiss Sep 15 '21 at 16:38
  • yes cassandra is on a different machine than go, but how to know which port cassandra needs ? @GabeWeiss – Jesver Sep 15 '21 at 17:23
  • 1
    https://cassandra.apache.org/doc/latest/cassandra/faq/#:~:text=more%20gory%20details.-,What%20ports%20does%20Cassandra%20use%3F,configurable%20in%20the%20cassandra%2Dyaml%20. Unless the configuration was customized. – Gabe Weiss Sep 15 '21 at 17:43

1 Answers1

1

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.

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
  • @EricRamirez Thanks for the tip, there is someone who made a stack with the same configuration as me here is the stack link: https://stackoverflow.com/questions/69210333/how-to-connect-go-api-to-cassandra-db – Jesver Sep 17 '21 at 08:56
  • Could you please update your original post with details of the error + stacktrace? It will give me a better idea of what the problem is. Cheers! – Erick Ramirez Sep 17 '21 at 12:29
  • @EricRamirez I've just updated the stack with all the missing information – Jesver Sep 17 '21 at 16:24
  • From the machine where you have your Go app running, can you `telnet` to the C* node on port `9042`? – Erick Ramirez Sep 18 '21 at 02:07
  • @EricRamirez I just tested the telnet connection, but it doesn't work, here is the error returned, - telnet xx.xxx.228.175 9042 - Connexion à xx.xxx.228.175...Impossible d’ouvrir une connexion à l’hôte, sur le port 9042: Échec lors de la connexion – Jesver Sep 18 '21 at 11:07
  • 1
    That tells me that there is no connectivity between the 2 machines so you need to create an access rule for the GCP compute instances. It isn't a C* issue. Cheers! – Erick Ramirez Sep 18 '21 at 12:34
  • @EricRamirez I've done a lot of testing by creating firewall rules, but every time it's the same result – Jesver Sep 18 '21 at 14:45
  • google cloud se desconecta pero devuelve un error – Jesver Sep 18 '21 at 15:20