eternal
eternal

Reputation: 419

Getting error connecting to 4-node Cassandra cluster running in Docker from Spring Boot app

I am trying to set up docker-compose with 4 Cassandra nodes. Here is my docker-compose configuration:

    version: "3.8"

services:
  cass1:
    image: cassandra:4
    container_name: cass1
    hostname: cass1
    networks:
      cass_netwrok:
        ipv4_address: 172.30.0.2
    expose:
      - 9042
    ports:
      - "9042:9042"
    environment:
      - CASSANDRA_CLUSTER_NAME=default
      - CASSANDRA_SEEDS=cass1
  cass2:
    image: cassandra:4
    container_name: cass2
    hostname: cass2
    networks:
      cass_netwrok:
        ipv4_address: 172.30.0.3
    expose:
      - 9042
    ports:
      - "9043:9042"
    environment:
      - CASSANDRA_CLUSTER_NAME=default
      - CASSANDRA_SEEDS=cass1
    depends_on:
      - cass1
  cass3:
    image: cassandra:4
    container_name: cass3
    hostname: cass3
    networks:
      cass_netwrok:
        ipv4_address: 172.30.0.4
    expose:
      - 9042
    ports:
      - "9044:9042"
    environment:
      - CASSANDRA_CLUSTER_NAME=default
      - CASSANDRA_SEEDS=cass1
    depends_on:
      - cass1
  cass4:
    expose:
      - 9042
    image: cassandra:4
    container_name: cass4
    hostname: cass4
    networks:
      cass_netwrok:
        ipv4_address: 172.30.0.5
    ports:
      - "9045:9042"
    environment:
      - CASSANDRA_CLUSTER_NAME=default
      - CASSANDRA_SEEDS=cass1
    depends_on:
      - cass1

networks:
  cass_netwrok:
    ipam:
      driver: default
      config:
        - subnet: 172.30.0.0/16

Looks like this is working, because I can connect to 1 node by localhost:9042 and create a keyspace plus table and observe the same table in the second node localhost:9043.

But when I am trying to set up the spring boot application and connect to Cassandra, I observe errors in logs:

