基于Redisson+SpringBoot的Redission分布式锁

原文:https://blog.csdn.net/sunct/article/details/80178197

 

 

定义分布式锁接口

package com.redis.lock.redisson_springboot;
 
import java.util.concurrent.TimeUnit;
 
public interface DistributedLocker {
 
    void lock(String lockKey);
 
    void unlock(String lockKey);
 
    void lock(String lockKey, int timeout);
    
    void lock(String lockKey, TimeUnit unit ,int timeout);
}

 

基于Redisson的分布式锁实现

package com.redis.lock.redisson_springboot;
 
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
 
import java.util.concurrent.TimeUnit;
 
public class RedissonDistributedLocker implements DistributedLocker {
    
    private RedissonClient redissonClient;
 
    @Override
    public void lock(String lockKey) {
        RLock lock = redissonClient.getLock(lockKey);
        lock.lock();
    }
 
    @Override
    public void unlock(String lockKey) {
        RLock lock = redissonClient.getLock(lockKey);
        lock.unlock();
    }
 
    @Override
    public void lock(String lockKey, int leaseTime) {
        RLock lock = redissonClient.getLock(lockKey);
        lock.lock(leaseTime, TimeUnit.SECONDS);
    }
    
    @Override
    public void lock(String lockKey, TimeUnit unit ,int timeout) {
        RLock lock = redissonClient.getLock(lockKey);
        lock.lock(timeout, unit);
    }
 
    public void setRedissonClient(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }
}

redis分布式锁帮助类

package com.redis.lock.redisson_springboot;
 
import java.util.concurrent.TimeUnit;
 
 
/**
 * redis分布式锁帮助类
 *
 */
public class RedissLockUtil {
    private static DistributedLocker redissLock;
    
    public static void setLocker(DistributedLocker locker) {
        redissLock = locker;
    }
    
    public static void lock(String lockKey) {
        redissLock.lock(lockKey);
    }
 
    public static void unlock(String lockKey) {
        redissLock.unlock(lockKey);
    }
 
    /**
     * 带超时的锁
     * @param lockKey
     * @param timeout 超时时间   单位:秒
     */
    public static void lock(String lockKey, int timeout) {
        redissLock.lock(lockKey, timeout);
    }
    
    /**
     * 带超时的锁
     * @param lockKey
     * @param unit 时间单位
     * @param timeout 超时时间
     */
    public static void lock(String lockKey, TimeUnit unit ,int timeout) {
        redissLock.lock(lockKey, unit, timeout);
    }
}

SpringBoot的自动配置类

package com.redis.lock.redisson_springboot;
 
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.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
 
@Configuration
@ConditionalOnClass(Config.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguration {
 
    @Autowired
    private RedissonProperties redssionProperties;
 
    /**
     * 哨兵模式自动装配
     * @return
     */
    @Bean
    @ConditionalOnProperty(name="redisson.master-name")
    RedissonClient redissonSentinel() {
        Config config = new Config();
        SentinelServersConfig serverConfig = config.useSentinelServers().addSentinelAddress(redssionProperties.getSentinelAddresses())
                .setMasterName(redssionProperties.getMasterName())
                .setTimeout(redssionProperties.getTimeout())
                .setMasterConnectionPoolSize(redssionProperties.getMasterConnectionPoolSize())
                .setSlaveConnectionPoolSize(redssionProperties.getSlaveConnectionPoolSize());
        
        if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
            serverConfig.setPassword(redssionProperties.getPassword());
        }
        return Redisson.create(config);
    }
 
    /**
     * 单机模式自动装配
     * @return
     */
    @Bean
    @ConditionalOnProperty(name="redisson.address")
    RedissonClient redissonSingle() {
        Config config = new Config();
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress(redssionProperties.getAddress())
                .setTimeout(redssionProperties.getTimeout())
                .setConnectionPoolSize(redssionProperties.getConnectionPoolSize())
                .setConnectionMinimumIdleSize(redssionProperties.getConnectionMinimumIdleSize());
        
        if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
            serverConfig.setPassword(redssionProperties.getPassword());
        }
 
        return Redisson.create(config);
    }
 
    /**
     * 装配locker类,并将实例注入到RedissLockUtil中
     * @return
     */
    @Bean
    DistributedLocker distributedLocker(RedissonClient redissonSingle) {
        RedissonDistributedLocker locker = new RedissonDistributedLocker();
        locker.setRedissonClient(redissonSingle);
        RedissLockUtil.setLocker(locker);
        return locker;
    }
 
}

