

Table of Contents
1. Overview
Spring boot microservice architecture requires sharing cache between two micro-services which are running independently so here we are explaining Spring boot hazelcast example and hazelcast configuration in spring boot.
Let’s try to understand the requirement of hazelcast, For example, two microservices are running separately, Obviously in separate JVM
. Now a requirement is like, value write by one microservice and read by another microservice which are running independent JVM
, To achieve memory sharing hazelcase will help us. Hazelcast supports Map
, List
, Set
and many other data structures, Here we are using Map
in our example.
2. Example
Here is an example of spring boot hazel case configuration :

Spring boot hazelcast example
2.1 pom.xml
To configure hazelcast
and hazelcast-spring
must be required in CLASSPATH
.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring-boot-example</groupId> <artifactId>spring-boot-hazelcast-example</artifactId> <version>1.0-SNAPSHOT</version> <description>Spring boot hazelcast example</description> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.hazelcast</groupId> <artifactId>hazelcast</artifactId> </dependency> <dependency> <groupId>com.hazelcast</groupId> <artifactId>hazelcast-spring</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.2 application.properties
server.port=8282
2.3 SpringBootConfig
package com.javadeveloperzone; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; /** * Created by JavaDeveloperZone on 19-07-2017. */ @SpringBootApplication @ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute public class SpringBootConfig { public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootConfig.class, args); // it wil start application } }
2.4 HazelcastConfiguration
Here we have created a bean for hazelcast
which has configuration related to hazel cast. In bellow code comments has been given to each line which is self-explanatory.
package com.javadeveloperzone; import com.hazelcast.config.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Created by JavaDeveloperZone on 08-01-2018. */ @Configuration public class HazelcastConfiguration { @Bean public Config hazelCastConfig(){ Config config = new Config(); config.setInstanceName("hazelcast-instance") // hazel case instance name .addMapConfig( new MapConfig() // create map .setName("configuration") .setMaxSizeConfig(new MaxSizeConfig(200, MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE)) .setEvictionPolicy(EvictionPolicy.LRU) .setTimeToLiveSeconds(-1)); // cache will be available until it will remove manually. less then 0 means never expired. return config; } }
2.5 HazelcastController
The controller is doing the job for simple read and write value to hazelcast Map
. This map will be shared between different JVM of the same hazelcast cluster.
package com.javadeveloperzone.controller; import com.hazelcast.core.Hazelcast; import com.hazelcast.core.HazelcastInstance; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * Created by JavaDeveloperZone on 19-07-2017. */ @RestController public class HazelcastController { @Autowired private HazelcastInstance instance; // autowire hazel cast instance @RequestMapping("/write") public String write(@RequestParam("values")String values) { java.util.Map<String,String> stringStringMap = instance.getMap("configuration"); // get map from hazel cast stringStringMap.put("data",values); // write value, This value will be accessible from another jvm also return "Value has been write to Hazelcast"; } @RequestMapping("/read") public String read() { java.util.Map<String,String> stringStringMap = instance.getMap("configuration"); // get map from hazel cast return "Hazelcast values is :" +stringStringMap.get("data"); // read value } }
2.6 Demo
For demo, we will start the same application with the different port means two instances of same application will be running on different whereJVM
data will be written by instance1 and read by instance2
.
-
Start Instance1
java -jar spring-boot-hazelcast-example-1.0-SNAPSHOT.jar --server.port=8181
2018-01-13 10:24:34.240 INFO 134680 --- [ main] c.h.i.cluster.impl.MulticastJoiner : [192.168 .0.103]:5701 [dev] [3.7.7] Members [1] { Member [192.168.0.103]:5701 - c6f63ad1-1988-46e0-bc90-cbb534c40570 this }
2. Start instance2
java -jar spring-boot-hazelcast-example-1.0-SNAPSHOT.jar --server.port=8282
2018-01-13 10:25:49.214 INFO 59140 --- [ration.thread-0] c.h.internal.cluster.ClusterService : [192.168. 0.103]:5702 [dev] [3.7.7] Members [2] { Member [192.168.0.103]:5701 - c6f63ad1-1988-46e0-bc90-cbb534c40570 Member [192.168.0.103]:5702 - d6e539a0-2835-4504-be66-67903d6922f0 this }
3. Write Data in instance1
Value has been write using 8181
port instance

Spring boot hazelcast example write value
4. Read Data from instance2
Value has been read using 8282
port instance

Spring boot hazelcast example read value
3. References
4. Source code
Spring Boot Hazelcast example (170 KB)