如何在填充Redis缓存时指定集群配置

时间:2019-05-29 08:19:08

标签: redis redis-cluster

我正在Redis中建立2个集群,每个集群中有6个节点。 在配置文件中提到了所有clusterNodes。 下面是用Java完成的集群配置。

public class RedisCServiceImpl implements RedisCService 
   {
   private static Logger logger = 
    LoggerFactory.getLogger(RedisCServiceImpl.class);
    private StatefulRedisClusterConnection<String, Object> 
   redisClusterConnection;
   private RedisClusterClient clusterClient;
@Value("${redis.cluster.nodes:}")
private String redisClusterNodes;

@Value("${redis.cluster.adaptiveRefreshTriggersTimeout:30}")
private int adaptiveRefreshTriggersTimeoutSec;

@Value("${redis.cluster.enablePeriodicRefreshMins:5}")
private int enablePeriodicRefreshMins;

@Value("${redis.cluster.subscriptionTimeoutSecs:15}")
private int subscriptionTimeoutSecs;

@Autowired RedisMessageListener redisMessageListener;

@Autowired
List<Regions> regions;


@PostConstruct
private void init() {
    if (redisClusterNodes.isEmpty()) {
        logger.warn("REDIS cluster configuration not found");
        return;
    }
    String[] arrRedisNodes = redisClusterNodes.split(",");
    ArrayList<RedisURI> redisURIs = new ArrayList<>();
    for (String node: arrRedisNodes) {
        String[] arr = node.split(":");
        if (arr.length > 1) {
            redisURIs.add(RedisURI.create(arr[0], Integer.valueOf(arr[1])));
        }
    }

    clusterClient = RedisClusterClient.create(redisURIs);

    ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
            .enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT, ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS)
            .adaptiveRefreshTriggersTimeout(adaptiveRefreshTriggersTimeoutSec, TimeUnit.SECONDS)
            .enablePeriodicRefresh(enablePeriodicRefreshMins, TimeUnit.MINUTES)
            .enableAllAdaptiveRefreshTriggers()
            .build();

    clusterClient.setOptions(ClusterClientOptions.builder()
            .topologyRefreshOptions(topologyRefreshOptions)
            .build());

    redisClusterConnection = clusterClient.connect(new SerializedObjectCodec());

    StatefulRedisClusterPubSubConnection<String, String> pubSubConnection = clusterClient.connectPubSub();
    RedisPubSubReactiveCommands<String, String> reactive = pubSubConnection.reactive();

    pubSubConnection.setNodeMessagePropagation(true);
    put("notify-keyspace-events", "KAE");
    riqRegions.forEach((RiqRegion riqRegion) -> {
            if (riqRegion.isSubscribe()) {
                String pattern = "__keyspace@0__:" + riqRegion.getKeyPrefix() + "*";
                logger.info("Subscribing for key pattern: {}", pattern);
                //Make sure that the subscription is successful before moving on
                //reactive.psubscribe(pattern).toBlocking().subscribe(message -> logger.info("Subscribe notification: {}", message));               //We were unsuccessful in getting reactive API to work consistently. Decided to settle on sync() API.
                pubSubConnection.sync().masters().commands().psubscribe(pattern);
                pubSubConnection.sync().slaves().commands().psubscribe(pattern);
            }
        }
    );

    reactive.observePatterns()
           .doOnNext(x -> redisMessageListener.clearLocalEntry(x.getChannel(), x.getMessage())).subscribe();
}

现在,当我有了差异集群时,如何从代码角度指定集群,我需要在其中缓存数据?

我认为的一种方法是更改​​代码   <K, V>void put(Region region, K key, V value);

到    <K, V>void put(Region region, K key, V value, Cluster cluster);

0 个答案:

没有答案