Redisson配置类

package com.redis.lock.redisson_springboot;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
 
@ConfigurationProperties(prefix = "redisson")
public class RedissonProperties {
 
    private int timeout = 3000;
 
    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 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.split(",");
    }
 
    public String getMasterName() {
        return masterName;
    }
 
    public void setMasterName(String masterName) {
        this.masterName = masterName;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
    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;
    }
}

属性文件配置项application.properties

# redisson lock
#1、单机模式
redisson.address=redis://10.18.75.115:6379
#redisson.password=
 
 
#2、哨兵模式
redisson.master-name=mymaster
#redisson.password=xxxx
redisson.sentinel-addresses=10.47.91.83:26379,10.47.91.83:26380,10.47.91.83:26381

 

 

定义分布式锁接口

  1.  
    package com.redis.lock.redisson_springboot;
  2.  
     
  3.  
    import java.util.concurrent.TimeUnit;
  4.  
     
  5.  
    public interface DistributedLocker {
  6.  
     
  7.  
    void lock(String lockKey);
  8.  
     
  9.  
    void unlock(String lockKey);
  10.  
     
  11.  
    void lock(String lockKey, int timeout);
  12.  
     
  13.  
    void lock(String lockKey, TimeUnit unit ,int timeout);
  14.  
    }

基于Redisson的分布式锁实现

  1.  
    package com.redis.lock.redisson_springboot;
  2.  
     
  3.  
    import org.redisson.api.RLock;
  4.  
    import org.redisson.api.RedissonClient;
  5.  
     
  6.  
    import java.util.concurrent.TimeUnit;
  7.  
     
  8.  
    public class RedissonDistributedLocker implements DistributedLocker {
  9.  
     
  10.  
    private RedissonClient redissonClient;
  11.  
     
  12.  
    @Override
  13.  
    public void lock(String lockKey) {
  14.  
    RLock lock = redissonClient.getLock(lockKey);
  15.  
    lock.lock();
  16.  
    }
  17.  
     
  18.  
    @Override
  19.  
    public void unlock(String lockKey) {
  20.  
    RLock lock = redissonClient.getLock(lockKey);
  21.  
    lock.unlock();
  22.  
    }
  23.  
     
  24.  
    @Override
  25.  
    public void lock(String lockKey, int leaseTime) {
  26.  
    RLock lock = redissonClient.getLock(lockKey);
  27.  
    lock.lock(leaseTime, TimeUnit.SECONDS);
  28.  
    }
  29.  
     
  30.  
    @Override
  31.  
    public void lock(String lockKey, TimeUnit unit ,int timeout) {
  32.  
    RLock lock = redissonClient.getLock(lockKey);
  33.  
    lock.lock(timeout, unit);
  34.  
    }
  35.  
     
  36.  
    public void setRedissonClient(RedissonClient redissonClient) {
  37.  
    this.redissonClient = redissonClient;
  38.  
    }
  39.  
    }

 

redis分布式锁帮助类

  1.  
    package com.redis.lock.redisson_springboot;
  2.  
     
  3.  
    import java.util.concurrent.TimeUnit;
  4.  
     
  5.  
     
  6.  
    /**
  7.  
    * redis分布式锁帮助类
  8.  
    *
  9.  
    */
  10.  
    public class RedissLockUtil {
  11.  
    private static DistributedLocker redissLock;
  12.  
     
  13.  
    public static void setLocker(DistributedLocker locker) {
  14.  
    redissLock = locker;
  15.  
    }
  16.  
     
  17.  
    public static void lock(String lockKey) {
  18.  
    redissLock.lock(lockKey);
  19.  
    }
  20.  
     
  21.  
    public static void unlock(String lockKey) {
  22.  
    redissLock.unlock(lockKey);
  23.  
    }
  24.  
     
  25.  
    /**
  26.  
    * 带超时的锁
  27.  
    * @param lockKey
  28.  
    * @param timeout 超时时间 单位:秒
  29.  
    */
  30.  
    public static void lock(String lockKey, int timeout) {
  31.  
    redissLock.lock(lockKey, timeout);
  32.  
    }
  33.  
     
  34.  
    /**
  35.  
    * 带超时的锁
  36.  
    * @param lockKey
  37.  
    * @param unit 时间单位
  38.  
    * @param timeout 超时时间
  39.  
    */
  40.  
    public static void lock(String lockKey, TimeUnit unit ,int timeout) {
  41.  
    redissLock.lock(lockKey, unit, timeout);
  42.  
    }
  43.  
    }

