Hazelcast: Setup Using Java
Hazelcast: Setup Using Java
This article defines setting up Hazelcast server and client. Here,Spring is used for performing CRUD and java utilities are standalone classes for nodes set-up.
Join the DZone community and get the full member experience.Join For Free
Thank you all for reading my last article, Hazelcast: Introduction and Clustering, which gives you an introduction about the 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 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.
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.
You can run this class multiple to create multiple nodes. These nodes will automatically join themselves and form a cluster.
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 cluster do 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.
Published at DZone with permission of Dheeraj Gupta . See the original article here.
Opinions expressed by DZone contributors are their own.