It looks like the default Spring boot auto configuration will create two hazelcast instances when using JCache and caching is enabled (@EnableCaching
)
Full example at: https://github.com/dirkvanrensburg/hazelcast-springboot-jcache
TLDR; Is there a way to get Spring boot's autoconfiguration to only create one Hazelcast instance when enabling caching through JCache?
I created a demo Spring boot project by adding the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
<version>${hazelcast.version}</version>
</dependency>
and adding @EnableCaching
to the Application class, Spring will auto configure Hazelcast but start two hazelcast instances, which joins in a cluster as evidenced in the logs:
Members [2] {
Member [192.168.1.157]:5701 - 3eabbe90-6815-49ff-8d93-9e4b12e67810
Member [192.168.1.157]:5702 - e9c93366-2408-4726-965a-b21dcf897113 this
}
The caching works but I don't want two instances of Hazelcast.
Hack
I managed to make it work by providing my own cache manager:
@Bean
public CacheManager springHzProvider(HazelcastInstance instance) {
return SpringHazelcastCachingProvider.getCacheManager(instance, null, new Properties());
}
and removing the hazelcast
and hazelcast-spring
dependencies and adding hazelcast-all
:
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>${hazelcast.version}</version>
</dependency>
But the question remains whether there is better 'proper' way of achieving this? Ideally without defining a custom cache manager and adding hazelcast-all
@EnableAutoConfiguration
annotation to exclude Spring Boot'sHazelcastAutoConfigurtation
class and remove yourspringHzProvider
method. It's not an ideal solution but a bit cleaner.HazelcastClientProxy
to the classpath. Do you understand what is going on here? Is it perhaps a bug inCacheAutoConfiguration
? If you want to add your comment as an answer then I'll accept it since it addresses both of my concerns (custom cachemanager, adding hazelcast-all)1.5.3.RELEASE