I have micro service architecture and all the micro services are using the same database. Now i want to have distributed cache in my architecture. For the moment i choose Hazelnut for distributed. As a start i put the following configuration in all my microservices.
@Configuration
@EnableCaching
@AutoConfigureBefore(value = { DatabaseConfiguration.class })
public class CacheConfiguration {
private final Logger LOG = LoggerFactory.getLogger(CacheConfiguration.class);
@Value("${cache.timeToLiveSeconds:3600}")
private final int timeToLiveSeconds = 3600;
@Value("${cache.backupCount:1}")
private final int backupCount = 1;
@Autowired
private Environment env;
@PreDestroy
public void destroy() {
LOG.info("Closing Cache Manager");
Hazelcast.shutdownAll();
}
@Bean
public CacheManager cacheManager(final HazelcastInstance hazelcastInstance) {
LOG.debug("Starting HazelcastCacheManager");
return new HazelcastCacheManager(hazelcastInstance);
}
@Bean
public HazelcastInstance hazelcastInstance() {
LOG.debug("Configuring Hazelcast");
final HazelcastInstance hazelCastInstance = Hazelcast
.getHazelcastInstanceByName("caching_service");
if (hazelCastInstance != null) {
LOG.debug("Hazelcast already initialized");
return hazelCastInstance;
}
final Config config = new Config();
config.setInstanceName("caching_service");
config.getNetworkConfig().setPort(5701);
config.getNetworkConfig().setPortAutoIncrement(true);
// In development, remove multicast auto-configuration
if (env.acceptsProfiles(Constants.DEV)) {
System.setProperty("hazelcast.local.localAddress", "127.0.0.1");
config.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false);
config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false);
}
config.getMapConfigs().put("default", initializeDefaultMapConfig());
return Hazelcast.newHazelcastInstance(config);
}
private MapConfig initializeDefaultMapConfig() {
final MapConfig mapConfig = new MapConfig();
/*
* Number of backups. If 1 is set as the backup-count for example, then all
* entries of the map will be copied to another JVM for fail-safety. Valid numbers
* are 0 (no backup), 1, 2, 3.
*/
mapConfig.setBackupCount(0);
/*
* Valid values are: NONE (no eviction), LRU (Least Recently Used), LFU (Least
* Frequently Used). NONE is the default.
*/
mapConfig.setEvictionPolicy(EvictionPolicy.LRU);
/*
* Maximum size of the map. When max size is reached, map is evicted based on the
* policy defined. Any integer between 0 and Integer.MAX_VALUE. 0 means
* Integer.MAX_VALUE. Default is 0.
*/
mapConfig.setMaxSizeConfig(
new MaxSizeConfig(0, MaxSizeConfig.MaxSizePolicy.USED_HEAP_SIZE));
return mapConfig;
}
}
Now i started my first micro service and the logs lines were
2017-07-16 10:38:06.581 INFO 13084 --- [ main] c.h.instance.DefaultAddressPicker : [LOCAL] [dev] [3.7.7] Picked [127.0.0.1]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
2017-07-16 10:38:06.595 INFO 13084 --- [ main] com.hazelcast.system : [127.0.0.1]:5701 [dev] [3.7.7] Hazelcast 3.7.7 (20170404 - e3c56ea) starting at [127.0.0.1]:5701
2017-07-16 10:38:06.595 INFO 13084 --- [ main] com.hazelcast.system : [127.0.0.1]:5701 [dev] [3.7.7] Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
2017-07-16 10:38:06.595 INFO 13084 --- [ main] com.hazelcast.system : [127.0.0.1]:5701 [dev] [3.7.7] Configured Hazelcast Serialization version : 1
2017-07-16 10:38:06.769 INFO 13084 --- [ main] c.h.s.i.o.impl.BackpressureRegulator : [127.0.0.1]:5701 [dev] [3.7.7] Backpressure is disabled
2017-07-16 10:38:07.171 DEBUG 13084 --- [ main] c.h.internal.cluster.ClusterService : [127.0.0.1]:5701 [dev] [3.7.7] Updating members [Member [127.0.0.1]:5701 - b2e3c44d-0392-4813-a2c8-d648695e8f1d this]
2017-07-16 10:38:07.171 DEBUG 13084 --- [ main] c.h.i.p.InternalPartitionService : [127.0.0.1]:5701 [dev] [3.7.7] Adding Member [127.0.0.1]:5701 - b2e3c44d-0392-4813-a2c8-d648695e8f1d this
2017-07-16 10:38:07.293 INFO 13084 --- [ main] c.h.s.i.o.impl.OperationExecutorImpl : [127.0.0.1]:5701 [dev] [3.7.7] Starting 8 partition threads
2017-07-16 10:38:07.295 INFO 13084 --- [ main] c.h.s.i.o.impl.OperationExecutorImpl : [127.0.0.1]:5701 [dev] [3.7.7] Starting 5 generic threads (1 dedicated for priority tasks)
2017-07-16 10:38:07.302 INFO 13084 --- [ main] com.hazelcast.core.LifecycleService : [127.0.0.1]:5701 [dev] [3.7.7] [127.0.0.1]:5701 is STARTING
2017-07-16 10:38:07.302 DEBUG 13084 --- [ main] c.h.i.p.InternalPartitionService : [127.0.0.1]:5701 [dev] [3.7.7] Adding Member [127.0.0.1]:5701 - b2e3c44d-0392-4813-a2c8-d648695e8f1d this
2017-07-16 10:38:07.303 INFO 13084 --- [ main] c.h.n.t.n.NonBlockingIOThreadingModel : [127.0.0.1]:5701 [dev] [3.7.7] TcpIpConnectionManager configured with Non Blocking IO-threading model: 3 input threads and 3 output threads
2017-07-16 10:38:07.305 DEBUG 13084 --- [ main] c.h.n.t.n.NonBlockingIOThreadingModel : [127.0.0.1]:5701 [dev] [3.7.7] IO threads selector mode is SELECT
2017-07-16 10:38:07.317 WARN 13084 --- [ main] com.hazelcast.instance.Node : [127.0.0.1]:5701 [dev] [3.7.7] No join method is enabled! Starting standalone.
2017-07-16 10:38:07.351 INFO 13084 --- [ main] com.hazelcast.core.LifecycleService : [127.0.0.1]:5701 [dev] [3.7.7] [127.0.0.1]:5701 is STARTED
Now i started my second micro service
2017-07-16 10:41:30.841 INFO 17936 --- [ main] c.h.instance.DefaultAddressPicker : [LOCAL] [dev] [3.7.7] Picked [127.0.0.1]:5702, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5702], bind any local is true
2017-07-16 10:41:30.854 INFO 17936 --- [ main] com.hazelcast.system : [127.0.0.1]:5702 [dev] [3.7.7] Hazelcast 3.7.7 (20170404 - e3c56ea) starting at [127.0.0.1]:5702
2017-07-16 10:41:30.855 INFO 17936 --- [ main] com.hazelcast.system : [127.0.0.1]:5702 [dev] [3.7.7] Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
2017-07-16 10:41:30.855 INFO 17936 --- [ main] com.hazelcast.system : [127.0.0.1]:5702 [dev] [3.7.7] Configured Hazelcast Serialization version : 1
2017-07-16 10:41:31.017 INFO 17936 --- [ main] c.h.s.i.o.impl.BackpressureRegulator : [127.0.0.1]:5702 [dev] [3.7.7] Backpressure is disabled
2017-07-16 10:41:31.451 DEBUG 17936 --- [ main] c.h.internal.cluster.ClusterService : [127.0.0.1]:5702 [dev] [3.7.7] Updating members [Member [127.0.0.1]:5702 - db64274e-73bd-4aa2-ae67-f626433b25c6 this]
2017-07-16 10:41:31.452 DEBUG 17936 --- [ main] c.h.i.p.InternalPartitionService : [127.0.0.1]:5702 [dev] [3.7.7] Adding Member [127.0.0.1]:5702 - db64274e-73bd-4aa2-ae67-f626433b25c6 this
2017-07-16 10:41:31.582 INFO 17936 --- [ main] c.h.s.i.o.impl.OperationExecutorImpl : [127.0.0.1]:5702 [dev] [3.7.7] Starting 8 partition threads
2017-07-16 10:41:31.583 INFO 17936 --- [ main] c.h.s.i.o.impl.OperationExecutorImpl : [127.0.0.1]:5702 [dev] [3.7.7] Starting 5 generic threads (1 dedicated for priority tasks)
2017-07-16 10:41:31.590 INFO 17936 --- [ main] com.hazelcast.core.LifecycleService : [127.0.0.1]:5702 [dev] [3.7.7] [127.0.0.1]:5702 is STARTING
2017-07-16 10:41:31.591 DEBUG 17936 --- [ main] c.h.i.p.InternalPartitionService : [127.0.0.1]:5702 [dev] [3.7.7] Adding Member [127.0.0.1]:5702 - db64274e-73bd-4aa2-ae67-f626433b25c6 this
2017-07-16 10:41:31.591 INFO 17936 --- [ main] c.h.n.t.n.NonBlockingIOThreadingModel : [127.0.0.1]:5702 [dev] [3.7.7] TcpIpConnectionManager configured with Non Blocking IO-threading model: 3 input threads and 3 output threads
2017-07-16 10:41:31.592 DEBUG 17936 --- [ main] c.h.n.t.n.NonBlockingIOThreadingModel : [127.0.0.1]:5702 [dev] [3.7.7] IO threads selector mode is SELECT
2017-07-16 10:41:31.633 WARN 17936 --- [ main] com.hazelcast.instance.Node : [127.0.0.1]:5702 [dev] [3.7.7] No join method is enabled! Starting standalone.
2017-07-16 10:41:31.633 WARN 17936 --- [ main] com.hazelcast.instance.Node : [127.0.0.1]:5702 [dev] [3.7.7] Config seed port is 5701 and cluster size is 1. Some of the ports seem occupied!
2017-07-16 10:41:31.663 INFO 17936 --- [ main] com.hazelcast.core.LifecycleService : [127.0.0.1]:5702 [dev] [3.7.7] [127.0.0.1]:5702 is STARTED
so i wanted to know if i am doing things right here since i get this line after started second microservice
[127.0.0.1]:5702 [dev] [3.7.7] Config seed port is 5701 and cluster size is 1. Some of the ports seem occupied!
I have the following queries
1.) Is the the right way to created Hazelcast spring distributed cache mechanism ?
2.) When i say distributed does it mean that if i have same entity cache in both the micro services , doing CRUD on entity in one micro service will update the cache in other micro services for the same entity as well.
3.) If i don't want to use spring cache annotations like Cacheable etc... can i remove EnableCaching or it is not a good idea. I want to create cache programmatically and manage cache programmatically and not by annotations.
Definitely hazecast can work in your use case. Check this answer to similar question https://stackoverflow.com/a/38247515/27563
Check this blogpost with caching example for spring boot.