spring cloud 采用nacos作为注册中心,动态配置redisson

需求

关于spring-boot整合redisson的相关方案网上有很多,主要是通过redisson-spring-boot-starter实现自动装配,现有的项目采用spring-cloudnacos作为服务的发现和注册中心,redis的相关配置信息不在本地保存,集中放到配置中心,项目启动时从配置中心读取配置文件,构建对应的SingleServerProperties,截止本文的发布,redisson-spring-boot-starter仍然不能很好的支持从nacos读取属性完成配置(原因在于:SingleServerProperties需要的配置信息无法通过单一的POJO描述,其中的部分属性依赖其它的bean,必须从最小的单元装配,最后整合装配成);


    org.redisson
    redisson-spring-boot-starter
    3.13.3

SingleServerProperties.java

@Bean(destroyMethod="shutdown")
RedissonClient redissonClient(SingleServerProperties properties) throws IOException {
    ObjectMapper mapper=new ObjectMapper();
    String jsonString = mapper.writeValueAsString(properties);
    Config config = Config.fromYAML(jsonString);
    if (StrUtil.isEmpty(properties.getSingleServerConfig().getPassword())){
        config.useSingleServer().setPassword(null);
    }
    return Redisson.create(config);
}

解决思路

拆分SingleServerProperties,分解到最小单元(所有的属性均是基础类型,不再依赖其他的bean),从最小的单元装配,最后整合装配成SingleServerProperties
参考SingleServerProperties的源码,重写该部分
image
BaseConfig.java

@Data
public class BaseConfig>  {
    /**
 * If pooled connection not used for a timeout time * and current connections amount bigger than minimum idle connections pool size, * then it will closed and removed from pool. * Value in milliseconds. */ @Value("${redisson.singleServerConfig.idleConnectionTimeout:}")
    private int idleConnectionTimeout = 10000;
    /**
 * Timeout during connecting to any Redis server. * Value in milliseconds. */ @Value("${redisson.singleServerConfig.connectTimeout:}")
    private int connectTimeout = 10000;
    /**
 * Redis server response timeout. Starts to countdown when Redis command was succesfully sent. * Value in milliseconds. */ @Value("${redisson.singleServerConfig.timeout:}")
    private int timeout = 3000;
    @Value("${redisson.singleServerConfig.retryAttempts:}")
    private int retryAttempts = 3;
    @Value("${redisson.singleServerConfig.retryInterval:}")
    private int retryInterval = 1500;
    /**
 * Password for Redis authentication. Should be null if not needed */ 
    @Value("${redisson.singleServerConfig.password:}")
    private String password;
    private String username;
    /**
 * Subscriptions per Redis connection limit */ 
    @Value("${redisson.singleServerConfig.subscriptionsPerConnection:}")
    private int subscriptionsPerConnection = 5;
    /**
    * Name of client connection */ 
    @Value("${redisson.singleServerConfig.clientName:}")
    private String clientName;
    private boolean sslEnableEndpointIdentification = true;
    private SslProvider sslProvider = SslProvider.JDK;
    private URL sslTruststore;
    private String sslTruststorePassword;
    private URL sslKeystore;
    private String sslKeystorePassword;
    private int pingConnectionInterval;
    private boolean keepAlive;
    private boolean tcpNoDelay;
}

Codec.java

@Data
@Component
public class Codec implements Serializable {
    private static final long serialVersionUID = -8394853238388494359L;
    @JsonProperty(value = "class")
    @Value("${redisson.codec.class}")
    String cls;
}

SingleServerConfig.java

@EqualsAndHashCode(callSuper = true)
@Data
@Component
public class SingleServerConfig extends BaseConfig {
    private static final long serialVersionUID = -3173391729030180033L;
    
    /*** Redis server address */ 
    @Value("${redisson.singleServerConfig.address:}")
    private String address;
    
    /**Minimum idle subscription connection amount */
    @Value("${redisson.singleServerConfig.subscriptionConnectionMinimumIdleSize:}")
    private int subscriptionConnectionMinimumIdleSize = 1;
    
    /*** Redis subscription connection maximum pool size * */ 
    @Value("${redisson.singleServerConfig.subscriptionConnectionPoolSize:}")
    private int subscriptionConnectionPoolSize = 50;
    
    /**Minimum idle Redis connection amount */ 
    @Value("${redisson.singleServerConfig.connectionMinimumIdleSize:}")
    private int connectionMinimumIdleSize = 24;
    
    /* Redis connection maximum pool size */
    @Value("${redisson.singleServerConfig.connectionPoolSize:}")
    private int connectionPoolSize = 64;
    
    /**Database index used for Redis connection */ 
    @Value("${redisson.singleServerConfig.database:}")
    private int database = 0;
    
    /**Interval in milliseconds to check DNS */   
    @Value("${redisson.singleServerConfig.dnsMonitoringInterval:}")
    private long dnsMonitoringInterval = 5000;
}

SingleServerProperties.java

@Data
@Component
public class SingleServerProperties implements Serializable {
    private static final long serialVersionUID = -385286650728472483L;
    @Autowired
    private SingleServerConfig singleServerConfig;
    
    @Value("${redisson.threads:}")
    private int threads;
    
    @Value("${redisson.nettyThreads:}")
    private int nettyThreads;
    
    @Value("${redisson.transportMode:}")
    private String transportMode;
    
    @Autowired
    private Codec codec;
}

最后集中配置

@Configuration
@ComponentScan
public class RedissonConfiguration {
    @Autowired
 SingleServerProperties singleServerProperties;
    @Bean(destroyMethod="shutdown")
    RedissonClient redissonClient(SingleServerProperties properties) throws IOException {
        ObjectMapper mapper=new ObjectMapper();
        String jsonString = mapper.writeValueAsString(properties);
        Config config = Config.fromYAML(jsonString);
        if (StrUtil.isEmpty(properties.getSingleServerConfig().getPassword())){
            config.useSingleServer().setPassword(null);
        }
        return Redisson.create(config);
    }
}

你可能感兴趣的:(spring cloud 采用nacos作为注册中心,动态配置redisson)