SpringBoot的自动配置类

  1.  
    package com.redis.lock.redisson_springboot;
  2.  
     
  3.  
    import org.apache.commons.lang3.StringUtils;
  4.  
    import org.redisson.Redisson;
  5.  
    import org.redisson.api.RedissonClient;
  6.  
    import org.redisson.config.Config;
  7.  
    import org.redisson.config.SentinelServersConfig;
  8.  
    import org.redisson.config.SingleServerConfig;
  9.  
    import org.springframework.beans.factory.annotation.Autowired;
  10.  
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  11.  
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  12.  
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
  13.  
    import org.springframework.context.annotation.Bean;
  14.  
    import org.springframework.context.annotation.Configuration;
  15.  
     
  16.  
     
  17.  
    @Configuration
  18.  
    @ConditionalOnClass(Config.class)
  19.  
    @EnableConfigurationProperties(RedissonProperties.class)
  20.  
    public class RedissonAutoConfiguration {
  21.  
     
  22.  
    @Autowired
  23.  
    private RedissonProperties redssionProperties;
  24.  
     
  25.  
    /**
  26.  
    * 哨兵模式自动装配
  27.  
    * @return
  28.  
    */
  29.  
    @Bean
  30.  
    @ConditionalOnProperty(name="redisson.master-name")
  31.  
    RedissonClient redissonSentinel() {
  32.  
    Config config = new Config();
  33.  
    SentinelServersConfig serverConfig = config.useSentinelServers().addSentinelAddress(redssionProperties.getSentinelAddresses())
  34.  
    .setMasterName(redssionProperties.getMasterName())
  35.  
    .setTimeout(redssionProperties.getTimeout())
  36.  
    .setMasterConnectionPoolSize(redssionProperties.getMasterConnectionPoolSize())
  37.  
    .setSlaveConnectionPoolSize(redssionProperties.getSlaveConnectionPoolSize());
  38.  
     
  39.  
    if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
  40.  
    serverConfig.setPassword(redssionProperties.getPassword());
  41.  
    }
  42.  
    return Redisson.create(config);
  43.  
    }
  44.  
     
  45.  
    /**
  46.  
    * 单机模式自动装配
  47.  
    * @return
  48.  
    */
  49.  
    @Bean
  50.  
    @ConditionalOnProperty(name="redisson.address")
  51.  
    RedissonClient redissonSingle() {
  52.  
    Config config = new Config();
  53.  
    SingleServerConfig serverConfig = config.useSingleServer()
  54.  
    .setAddress(redssionProperties.getAddress())
  55.  
    .setTimeout(redssionProperties.getTimeout())
  56.  
    .setConnectionPoolSize(redssionProperties.getConnectionPoolSize())
  57.  
    .setConnectionMinimumIdleSize(redssionProperties.getConnectionMinimumIdleSize());
  58.  
     
  59.  
    if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
  60.  
    serverConfig.setPassword(redssionProperties.getPassword());
  61.  
    }
  62.  
     
  63.  
    return Redisson.create(config);
  64.  
    }
  65.  
     
  66.  
    /**
  67.  
    * 装配locker类,并将实例注入到RedissLockUtil中
  68.  
    * @return
  69.  
    */
  70.  
    @Bean
  71.  
    DistributedLocker distributedLocker(RedissonClient redissonSingle) {
  72.  
    RedissonDistributedLocker locker = new RedissonDistributedLocker();
  73.  
    locker.setRedissonClient(redissonSingle);
  74.  
    RedissLockUtil.setLocker(locker);
  75.  
    return locker;
  76.  
    }
  77.  
     
  78.  
    }