2022-08-30 14:45:47.252  INFO 135480 --- [           main] c.d.o.d.i.core.DefaultMavenCoordinates   : DataStax Java driver for Apache Cassandra(R) (com.datastax.oss:java-driver-core) version 4.14.1
2022-08-30 14:45:47.692  INFO 135480 --- [     s0-admin-0] c.d.oss.driver.internal.core.time.Clock  : Using native clock for microsecond precision
2022-08-30 14:45:48.191  WARN 135480 --- [        s0-io-2] c.d.o.d.i.c.m.DefaultTopologyMonitor     : [s0] Found invalid row in system.peers_v2 for peer: /172.30.0.3. This is likely a gossip or snitch issue, this node will be ignored.
2022-08-30 14:45:48.214  WARN 135480 --- [        s0-io-2] c.d.o.d.i.c.m.SchemaAgreementChecker     : [s0] Found invalid system.peers row for peer: /172.30.0.3, excluding from schema agreement check.
2022-08-30 14:45:53.315  WARN 135480 --- [     s0-admin-0] c.d.o.d.internal.core.pool.ChannelPool   : [s0|/172.30.0.5:9042]  Error while opening new channel (ConnectionInitException: [s0|connecting...] Protocol initialization request, step 1 (STARTUP {CQL_VERSION=3.0.0, DRIVER_NAME=DataStax Java driver for Apache Cassandra(R), DRIVER_VERSION=4.14.1, CLIENT_ID=6bf33612-5f5a-497c-a475-ef1d522d6c2d}): failed to send request (java.nio.channels.NotYetConnectedException))
2022-08-30 14:45:53.315  WARN 135480 --- [     s0-admin-1] c.d.o.d.internal.core.pool.ChannelPool   : [s0|/172.30.0.4:9042]  Error while opening new channel (ConnectionInitException: [s0|connecting...] Protocol initialization request, step 1 (STARTUP {CQL_VERSION=3.0.0, DRIVER_NAME=DataStax Java driver for Apache Cassandra(R), DRIVER_VERSION=4.14.1, CLIENT_ID=6bf33612-5f5a-497c-a475-ef1d522d6c2d}): failed to send request (java.nio.channels.NotYetConnectedException))
2022-08-30 14:45:53.346  INFO 135480 --- [     s1-admin-0] c.d.oss.driver.internal.core.time.Clock  : Using native clock for microsecond precision
2022-08-30 14:45:53.387  WARN 135480 --- [        s1-io-2] c.d.o.d.i.c.m.DefaultTopologyMonitor     : [s1] Found invalid row in system.peers_v2 for peer: /172.30.0.3. This is likely a gossip or snitch issue, this node will be ignored.
2022-08-30 14:45:53.394  WARN 135480 --- [        s1-io-2] c.d.o.d.i.c.m.SchemaAgreementChecker     : [s1] Found invalid system.peers row for peer: /172.30.0.3, excluding from schema agreement check.
2022-08-30 14:45:58.438  WARN 135480 --- [     s1-admin-1] c.d.o.d.internal.core.pool.ChannelPool   : [s1|/172.30.0.4:9042]  Error while opening new channel (ConnectionInitException: [s1|connecting...] Protocol initialization request, step 1 (STARTUP {CQL_VERSION=3.0.0, DRIVER_NAME=DataStax Java driver for Apache Cassandra(R), DRIVER_VERSION=4.14.1, CLIENT_ID=b9d24164-5b75-4a69-acbb-b162b20c0679}): failed to send request (java.nio.channels.NotYetConnectedException))
2022-08-30 14:45:58.446  WARN 135480 --- [     s1-admin-0] c.d.o.d.internal.core.pool.ChannelPool   : [s1|/172.30.0.5:9042]  Error while opening new channel (ConnectionInitException: [s1|connecting...] Protocol initialization request, step 1 (STARTUP {CQL_VERSION=3.0.0, DRIVER_NAME=DataStax Java driver for Apache Cassandra(R), DRIVER_VERSION=4.14.1, CLIENT_ID=b9d24164-5b75-4a69-acbb-b162b20c0679}): failed to send request (java.nio.channels.NotYetConnectedException))
2022-08-30 14:45:58.472  WARN 135480 --- [        s0-io-2] c.d.o.d.i.c.m.SchemaAgreementChecker     : [s0] Found invalid system.peers row for peer: /172.30.0.3, excluding from schema agreement check.
2022-08-30 14:45:58.474  WARN 135480 --- [        s1-io-2] c.d.o.d.i.c.m.SchemaAgreementChecker     : [s1] Found invalid system.peers row for peer: /172.30.0.3, excluding from schema agreement check.
2022-08-30 14:45:58.608  WARN 135480 --- [        s1-io-2] c.d.o.d.i.c.m.SchemaAgreementChecker     : [s1] Found invalid system.peers row for peer: /172.30.0.3, excluding from schema agreement check.
2022-08-30 14:45:59.305  INFO 135480 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-08-30 14:45:59.319  INFO 135480 --- [           main] c.e.nosqltask3.NoSqlTask3Application     : Started NoSqlTask3Application in 14.233 seconds (JVM running for 15.225)
2022-08-30 14:45:59.334  WARN 135480 --- [     s0-admin-0] c.d.o.d.internal.core.pool.ChannelPool   : [s0|/172.30.0.5:9042]  Error while opening new channel (ConnectionInitException: [s0|connecting...] Protocol initialization request, step 1 (STARTUP {CQL_VERSION=3.0.0, DRIVER_NAME=DataStax Java driver for Apache Cassandra(R), DRIVER_VERSION=4.14.1, CLIENT_ID=6bf33612-5f5a-497c-a475-ef1d522d6c2d}): failed to send request (java.nio.channels.NotYetConnectedException))
2022-08-30 14:45:59.429  WARN 135480 --- [     s0-admin-1] c.d.o.d.internal.core.pool.ChannelPool   : [s0|/172.30.0.4:9042]  Error while opening new channel (ConnectionInitException: [s0|connecting...] Protocol initialization request, step 1 (STARTUP {CQL_VERSION=3.0.0, DRIVER_NAME=DataStax Java driver for Apache Cassandra(R), DRIVER_VERSION=4.14.1, CLIENT_ID=6bf33612-5f5a-497c-a475-ef1d522d6c2d}): failed to send request (java.nio.channels.NotYetConnectedException))
2022-08-30 14:46:04.472  WARN 135480 --- [     s1-admin-0] c.d.o.d.internal.core.pool.ChannelPool   : [s1|/172.30.0.5:9042]  Error while opening new channel (ConnectionInitException: [s1|connecting...] Protocol initialization request, step 1 (STARTUP {CQL_VERSION=3.0.0, DRIVER_NAME=DataStax Java driver for Apache Cassandra(R), DRIVER_VERSION=4.14.1, CLIENT_ID=b9d24164-5b75-4a69-acbb-b162b20c0679}): failed to send request (java.nio.channels.NotYetConnectedException))

Especially I don't know how to fix Error while opening new channel. I guess that this is because spring boot trying to connect to the node by container IP and default Cassandra port 9042, but only the first node has the 9042 port exposed.

Here is my spring properties:

spring:
  data:
    cassandra:
      keyspace-name: events
      contact-points:
        - "localhost:9042"
        - "localhost:9043"
        - "localhost:9044"
        - "localhost:9045"

And Cassandra config class:

@Configuration
@EnableCassandraRepositories(basePackages = "com.eternal.nosqltask3.repository")
public class CassandraConfig extends AbstractCassandraConfiguration {

    @Value("${spring.data.cassandra.keyspace-name}")
    private String keyspace;
    @Override
    protected String getKeyspaceName() {
        return keyspace;
    }
}

Please suggest, what am I missing to set up multi-node Cassandra and spring boot without errors.

Upvotes: 2

Views: 1751

Answers (1)

Erick Ramirez
Erick Ramirez

Reputation: 16353

[EDITED] You need to publish both the port and a host IP address to expose the containers' services outside of the Docker network. For example:

$ docker run ... -p 10.1.2.3:10001:9042

This means that the container service running on port 9042 will be accessible on host IP 10.1.2.3:10001.

As a side note, if your goal is to simply learn how to build apps with Cassandra as a backend then you can use Astra DB. It is a Cassandra-as-a-service running on the cloud. You can sign up for a free account here with no credit card required and you can launch a free Cassandra cluster in just 5 clicks. Cheers!

Upvotes: 0

Related Questions