org.zodiac.autoconfigure.redis.reactive.ReactiveRedissonConnectionConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.redis.reactive;
import java.lang.reflect.Field;
import org.redisson.config.Config;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.zodiac.autoconfigure.redis.RedisCacheCloudProperties;
import org.zodiac.autoconfigure.redis.RedisCacheCloudRedissonProperties;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.redis.redisson.RedissonConfig;
import org.zodiac.redis.redisson.RedissonInfo;
@SpringBootConfiguration
@ConditionalOnWebApplication(type = Type.REACTIVE)
@ConditionalOnProperty(prefix = SystemPropertiesConstants.Zodiac.SPRING_REDIS_REDISSON_PREFIX)
@ConditionalOnClass(value = {org.redisson.api.RedissonClient.class, org.redisson.spring.data.connection.RedissonConnectionFactory.class})
class ReactiveRedissonConnectionConfiguration {
@Bean
@ConditionalOnMissingBean(value = {ReactiveRedisConnectionFactory.class})
public org.redisson.spring.data.connection.RedissonConnectionFactory
redisConnectionFactory(RedisCacheCloudProperties redisConfigProperties) {
return createRedissonConnectionFactory(redisConfigProperties);
}
private org.redisson.spring.data.connection.RedissonConnectionFactory
createRedissonConnectionFactory(RedisCacheCloudProperties redisConfigProperties) {
RedisCacheCloudRedissonProperties redisRedissonConfigProperties = (RedisCacheCloudRedissonProperties)redisConfigProperties;
// TODO 根据Redisson中单点、集群、哨兵、主备等不同配置判断特殊配置
RedissonInfo redisson = redisRedissonConfigProperties.getRedisson();
RedissonConfig redissonConfig = redisson.getConfig();
Long appId = redisson.getCacheCloudAppId();
Config config = new Config();
if (appId == null) {
if (redissonConfig.getClusterServersConfig() != null) {
setConfigPrivateFiled("clusterServersConfig", config, redissonConfig.getClusterServersConfig());
}
if (redissonConfig.getSentinelServersConfig() != null) {
setConfigPrivateFiled("sentinelServersConfig", config, redissonConfig.getSentinelServersConfig());
}
if (redissonConfig.getSingleServerConfig() != null) {
setConfigPrivateFiled("singleServerConfig", config, redissonConfig.getSingleServerConfig());
}
config.setNettyThreads(redissonConfig.getNettyThreads());
config.setThreads(redissonConfig.getThreads());
}
return new org.redisson.spring.data.connection.RedissonConnectionFactory(config);
}
private void setConfigPrivateFiled(String fieldName, Config config, Object obj) {
try {
Class cl = Config.class;
Field field = cl.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(config, obj);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}