Caching With Spring Boot and Hazelcast
In this article, I’ll explain step-by-step how to enable Hazelcast as a cache manager for your Spring Boot application.
Join the DZone community and get the full member experience.
Join For FreeSpring Boot is very well integrated with Hazelcast. It’s enough to provide Hazelcast configuration on the classpath and it will be automatically used. In this blog post, I’ll explain step-by-step how to enable Hazelcast as a cache manager for your Spring Boot application.
Source code from this blog is posted on the Hazelcast guide repository on Github.
Spring Boot Application
To use caching in your Spring Boot application, you need to:
- Add
org.springframework.boot:spring-boot-starter-cache
dependency - Add
@EnableCaching
annotation to your main class - Add
@Cacheable("books")
annotation to every method you want to cache
For more explanation on the Spring Boot cache topic, please check the official Caching Data with Spring guide.
In our case, let’s have a simple web service with two classes defined as follows.
// BookController.java
"/books") (
public class BookController {
private BookService bookService;
"/{isbn}") (
public String getBookNameByIsbn( ("isbn") String isbn) {
return bookService.getBookNameByIsbn(isbn);
}
}
xxxxxxxxxx
// BookService.java
public class BookService {
"books") (
public String getBookNameByIsbn(String isbn) {
return findBookInSlowSource(isbn);
}
private String findBookInSlowSource(String isbn) {
// some long processing
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Sample Book Name";
}
}
If we started the application, then every call to the endpoint /books/<isbn>
would go to the method findBookNameByIsbn()
which in turn would first check the cache. Only if it does not find value in the cache, the method findBookInSlowSource()
would be executed.
Using Hazelcast as Cache Manager
We want to use Hazelcast as the cache manager. The good news is that all you have to do it to add Hazelcast to your classpath.
xxxxxxxxxx
<!-- pom.xml -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>4.0.2</version>
</dependency>
Then, you need to add Hazelcast configuration in one of the following manners:
- Add
hazelcast.yaml
configuration OR - Add
hazelcast.xml
configuration OR - Define
@Bean
with Hazelcast configuration in the source code
Let’s use the first option and add the following file into src/main/resources
.
xxxxxxxxxx
# hazelcast.yaml
hazelcast
network
join
multicast
enabledtrue
No more configuration needed, Hazelcast is already used as the cache manager in your project!
Starting the Application
To start the application, run the following command.
mvn spring-boot:run
You should see in the logs that embedded Hazelcast has started.
Members {size:1, ver:1} [
Member [172.30.63.9]:5701 - 75cd0b19-ee36-4e0a-9d9c-38c49f67f842 this ]
Testing the Application
You can test the application by executing the following command.
curl locahost:8080/books/123
Sample Book Name
The first time you execute this command it should take some time to get the response. However, when you try it again, it’s instant. That means that the cache is used.
curl locahost:8080/books/123
Sample Book Name
What’s More?
Spring Boot is really well integrated with Hazelcast and vice-versa. If you want to use Hazelcast in the client-server topology, then it’s enough if you create hazelcast-client.yaml
file instead of hazelcast.yaml
on your classpath. And that’s it! You configured Hazelcast client.
To read more check out the official documentation Spring Boot: Hazelcast.
Published at DZone with permission of Rafał Leszko. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments