美文网首页
Redisson连接redis单机和哨兵模式

Redisson连接redis单机和哨兵模式

作者: 程序员小白成长记 | 来源:发表于2021-06-20 02:55 被阅读0次

    从图中可以看出redisson对于不同的redis模式给出了不同的配置类

    • MasterSlaveServersConfig
    • SentinelServersConfig
    • SingleServerConfig
    image.png

    具体配置

    • application.properties
    # redisson-single
    # redisson.address=redis://101.241.71.70:9090
    # redisson.password=123456
    
    
    # redisson-sentinel
    redisson.masterName=mymaster
    redisson.schema=redis://
    redisson.sentinelAddresses=101.241.71.70:26377,101.241.71.70:26378,101.241.71.70:26379
    redisson.password=123456
    
    • pom.xml
    <dependency>
        <groupId>org.redisson</groupId>
        <artifactId>redisson</artifactId>
        <!--<version>3.11.4</version>-->
        <version>3.13.4</version>
    </dependency>
    
    • RedissionConfiguration.class
    import org.apache.commons.lang3.StringUtils;
    import org.redisson.Redisson;
    import org.redisson.api.RedissonClient;
    import org.redisson.config.Config;
    import org.redisson.config.SentinelServersConfig;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    
    @Configuration
    @ConfigurationProperties(prefix = "redisson")
    public class RedissionConfiguration {
        private int timeout = 3000;
        private String schema = "redis://";
        private String address;
        private String password;
        private int connectionPoolSize = 64;
        private int connectionMinimumIdleSize = 10;
        private int slaveConnectionPoolSize = 250;
        private int masterConnectionPoolSize = 250;
        private String[] sentinelAddresses;
        private String masterName;
    
        public int getTimeout() {
            return timeout;
        }
    
        public void setTimeout(int timeout) {
            this.timeout = timeout;
        }
    
        public String getSchema() {
            return schema;
        }
    
        public void setSchema(String schema) {
            this.schema = schema;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public int getConnectionPoolSize() {
            return connectionPoolSize;
        }
    
        public void setConnectionPoolSize(int connectionPoolSize) {
            this.connectionPoolSize = connectionPoolSize;
        }
    
        public int getConnectionMinimumIdleSize() {
            return connectionMinimumIdleSize;
        }
    
        public void setConnectionMinimumIdleSize(int connectionMinimumIdleSize) {
            this.connectionMinimumIdleSize = connectionMinimumIdleSize;
        }
    
        public int getSlaveConnectionPoolSize() {
            return slaveConnectionPoolSize;
        }
    
        public void setSlaveConnectionPoolSize(int slaveConnectionPoolSize) {
            this.slaveConnectionPoolSize = slaveConnectionPoolSize;
        }
    
        public int getMasterConnectionPoolSize() {
            return masterConnectionPoolSize;
        }
    
        public void setMasterConnectionPoolSize(int masterConnectionPoolSize) {
            this.masterConnectionPoolSize = masterConnectionPoolSize;
        }
    
        public String[] getSentinelAddresses() {
            return sentinelAddresses;
        }
    
        public void setSentinelAddresses(String[] sentinelAddresses) {
            this.sentinelAddresses = sentinelAddresses;
        }
    
        public String getMasterName() {
            return masterName;
        }
    
        public void setMasterName(String masterName) {
            this.masterName = masterName;
        }
    
    
    
        /*测试时mock使用*/
        /*@Bean
        RedissonClient redissonSingle() {
            RedissonClient redissonClient = Mockito.mock(RedissonClient.class);
            return redissonClient;
        }*/
    
        /**
         * 单机模式
         */
        /*@Bean
        RedissonClient redissonSingle() {
            Config config = new Config();
            SingleServerConfig serverConfig = config.useSingleServer()
                    .setAddress(address)
                    .setTimeout(timeout)
                    .setConnectionPoolSize(connectionPoolSize)
                    .setConnectionMinimumIdleSize(getConnectionMinimumIdleSize());
            if (StringUtils.isNotBlank(password)) {
                serverConfig.setPassword(password);
            }
            return Redisson.create(config);
        }*/
    
        /**
         * 哨兵模式
         * @return
         */
        @Bean
        RedissonClient redissonSentinel() {
            Config config = new Config();
    
            // 拼接协议
            String[] sentinelAddressesWithSchema = new String[sentinelAddresses.length];
            for (int i = 0; i < sentinelAddresses.length; i++) {
                sentinelAddressesWithSchema[i] = schema + sentinelAddresses[i];
            }
            
            SentinelServersConfig serversConfig = config.useSentinelServers()
                    .setMasterName(masterName)
                    .addSentinelAddress(sentinelAddressesWithSchema)
                    .setMasterConnectionPoolSize(masterConnectionPoolSize)
                    .setSlaveConnectionPoolSize(slaveConnectionPoolSize)
                    .setMasterConnectionMinimumIdleSize(getConnectionMinimumIdleSize())
                    .setSlaveConnectionMinimumIdleSize(getConnectionMinimumIdleSize());
    
            if (StringUtils.isNotBlank(password)) {
                serversConfig.setPassword(password);
            }
            return Redisson.create(config);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Redisson连接redis单机和哨兵模式

          本文链接:https://www.haomeiwen.com/subject/hyjjyltx.html