dousi2553 2018-09-10 22:11 采纳率: 0%
浏览 216
已采纳

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"  
services:  
  cassandra00:
    restart: always
    image: cassandra:latest
    volumes: 
      - ./db/casdata:/var/lib/cassandra
    ports: 
      - 7000:7000
      - 7001:7001
      - 7199:7199
      - 9042:9042
      - 9160:9160
    environment:
      - CASSANDRA_RPC_ADDRESS=127.0.0.1
      - CASSANDRA_BROADCAST_ADDRESS=127.0.0.1
      - CASSANDRA_LISTEN_ADDRESS=127.0.0.1
      - CASSANDRA_START_RPC=true
  db:
    restart: always
    build: ./db
    environment:
      POSTGRES_USER: patientplatypus
      POSTGRES_PASSWORD: SUPERSECRETFAKEPASSD00T
      POSTGRES_DB: zennify
    expose:
      - "5432"
    ports:
      - 5432:5432
    volumes:
      - ./db/pgdata:/var/lib/postgresql/data
  app:
    restart: always
    build: 
      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'
    ports:
      - 8000:8000
    depends_on:
      - db
      - cassandra00
    links:
      - db
      - cassandra00
    volumes:
      - ./:/go/src/github.com/patientplatypus/webserver/

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

  - CASSANDRA_RPC_ADDRESS=127.0.0.1
  - CASSANDRA_BROADCAST_ADDRESS=127.0.0.1
  - CASSANDRA_LISTEN_ADDRESS=127.0.0.1

If you try and pass 0.0.0.0 you get the following error:

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

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 StorageService.java:1446 - JOINING: Finish joining ring
cassandra00_1  | INFO  [main] 2018-09-10 22:06:29,179 StorageService.java:2289 - Node /127.0.0.1 state jump to NORMAL
cassandra00_1  | INFO  [main] 2018-09-10 22:06:29,607 NativeTransportService.java:70 - Netty using native Epoll event loop
cassandra00_1  | INFO  [main] 2018-09-10 22:06:29,750 Server.java:155 - 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 Server.java:156 - Starting listening for CQL clients on /127.0.0.1:9042 (unencrypted)...
cassandra00_1  | INFO  [main] 2018-09-10 22:06:29,990 ThriftServer.java:116 - Binding thrift service to /127.0.0.1:9160

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

package data

import(
    "fmt"
    "github.com/gocql/gocql"
)

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 127.0.0.1: 
dial tcp 127.0.0.1:9042: 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("127.0.0.1")

cluster := gocql.NewCluster("127.0.0.1:9042")

cluster := gocql.NewCluster("127.0.0.1:9160")

These seemed likely candidates for example, but no luck.

Does anyone have any idea what I am doing wrong?

  • 写回答

1条回答 默认 最新

  • douxuanou2787 2018-09-10 22:45
    关注

    Use the service name cassandra00 for the hostname per the docker-compose documentation https://docs.docker.com/compose/compose-file/#links

    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 https://docs.docker.com/samples/library/cassandra/

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?