The documentation you are viewing is not the latest documentation of Micronaut Cassandra

Micronaut Cassandra Configuration

Provides integration between Micronaut and Cassandra

Version:

1 Introduction

The micronaut-cassandra module includes support for integrating Micronaut services with Cassandra.

Release History

2.0.0

  • Upgraded cassandra driver to 4.4.0 from 3.1.0

  • Micronaut 1.3.x minimum version

Migrating from 1.2.X to 2.X.X

The Datastax 4.4.0 drive renames a lot of fields so these properties will need to be migrated over. Individuals that wish to use their existing configuration will have to stay on 1.2.X.

  • cassandra.*.contactPoint and cassandra.*.port are passed to cassandra.*.basic.contact-points where contact-points is a list of strings in the format <host>:<port>

  • cassandra.*.MaxSchemaAgreementWaitSecondscassandra.*.advanced.control-connection.schema-agreement.timeout

  • cassandra.*.sslcassandra.*.advanced.ssl-engine-factory = DefaultSslEngineFactory

  • cassandra.*.clusterNamecassandra.*.basic.session-name

2 Setting up Cassandra

Using the CLI

If you are creating your project using the Micronaut CLI, supply the cassandra feature to include Cassandra configuration in your project:

$ mn create-app my-app --features cassandra

To enable the Cassandra configuration, add the following dependency to your application:

implementation("io.micronaut.cassandra:micronaut-cassandra:2.2.0")
<dependency>
    <groupId>io.micronaut.cassandra</groupId>
    <artifactId>micronaut-cassandra</artifactId>
    <version>2.2.0</version>
</dependency>

Micronaut supports Cassandra configuration by using the Datastax Java Driver. Configuration values can be supplied a property source such as below. The property name is derived from the builder methods in CqlSessionBuilder. Micronaut will create a CqlSession bean. This bean can be then injected into any other Micronaut bean.

Supply Single Configuration
cassandra:
  default:
    advanced:
      metadata:
        schema:
          enabled: false
    basic:
      contact-points:
        - "127.0.0.1:9042"
        - "127.0.0.2:8042"
      load-balancing-policy:
        local-datacenter: datacenter1

Multiple Cluster instances can be configured as follows:

Supply Multiple Configuration
cassandra:
  default:
    advanced:
      metadata:
        schema:
          enabled: false
    basic:
      contact-points:
        - "127.0.0.1:9042"
      load-balancing-policy:
        local-datacenter: datacenter1
  secondary:
    advanced:
      metadata:
        schema:
          enabled: false
    basic:
      contact-points:
        - "127.0.0.1:9043"
      load-balancing-policy:
        local-datacenter: datacenter2

3 Health Checks

When the cassandra module is activated a CassandraHealthIndicator is activated resulting in the /health endpoint and CurrentHealthStatus interface resolving the health of the Cassandra cluster.

"cassandra": {
  "status": "UP",
  "details": {
    "session": "OPEN",
    "cluster_name": "Test Cluster",
    "nodes_count": 1,
    "nodes_state": {
      "UP": 1
    }
    "nodes (10 max.)": {
      "4a9e6e53-7eed-4c1f-b497-4f93715b2b04": {
        "endpoint": "localhost/0:0:0:0:0:0:0:1:32834"
        "broadcast_address": "/172.17.0.3",
        "state": "UP",
        "distance": "LOCAL",
        "open_connections": "2",
        "cassandra_version": "3.11.2",
        "datacenter": "datacenter1",
        "rack": "rack1",
        "uptime_ms": "1593462452206",
        "is_reconnecting": "false"
      }
    }
  }
}
To disable the Cassandra health indicator entirely, add endpoints.health.cassandra.enabled: false.

See the section on the Health Endpoint for more information.

4 Additional Notes

The Datastax Cassandra driver is configured using lightbend/config under the field datastax-java-driver.\*.

The equivalent bean created for micronaut is mapped under the cassandra... fields provided in your application.conf, but the bean will fallback on the datastax-java-driver.\* values if they are present.

These config values are provided to the CqlSession under the hood. Datastax does not provide a class that maps to all the various possible config keys, but a full list of values that can be provided can be found in the DefaultDriverOption enum.

datastax configuration under lightbend/config - application.conf
datastax-java-driver {
    basic {
        contact-points = [ "1.2.3.4:9042", "5.6.7.8:9042" ]
        load-balancing-policy.local-datacenter = datacenter1
    }
}
micronaut bean configuration - application.yml
cassandra:
  default:
    basic:
      contact-points:
        - "127.0.0.1:9042"
        - "5.6.7.8:9042"
      load-balancing-policy:
        local-datacenter: datacenter1

5 Repository

You can find the source code of this project in this repository: