【问题标题】:Golang gocql cannot connect to Cassandra (using Docker)Golang gocql 无法连接到 Cassandra(使用 Docker)
【发布时间】:2019-02-15 09:11:20
【问题描述】:

我正在尝试使用 docker 和 Golang 设置并连接到 Cassandra 单节点实例,但它无法正常工作。

我可以找到解决 golang gocql 包和 Cassandra 之间的连接问题的最接近的信息:Cassandra cqlsh - connection refused,但是有许多不同的投票答案,没有明确指出哪个是首选。这也是一个受保护的问题(不是“我也是”),所以很多社区成员似乎都遇到了这个问题。

这个问题应该略有不同,因为它使用的是 Docker,而且我尝试过最多(如果不是上面链接的所有解决方案)。

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/

诚然,我对应该在环境部分传递给 Cassandra 的监听地址有点犹豫,所以我只传递了“家”:

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

如果您尝试传递0.0.0.0,则会收到以下错误:

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)!

总的来说,但是我认为我正在为 Cassandra(afaict)获得正确的启动过程,因为我的终端输出 Cassandra 正常启动并且正在侦听适当的端口:

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

在我的 golang 代码中,我有以下被调用的包(简化以显示相关部分):

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
    <...>
}

这导致我的终端出现以下错误:

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

我尝试了几种连接地址的变体

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")

例如,这些似乎是可能的候选人,但没有运气。

有人知道我做错了什么吗?

【问题讨论】:

    标签: database docker go cassandra docker-compose


    【解决方案1】:

    根据 docker-compose 文档 https://docs.docker.com/compose/compose-file/#links,使用服务名称 cassandra00 作为主机名

    可以通过与别名相同的主机名访问链接服务的容器,如果未指定别名,则使用服务名。

    根据https://docs.docker.com/samples/library/cassandra/ 保留CASSANDRA_LISTEN_ADDRESS envvar 未设置(或通过auto

    默认值是auto,它会将cassandra.yaml中的listen_address选项设置为容器启动时的IP地址。此默认值应该适用于大多数用例。

    【讨论】:

    • 谢谢!我认为这可能是 - 确认其他一些事情以获得新的构建,然后给你互联网积分:P。一秒。
    • 我正在使用 docker run 命令并遇到上述问题。你能顺利地跑吗?这是我收到的错误gocql: unable to create session: unable to discover protocol version: dial tcp 127.0.0.1:9042
    猜你喜欢
    • 2022-07-25
    • 2016-04-11
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 2020-05-31
    • 2017-06-06
    相关资源
    最近更新 更多