Springboot使用Redisson配置

  • A+
所属分类:redis

1、单机配置

@Component@ConfigurationProperties(prefix = "spring.redis")public class RedisConfigProperties {    private String password;    private String database;    private int port;    private String host;    /**
     * @return the password
     */
    public String getPassword() {        return password;
    }    /**
     * @param password the password to set
     */
    public void setPassword(String password) {        this.password = password;
    }    /**
     * @return the database
     */
    public String getDatabase() {        return database;
    }    /**
     * @param database the database to set
     */
    public void setDatabase(String database) {        this.database = database;
    }    /**
     * @return the port
     */
    public int getPort() {        return port;
    }    /**
     * @param port the port to set
     */
    public void setPort(int port) {        this.port = port;
    }    /**
     * @return the host
     */
    public String getHost() {        return host;
    }    /**
     * @param host the host to set
     */
    public void setHost(String host) {        this.host = host;
    }
}
@Configurationpublic class RedissonConfig {    
    @Autowired
    private RedisConfigProperties redisConfigProperties;    //添加redisson的bean
    @Bean
    public Redisson redisson() {        //redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加
        String redissonAddr = "redis://" + redisConfigProperties.getHost()+":"+redisConfigProperties.getPort();        Config config = new Config();        SingleServerConfig singleConfig = config.useSingleServer().setAddress(redissonAddr).              //心跳检测,定时与redis连接,可以防止一段时间过后,与redis的连接断开
                setPingConnectionInterval(1000);
        singleConfig.setPassword(redisConfigProperties.getPassword());//设置密码
        return (Redisson) Redisson.create(config);
    }
}

2、集群配置

@Component@ConfigurationProperties(prefix = "spring.redis")public class RedisConfigProperties {    private String password;    private cluster cluster;    public static class cluster {        private List<String> nodes;        public List<String> getNodes() {            return nodes;
        }        public void setNodes(List<String> nodes) {            this.nodes = nodes;
        }
    }    public String getPassword() {        return password;
    }    public void setPassword(String password) {        this.password = password;
    }    public RedisConfigProperties.cluster getCluster() {        return cluster;
    }    public void setCluster(RedisConfigProperties.cluster cluster) {        this.cluster = cluster;
    }
}
@Configurationpublic class RedissonConfig {    @Autowired
    private RedisConfigProperties redisConfigProperties;    //添加redisson的bean
    @Bean
    public Redisson redisson() {        //redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加
        List<String> clusterNodes = new ArrayList<>();        for (int i = 0; i < redisConfigProperties.getCluster().getNodes().size(); i++) {
            clusterNodes.add("redis://" + redisConfigProperties.getCluster().getNodes().get(i));
        }        Config config = new Config();        ClusterServersConfig clusterServersConfig = config.useClusterServers()
                .addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]));        //心跳检测,定时与redis连接,可以防止一段时间过后,与redis的连接断开                
        clusterServersConfig.setPingConnectionInterval(1000);
        clusterServersConfig.setPassword(redisConfigProperties.getPassword());//设置密码
        return (Redisson) Redisson.create(config);
    }
}

3、使用方法

    public boolean test1() throws Exception {        String LOCK_STATUS = "lock_status";        RLock lock = null;        try {
            lock = redissonClient.getLock(LOCK_STATUS);            if (lock.tryLock()) {                //业务.....
            } else {                return false;
            }           
        } catch (Exception e) {
            logger.error("err",e);
        }finally {            if(null != lock && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }        return true;
    }

4、maven

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

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: