我有微服务架构,所有微服务都使用相同的数据库。现在我想在我的架构中使用分布式缓存。目前我选择榛子分发。首先,我将以下配置放在我的所有微服务中。
@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;
}
}
现在我开始了我的第一个微服务和日志行
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
现在我开始了第二次微服务
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
所以我想知道我是否正在做这件事,因为我在开始第二次微服务后得到这条线
[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!
我有以下查询
1。)是否是创建Hazelcast spring分布式缓存机制的正确方法?
2。)当我说分布式确实意味着如果我在两个微服务中都有相同的实体缓存,那么在一个微实体上做实体的CRUD 服务将更新其他微服务中的缓存 实体也是如此。
3。)如果我不想使用像Cacheable等弹簧缓存注释......我可以删除EnableCaching或者它不是一个好主意。我想要 以编程方式创建缓存并以编程方式管理缓存而不是 通过注释。
答案 0 :(得分:1)
绝对hazecast可以在您的用例中使用。 检查类似问题的答案 https://stackoverflow.com/a/38247515/27563
使用缓存示例检查此blogpost以进行spring boot。