redisson 基於spring boot 2.0的集群配置


redisson 基於spring boot 2.0的集群配置

redisson客戶端 pom配置

        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.16.1</version>
        </dependency>

代碼示例

@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedissonConfig {

    /**
     * redisson 配置
     * @param properties
     * @return
     */
    @Bean
    public RedissonClient getRedisson(RedisProperties properties) {
        Config config = new Config();
        if (properties.getCluster() != null && properties.getCluster().getNodes() != null ) {
            String[] nodes = properties.getCluster().getNodes().toArray(new String[0]);
            String[] result = new String[nodes.length];
            for (int i = 0; i < nodes.length; i++) {
                String nodePart = "redis://"+nodes[i];
                result[i] = nodePart;
            }
            //集群配置
            config.useClusterServers()
                    .setScanInterval(10000)
                    .addNodeAddress(result)
                    .setPassword(properties.getPassword());
        } else {
            //單機配置
config.useSingleServer().setAddress(properties.getHost() + ":" + properties.getPort()).setPassword(properties.getPassword());
 } return Redisson.create(config); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM