HazelCast in Spring-boot Application
How to configure a hazelcast client in a spring boot application
HazelCast
Hazelcast is an In-Memory Computing Platform is comprised of Hazelcast IMDG, the most widely deployed in-memory data grid, and Hazelcast Jet, the industry’s most advanced in-memory stream processing solution
How to configure Java Spring boot application to connect running HazelCast member instance.
Create a Spring-boot application
Follow the below spring starter project link to create a spring-boot application
Import the project to your favorite IDE
Once you import the project to your favorite IDE, start the boot application.
Configure HazelCast with Spring-boot
Add the below Maven dependency to your project pom.xml
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
<version>3.12.5</version>
</dependency>
Connect to a HazleCast Instance
We can connect to hazelcast using hazelcast.xml or Java Config, let us see how to set-up the connection using java-config.
Create a Java Class and add the method in the below example.
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.hazelcast.HazelcastKeyValueAdapter;
import org.springframework.data.hazelcast.repository.config.EnableHazelcastRepositories;
import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.keyvalue.core.KeyValueTemplate;@Configuration
public class HazelCastConfig {@Bean
HazelcastInstance hazelcastInstance() {
ClientConfig clientConfig = new ClientConfig();
return HazelcastClient.newHazelcastClient(clientConfig);
}@Bean
public KeyValueOperations keyValueTemplate() {
return new KeyValueTemplate(new HazelcastKeyValueAdapter(hazelcastInstance()));
}@Bean
public HazelcastKeyValueAdapter hazelcastKeyValueAdapter(@Qualifier("hazelcastInstance") HazelcastInstance hazelcastInstance) {
return new HazelcastKeyValueAdapter(hazelcastInstance);
}}
Build and Deploy spring-boot
Once you successfully build your project, run the spring-boot application either through the command line or with in your IDE. There should not be any start-up errors if your HazelCast instance is already up and running
Github link
You can find a working code for this example at our git repo
If you find this article was helpful to you, please do press the 👏 clap button and help others find this story too.