4 changed files with 134 additions and 0 deletions
			
			
		@ -0,0 +1,56 @@ | 
				
			|||
package com.epmet.commons.tools.config; | 
				
			|||
 | 
				
			|||
import com.epmet.commons.tools.constant.NumConstant; | 
				
			|||
import com.epmet.commons.tools.constant.StrConstant; | 
				
			|||
import lombok.extern.slf4j.Slf4j; | 
				
			|||
import org.apache.commons.lang3.StringUtils; | 
				
			|||
import org.redisson.Redisson; | 
				
			|||
import org.redisson.api.RedissonClient; | 
				
			|||
import org.redisson.codec.JsonJacksonCodec; | 
				
			|||
import org.redisson.config.Config; | 
				
			|||
import org.springframework.beans.factory.annotation.Value; | 
				
			|||
import org.springframework.context.annotation.Bean; | 
				
			|||
import org.springframework.context.annotation.Configuration; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * redisson 配置类 | 
				
			|||
 */ | 
				
			|||
@Slf4j | 
				
			|||
@Configuration | 
				
			|||
public class RedissonConfig { | 
				
			|||
 | 
				
			|||
    @Value("${spring.redis.host}") | 
				
			|||
    private String host; | 
				
			|||
    @Value("${spring.redis.port}") | 
				
			|||
    private String port; | 
				
			|||
    @Value("${spring.redis.password}") | 
				
			|||
    private String password; | 
				
			|||
 | 
				
			|||
 | 
				
			|||
    @Bean | 
				
			|||
    public RedissonClient getRedisson() { | 
				
			|||
        if (StringUtils.isBlank(host)) { | 
				
			|||
            log.warn("getRedisson redis param is null,don't need to init redissonClient"); | 
				
			|||
            return null; | 
				
			|||
        } | 
				
			|||
 | 
				
			|||
        try { | 
				
			|||
            Config config = new Config(); | 
				
			|||
            config.setCodec(new JsonJacksonCodec()); | 
				
			|||
            config.setThreads(NumConstant.FOUR); | 
				
			|||
            config.setNettyThreads(NumConstant.FOUR); | 
				
			|||
            //redis://ip:port
 | 
				
			|||
            //redis的部署方式有单节点部署、主从方式部署、哨兵方式部署、集群方式部署
 | 
				
			|||
            config.useSingleServer().setAddress("redis://".concat(host).concat(StrConstant.COLON).concat(port)); | 
				
			|||
            config.useSingleServer().setPassword(password); | 
				
			|||
            config.useSingleServer().setConnectTimeout(NumConstant.ONE_THOUSAND * NumConstant.FIVE); | 
				
			|||
            config.useSingleServer().setDatabase(NumConstant.TEN); | 
				
			|||
            return Redisson.create(config); | 
				
			|||
        } catch (Exception e) { | 
				
			|||
            log.error("初始化redisson失败", e); | 
				
			|||
            return null; | 
				
			|||
        } | 
				
			|||
 | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
} | 
				
			|||
@ -0,0 +1,61 @@ | 
				
			|||
package com.epmet.stats.test; | 
				
			|||
 | 
				
			|||
import com.epmet.DataStatsApplication; | 
				
			|||
import lombok.extern.slf4j.Slf4j; | 
				
			|||
import org.junit.Test; | 
				
			|||
import org.junit.runner.RunWith; | 
				
			|||
import org.redisson.api.RLock; | 
				
			|||
import org.redisson.api.RedissonClient; | 
				
			|||
import org.springframework.beans.factory.annotation.Autowired; | 
				
			|||
import org.springframework.boot.test.context.SpringBootTest; | 
				
			|||
import org.springframework.test.context.junit4.SpringRunner; | 
				
			|||
 | 
				
			|||
import java.util.concurrent.TimeUnit; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * desc:redisson测试类 | 
				
			|||
 */ | 
				
			|||
@Slf4j | 
				
			|||
@RunWith(value = SpringRunner.class) | 
				
			|||
@SpringBootTest(classes = {DataStatsApplication.class}) | 
				
			|||
public class RedissonTest { | 
				
			|||
 | 
				
			|||
    @Autowired | 
				
			|||
    private RedissonClient redissonClient; | 
				
			|||
 | 
				
			|||
    @Test | 
				
			|||
    public void lockTest() { | 
				
			|||
        //获取一个名为 lockName 的锁实例
 | 
				
			|||
        RLock lock = redissonClient.getLock("lockName"); | 
				
			|||
        try { | 
				
			|||
            // 尝试加锁(推荐使用)
 | 
				
			|||
            // 参数1 等待时长 waitTime:5 等待时长是5秒 如果5秒内还获取不到锁 则返回false,
 | 
				
			|||
            // 参数2 持有时长 leaseTime:5 持有锁时长超过5秒 就释放锁 此时如果继续lock.unlock()会抛出异常
 | 
				
			|||
            // 参数3 时间单位
 | 
				
			|||
            boolean bs = lock.tryLock(5, 6, TimeUnit.SECONDS); | 
				
			|||
 | 
				
			|||
            //如果获取不到锁 会一直阻塞到着 直至获取到锁 不推荐使用
 | 
				
			|||
            //lock.lock();
 | 
				
			|||
 | 
				
			|||
            //异步方式
 | 
				
			|||
        /*    RFuture<Void> future = lock.lockAsync(); | 
				
			|||
            if (future.isSuccess()){ | 
				
			|||
                //todo something
 | 
				
			|||
            }*/ | 
				
			|||
            if (bs) { | 
				
			|||
                // 业务代码
 | 
				
			|||
                System.out.println("进入业务代码: " + 123); | 
				
			|||
            } else { | 
				
			|||
                Thread.sleep(300); | 
				
			|||
            } | 
				
			|||
        } catch (Exception e) { | 
				
			|||
            log.error("lockTest exception", e); | 
				
			|||
        } finally { | 
				
			|||
            //判断是否是当前线程 持有锁
 | 
				
			|||
            if (lock.isHeldByCurrentThread()) { | 
				
			|||
                lock.unlock(); | 
				
			|||
            } | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
} | 
				
			|||
					Loading…
					
					
				
		Reference in new issue