cassandra容器数据库不可用异常

uurv41yg  于 2021-06-09  发布在  Cassandra
关注(0)|答案(1)|浏览(619)

我使用cassandra docker映像,在docker容器上运行的spring引导服务,并使用datastax java驱动程序。
我可以连接Cassandra,也可以看到日志,下面是docker compose文件,

version: '3.5'

networks:
  my-network:

 cassandra-db:
    container_name: cassandra-db
    image: cassandra:4.0-beta1
    ports:
      - "9042:9042"
    restart: on-failure
    environment:
      - CASSANDRA_CLUSTER_NAME='cassandra-cluster'
      - CASSANDRA_NUM_TOKENS=256
      - CASSANDRA_RPC_ADDRESS=0.0.0.0
      - CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch
    volumes:
      - ./out/cassandra_data:/var/lib/cassandra
    networks:
      - my-network

 client-service:
    container_name: client-service
    image: data/client-service
    environment:
      - SPRING_PROFILES_ACTIVE=dev
    ports:
      - 8087:8087
    depends_on:
      - cassandra-db
    links:
      - cassandra-db
    networks:
      - my-network

我的键空间查询是,

CREATE KEYSPACE IF NOT EXISTS SensorKeySpace WITH replication = {'class':'NetworkTopologyStrategy','replication_factor':3}

和建筑物 Cqlsession 使用,

.addContactEndPoint(new DefaultEndPoint(
                        InetSocketAddress.createUnresolved("cassandra-db",9042)))
                .withLocalDatacenter("DC1")
                .withKeyspace(CassandraConstant.KEY_SPACE_NAME.getValue())
                .build();

Cassandra已经启动并运行了,而且已经验证了,

CONTAINER ID        IMAGE         COMMAND                  CREATED             STATUS              PORTS                                  NAMES
57d1d29dbf00  client-service      "java -Djava.securit…"   4 minutes ago       Up 4 minutes   0.0.0.0:8087->8087/tcp                      client-service
00548a4058e6  cassandra:4.0-beta1  "docker-entrypoint.s…"   4 minutes ago  Up 4 minutes   7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9042->9042/tcp   cassandra-db

问题出在一开始,
我曾经 SimpleStrategy 创建keyspace时出现以下错误,

Caused by: [com.datastax.oss.driver.api.core.CqlSession]: Factory method 'session' threw exception; nested exception is java.lang.IllegalStateException: Since you provided explicit contact points, the local DC must be explicitly set (see basic.load-balancing-policy.local-datacenter in the config, or set it programmatically with SessionBuilder.withLocalDatacenter). Current contact points are: Node(endPoint=cassandra-db:9042, hostId=729bd04f-b345-4df3-a148-80545d30e032, hashCode=5563e4c4)=datacenter1. Current DCs in this cluster are: datacenter1

然后配置 .withLocalDatacenter("DC1") 以及 - CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch 在docker compose中。
在配置之后,在启动容器的过程中没有错误,但是当我点击查询时,我得到了下面的错误,

The Select Query is [SELECT NAME,VALUE FROM READING ]
client-service       | 2020-09-08 11:12:38.480 ERROR 1 --- [nio-8087-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.util.concurrent.ExecutionException: org.springframework.data.cassandra.CassandraConnectionFailureException: Query; CQL [com.datastax.oss.driver.internal.core.cql.DefaultSimpleStatement@1265401a]; No node was available to execute the query; nested exception is com.datastax.oss.driver.api.core.NoNodeAvailableException: No node was available to execute the query] with root cause
client-service       | com.datastax.oss.driver.api.core.NoNodeAvailableException: No node was available to execute the query

我看过api文件,https://docs.datastax.com/en/drivers/java/4.0/com/datastax/oss/driver/api/core/nonodeavailableexception.html 这告诉我

This can happen if all nodes are down, or if all the contact points provided at startup were invalid.

我的Cassandra数据库已启动(验证),连接成功。这里少了什么?

dluptydi

dluptydi1#

所以这里的问题是键空间的定义。

CREATE KEYSPACE IF NOT EXISTS SensorKeySpace WITH replication = 
    {'class':'NetworkTopologyStrategy','replication_factor':3}

这个 NetworkTopologyStrategy 有数据中心意识。这意味着需要为每个数据中心设置复制。据我所见,您的代码似乎在寻找“dc1”数据中心。

.withLocalDatacenter("DC1")

但是,你的日志告诉你:
此群集中当前的DC是:datacenter1
这意味着您的键空间定义应该如下所示:

CREATE KEYSPACE IF NOT EXISTS SensorKeySpace WITH replication = 
    {'class':'NetworkTopologyStrategy','datacenter1':3}

你的代码应该是:

.withLocalDatacenter("datacenter1")

注意:数据中心名称区分大小写。请确保验证您在中使用的数据中心名称 cassandra-rackdc.properties 文件。

相关问题