首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Golang gocql无法连接Cassandra (使用Docker)

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

Stack Overflow用户
提问于 2018-09-10 22:11:24
回答 1查看 1.9K关注 0票数 3

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

我能找到的最接近解决golang gocql包和Cassandra之间的连接问题的信息可以在这里找到:Cassandra cqlsh - connection refused,但是有许多不同的向上投票答案,没有明确的指示。这也是一个受保护的问题(不包括"me toos"),因此许多社区成员似乎对此有困难。

这个问题应该略有不同,因为它使用的Docker和我已经尝试了大部分(如果不是所有的解决方案链接到上面)。

代码语言:javascript
复制
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的哪些收听地址有点不稳定,所以我只是路过了'home':

代码语言:javascript
复制
  - 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,则会得到以下错误:

代码语言:javascript
复制
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正常启动并监听适当的端口:

代码语言:javascript
复制
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代码中,我有以下正在被调用的包(简化为显示相关部分):

代码语言:javascript
复制
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
    <...>
}

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

代码语言:javascript
复制
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")

例如,这些似乎很有可能成为候选人,但却没有运气。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-10 22:45:24

使用服务名称cassandra00作为停靠者-组合文档https://docs.docker.com/compose/compose-file/#links的主机名。

可在与别名相同的主机名上访问链接服务的容器,如果未指定别名,则可访问服务名称。

保留CASSANDRA_LISTEN_ADDRESS envvar未设置(或传递auto)给每个https://docs.docker.com/samples/library/cassandra/

默认值是auto,它将在容器启动时将listen_address选项在cassandra.yaml中设置为容器的IP地址。此默认设置在大多数用例中都能工作。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52266171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档