官方文档:https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95
1、引用redisson的pom
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.5.0</version> </dependency>
2、定义Lock的接口定义类
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); }
3、Lock接口实现类
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; } }
4、redisson属性装配类
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; } }
5、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; import com.longge.lock.DistributedLocker; import com.longge.lock.RedissonDistributedLocker; import com.longge.lock.RedissonProperties; import com.longge.utils.RedissLockUtil; @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 redissonClient) { DistributedLocker locker = new RedissonDistributedLocker(); locker.setRedissonClient(redissonClient); RedissLockUtil.setLocker(locker); return locker; } }
6、Lock帮助类
import java.util.concurrent.TimeUnit; import com.longge.lock.DistributedLocker; /** * redis分布式锁帮助类 * @author yangzhilong * */ 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); } }
属性文件实例:
1、单机模式
# redisson lock redisson.address=redis://10.18.75.115:6379 redisson.password=
这里如果不加redis://前缀会报URI构建错误,
Caused by: java.net.URISyntaxException: Illegal character in scheme name at index 0
其次,在redis进行连接的时候如果不对密码进行空判断,会出现AUTH校验失败的情况。
Caused by: org.redisson.client.RedisException: ERR Client sent AUTH, but no password is set. channel
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
更多的配置信息可以去官网查看
时间: 2024-10-06 23:59:45