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.configuration:micronaut-cassandra:2.0.0")
<dependency>
    <groupId>io.micronaut.configuration</groupId>
    <artifactId>micronaut-cassandra</artifactId>
    <version>2.0.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 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

4 Repository

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