Getting Started with Hazelcast using Micronaut

See something wrong? Edit this page.

This guide will get you started to use Hazelcast in a Micronaut application.

You can see the whole project here.

What You’ll Learn

In this guide, you will learn how to use Hazelcast IMDG within Micronaut microservices.

The Micronaut application contains two REST controllers which helps you to put data and read it back. The application initializes a single Hazelcast IMDG member instance which is used to keep the data. When you run the application multiple times, Hazelcast IMDG instances build a cluster and share the data.

Prerequisites

  • ~15 minutes

  • Apache Maven 3.2+

  • JDK 11+

The Micronaut Application Structure

The application in the initial directory is a basic Micronaut app having three endpoints:

  • / is the homepage returning “Homepage” string only

  • /put is the mapping where key and value are saved to a local map through @CachePut annotation.

  • /get is the mapping where the values in the local map can be obtained by keys through @Cacheable annotation.

Using Hazelcast in the Application

To configure caching with Hazelcast, we’ll start by adding some dependencies to our pom.xml file:

<dependency>
    <groupId>io.micronaut.cache</groupId>
    <artifactId>micronaut-cache-hazelcast</artifactId>
    <version>${micronaut-cache-hazelcast.version}</version>
</dependency>
<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast</artifactId>
    <version>${hazelcast.version}</version>
</dependency>

The first dependency is Micronaut Cache for Hazelcast, the second one is Hazelcast IMDG itself.

Now, we just need to add a configuration bean to enable Hazelcast:

@Factory
public class HazelcastConfiguration {

    @Bean
    public Config hazelcastConfig() {
        Config configuration = new Config()
          .setClusterName("micronaut-cluster");
        JoinConfig joinConfig = configuration.getNetworkConfig().getJoin();
        joinConfig.getMulticastConfig().setEnabled(false);
        joinConfig.getTcpIpConfig().setEnabled(true).addMember("localhost");
        return configuration;
    }
}

This allows defining a custom configuration for our Hazelcast instance bean. We enable TCP/IP config for the discovery.

Our application with Hazelcast caching is now ready to go. We don’t need to change anything else because we’re already using Micronaut caching annotations in the CommandService class.

Running the Sample Application

Build the application by using Maven:

mvn clean package

The very first request to each application instance will take a bit longer time to respond since the first request causes the Hazelcast instance to be started.

Run the application by typing:

java -jar target/hazelcast-embedded-micronaut-0.1.0.jar 8080

Open another terminal and rerun the application by typing

java -jar target/hazelcast-embedded-micronaut-0.1.0.jar 8081

Now, you can issue HTTP calls to read/write data from the cache.

Firstly, open another terminal and run the following command to put the data into a Hazelcast distributed map:

curl  "localhost:8080/put?key=key1&value=hazelcast"

You will see the value in the output. Then run the command below to get the data back. Please note we’re calling the other application instance:

curl "localhost:8081/get?key=key1"

Again, you will see the value in the output since the data is distributed among Hazelcast cluster instances and can be accessed from any of them.

Summary

In this guide, we developed a simple application that stores the data in a Hazelcast instance. We started two application instances, and they formed a Hazelcast cluster. Since the data was shared among Hazelcast cluster instances, we could access it from both application instances.