2018-09-10 22:11 阅读 173

Golang gocql无法连接到Cassandra(使用Docker)

I am trying to setup and connect to a Cassandra single node instance using docker and Golang and it is not working.

The closest information I could find to addressing connection issues between the golang gocql package and Cassandra is available here: Cassandra cqlsh - connection refused, however there are many different upvote answers with no clear indication of which is preferred. It is also a protected question (no "me toos"), so a lot of community members seem to be having trouble with this.

This problem should be slightly different, as it is using Docker and I have tried most (if not all of the solutions linked to above).

version: "3"  
    restart: always
    image: cassandra:latest
      - ./db/casdata:/var/lib/cassandra
      - 7000:7000
      - 7001:7001
      - 7199:7199
      - 9042:9042
      - 9160:9160
    restart: always
    build: ./db
      POSTGRES_USER: patientplatypus
      POSTGRES_DB: zennify
      - "5432"
      - 5432:5432
      - ./db/pgdata:/var/lib/postgresql/data
    restart: always
      context: .
      dockerfile: Dockerfile
    command: bash -c 'while !</dev/tcp/db/5432; do sleep 10; done; realize start --run'
    # command: bash -c 'while !</dev/tcp/db/5432; do sleep 10; done; go run main.go'
      - 8000:8000
      - db
      - cassandra00
      - db
      - cassandra00
      - ./:/go/src/

Admittedly, I am a little shaky on what listening addresses I should pass to Cassandra in the environment section, so I just passed 'home':


If you try and pass you get the following error:

cassandra00_1  | Exception (org.apache.cassandra.exceptions.ConfigurationException) encountered during startup: listen_address cannot be a wildcard address (!
cassandra00_1  | listen_address cannot be a wildcard address (!
cassandra00_1  | ERROR [main] 2018-09-10 21:50:44,530 - Exception encountered during startup: listen_address cannot be a wildcard address (!

Overall, however I think that I am getting the correct start up procedure for Cassandra (afaict) because my terminal outputs that Cassandra started up as normal and is listening on the appropriate ports:

cassandra00_1  | INFO  [main] 2018-09-10 22:06:28,920 - JOINING: Finish joining ring
cassandra00_1  | INFO  [main] 2018-09-10 22:06:29,179 - Node / state jump to NORMAL
cassandra00_1  | INFO  [main] 2018-09-10 22:06:29,607 - Netty using native Epoll event loop
cassandra00_1  | INFO  [main] 2018-09-10 22:06:29,750 - Using Netty Version: [netty-buffer=netty-buffer-4.0.44.Final.452812a, netty-codec=netty-codec-4.0.44.Final.452812a, netty-codec-haproxy=netty-codec-haproxy-4.0.44.Final.452812a, netty-codec-http=netty-codec-http-4.0.44.Final.452812a, netty-codec-socks=netty-codec-socks-4.0.44.Final.452812a, netty-common=netty-common-4.0.44.Final.452812a, netty-handler=netty-handler-4.0.44.Final.452812a, netty-tcnative=netty-tcnative-1.1.33.Fork26.142ecbb, netty-transport=netty-transport-4.0.44.Final.452812a, netty-transport-native-epoll=netty-transport-native-epoll-4.0.44.Final.452812a, netty-transport-rxtx=netty-transport-rxtx-4.0.44.Final.452812a, netty-transport-sctp=netty-transport-sctp-4.0.44.Final.452812a, netty-transport-udt=netty-transport-udt-4.0.44.Final.452812a]
cassandra00_1  | INFO  [main] 2018-09-10 22:06:29,754 - Starting listening for CQL clients on / (unencrypted)...
cassandra00_1  | INFO  [main] 2018-09-10 22:06:29,990 - Binding thrift service to /

In my golang code I have the following package that is being called (simplified to show relevant section):

package data


func create_userinfo_table() {
    fmt.Println("replicating table in cassandra")
    cluster := gocql.NewCluster("localhost") //<---error here!
    cluster.ProtoVersion = 4

Which results in the following error in my terminal:

app_1          | [21:52:38][WEBSERVER] : 2018/09/10 
21:52:38 gocql: unable to dial control conn 
dial tcp connect: connection refused

app_1          | [21:52:38][WEBSERVER] : 2018/09/10 
21:52:38 gocql: unable to dial control conn ::1: 
dial tcp [::1]:9042: connect: cannot assign requested address

app_1          | [21:52:38][WEBSERVER] : 2018/09/10 
21:52:38 Could not connect to cassandra cluster: gocql: 
unable to create session: control: unable to connect to initial hosts: 
dial tcp [::1]:9042: connect: cannot assign requested address

I have tried several variations on the connection address

cluster := gocql.NewCluster("localhost")

cluster := gocql.NewCluster("")

cluster := gocql.NewCluster("")

cluster := gocql.NewCluster("")

These seemed likely candidates for example, but no luck.

Does anyone have any idea what I am doing wrong?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douxuanou2787 douxuanou2787 2018-09-10 22:45

    Use the service name cassandra00 for the hostname per the docker-compose documentation

    Containers for the linked service are reachable at a hostname identical to the alias, or the service name if no alias was specified.

    Leave the CASSANDRA_LISTEN_ADDRESS envvar unset (or pass auto) per

    The default value is auto, which will set the listen_address option in cassandra.yaml to the IP address of the container as it starts. This default should work in most use cases.

    点赞 评论 复制链接分享