Hazelcast: Setup Using Java

Dheeraj Gupta
2 min readFeb 22, 2021

Thank you all for reading my last article, Hazelcast: Introduction and Clustering, which gives you an introduction to caching and clustering technologies. In this, we will be moving with a Spring Boot project to set up a simple Hazelcast server and client.

Let’s start by adding the dependency to our “pom.xml” file or “build.gradle” depending on the type of build tool.

com.hazelcast:hazelcast is the core Hazelcast jar that adds the functions regarding the utilities such as IMAP, Hazelcast instance, etc. hazelcast-client is the java native library to create a hazelcast client to fetch and put the data in the cache.

Configurations:

We can configure Hazelcast either using XML or using java. Since we are using spring boot let’s move ahead with the second approach.

After configuring the Hazelcast let’s add a controller to manipulate data (perform CRUD) in the cluster.

Here we will add the HazelcastInstance interface which helps to perform CRUD in the Hazelcast cache.

In this project, I’ve also added to standalone utilities that can create a Hazelcast server and a Hazelcast client when you run them by themselves.

Hazelcast Server:

You can run this class multiple to create multiple nodes. These nodes will automatically join themselves and form a cluster.

Hazelcast Client:

This is a standalone utility class to fetch data from the cluster.

Hazelcast by default uses multicast to discover nodes in a cluster. We can also configure the discovery of our environment by TCP/IP. We can set up the ports and add machine members programmatically. Hazelcast by default books 100 ports for itself starting from 5701. In this file, we can also add the number of nodes we want to create and the cluster does not reserve all the ports. In this project, I have used more of the default network configuration and did not configure this.

Our Hazelcast cluster and client are ready. In the above project, while I use spring boot configuration and controller to create and doing CRUD on cache, I also created some utilities to create standalone java classes to create Hazelcast nodes and clients, so that Spring dependency could be lowered.

Again you can find this code at my Github repo: https://github.com/dheerajgupta217/hazelcast-setup

In the next article, Hazelcast Mancenter: Manage your cache, we will be looking into the Hazelcast Management center to manipulate and read caches or maps.

Originally published at https://dzone.com.

--

--

Dheeraj Gupta

Senior Software Developer. Develops in Java, Spring Boot. Query in MySql. Deploys on AWS Cloud. Learns Hadoop. Coder @ https://github.com/dheerajgupta217