1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| @ConfigurationProperties(prefix = "summer.redis") public class RedisConfig {
private String seedAddress; private String password;
private LettuceClientConfiguration getLettuceClientConfiguration() { ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder() .dynamicRefreshSources(true) .enablePeriodicRefresh(Duration.ofSeconds(30)) .enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.UNKNOWN_NODE) .adaptiveRefreshTriggersTimeout(Duration.ofSeconds(25)) .build();
return LettucePoolingClientConfiguration.builder() .clientOptions(ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build()) .build(); }
private RedisClusterConfiguration getRedisClusterConfiguration(String seedAddress) { RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration(); getRedisNodes(seedAddress).forEach(clusterConfig::addClusterNode); clusterConfig.setPassword(RedisPassword.of(password)); return clusterConfig; }
private List<RedisNode> getRedisNodes(String seedAddress) { return Arrays.stream(seedAddress.split(",")) .map(s -> { String[] seed = s.split(":"); String host = seed[0]; int port = Integer.parseInt(seed[1]); log.info("[RedisConnectionFactory] Add Cluster Seed. host: {}, port: {}", host, port); return RedisNode.newRedisNode().listeningAt(host, port).build(); }).collect(Collectors.toList()); }
@Bean public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() { return new LettuceConnectionFactory(getRedisClusterConfiguration(seedAddress), getLettuceClientConfiguration()); }
@Bean ReactiveRedisTemplate<String, String> stringReactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) { return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, RedisSerializationContext.string()); } }
|