登录
首页 >  Golang >  Go问答

Golang gocql 无法通过 Docker 连接到 Cassandra

来源:stackoverflow

时间:2024-03-21 21:54:33 160浏览 收藏

在使用 Docker 和 Go 语言库 gocql 时,无法连接到 Cassandra 单节点实例。尽管 Cassandra 已启动并侦听适当的端口,但 gocql 无法建立连接,并显示“无法拨打控制连接”的错误。已尝试了多种连接地址变体,包括“localhost”、“127.0.0.1”和端口号,但均未成功。根据 Docker Compose 文档,使用服务名称作为主机名,并且 CASSANDRA_LISTEN_ADDRESS 环境变量未设置,这应该符合大多数用例。

问题内容

我正在尝试使用 docker 和 golang 设置并连接到 cassandra 单节点实例,但它不起作用。

我能找到的解决 golang gocql 包和 cassandra 之间的连接问题的最接近的信息可以在这里找到:cassandra cqlsh - 连接被拒绝,但是有许多不同的赞成答案,没有明确表明哪个是首选。这也是一个受保护的问题(没有“我也是”),因此很多社区成员似乎都遇到了麻烦。

这个问题应该略有不同,因为它使用 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 !

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

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

例如,这些看起来可能是候选者,但运气不佳。

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


解决方案


根据 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地址。此默认值应该适用于大多数用例。

终于介绍完啦!小伙伴们,这篇关于《Golang gocql 无法通过 Docker 连接到 Cassandra》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>