Redisson配置类

  1.  
    package com.redis.lock.redisson_springboot;
  2.  
     
  3.  
    import org.springframework.boot.context.properties.ConfigurationProperties;
  4.  
     
  5.  
    @ConfigurationProperties(prefix = "redisson")
  6.  
    public class RedissonProperties {
  7.  
     
  8.  
    private int timeout = 3000;
  9.  
     
  10.  
    private String address;
  11.  
     
  12.  
    private String password;
  13.  
     
  14.  
    private int connectionPoolSize = 64;
  15.  
     
  16.  
    private int connectionMinimumIdleSize=10;
  17.  
     
  18.  
    private int slaveConnectionPoolSize = 250;
  19.  
     
  20.  
    private int masterConnectionPoolSize = 250;
  21.  
     
  22.  
    private String[] sentinelAddresses;
  23.  
     
  24.  
    private String masterName;
  25.  
     
  26.  
    public int getTimeout() {
  27.  
    return timeout;
  28.  
    }
  29.  
     
  30.  
    public void setTimeout(int timeout) {
  31.  
    this.timeout = timeout;
  32.  
    }
  33.  
     
  34.  
    public int getSlaveConnectionPoolSize() {
  35.  
    return slaveConnectionPoolSize;
  36.  
    }
  37.  
     
  38.  
    public void setSlaveConnectionPoolSize(int slaveConnectionPoolSize) {
  39.  
    this.slaveConnectionPoolSize = slaveConnectionPoolSize;
  40.  
    }
  41.  
     
  42.  
    public int getMasterConnectionPoolSize() {
  43.  
    return masterConnectionPoolSize;
  44.  
    }
  45.  
     
  46.  
    public void setMasterConnectionPoolSize(int masterConnectionPoolSize) {
  47.  
    this.masterConnectionPoolSize = masterConnectionPoolSize;
  48.  
    }
  49.  
     
  50.  
    public String[] getSentinelAddresses() {
  51.  
    return sentinelAddresses;
  52.  
    }
  53.  
     
  54.  
    public void setSentinelAddresses(String sentinelAddresses) {
  55.  
    this.sentinelAddresses = sentinelAddresses.split(",");
  56.  
    }
  57.  
     
  58.  
    public String getMasterName() {
  59.  
    return masterName;
  60.  
    }
  61.  
     
  62.  
    public void setMasterName(String masterName) {
  63.  
    this.masterName = masterName;
  64.  
    }
  65.  
     
  66.  
    public String getPassword() {
  67.  
    return password;
  68.  
    }
  69.  
     
  70.  
    public void setPassword(String password) {
  71.  
    this.password = password;
  72.  
    }
  73.  
     
  74.  
    public String getAddress() {
  75.  
    return address;
  76.  
    }
  77.  
     
  78.  
    public void setAddress(String address) {
  79.  
    this.address = address;
  80.  
    }
  81.  
     
  82.  
    public int getConnectionPoolSize() {
  83.  
    return connectionPoolSize;
  84.  
    }
  85.  
     
  86.  
    public void setConnectionPoolSize(int connectionPoolSize) {
  87.  
    this.connectionPoolSize = connectionPoolSize;
  88.  
    }
  89.  
     
  90.  
    public int getConnectionMinimumIdleSize() {
  91.  
    return connectionMinimumIdleSize;
  92.  
    }
  93.  
     
  94.  
    public void setConnectionMinimumIdleSize(int connectionMinimumIdleSize) {
  95.  
    this.connectionMinimumIdleSize = connectionMinimumIdleSize;
  96.  
    }
  97.  
    }

属性文件配置项application.properties

  1.  
    # redisson lock
  2.  
    #1、单机模式
  3.  
    redisson.address=redis://10.18.75.115:6379
  4.  
    #redisson.password=
  5.  
     
  6.  
     
  7.  
    #2、哨兵模式
  8.  
    redisson.master-name=mymaster
  9.  
    #redisson.password=xxxx
  10.  
    redisson.sentinel-addresses=10.47.91.83:26379,10.47.91.83:26380,10.47.91.83:26381
  11.  

posted @ 2019-08-08 17:54  这个名字想了很久~  阅读(11444)  评论(0编辑  收藏  举报