天天看點

說一說spring boot優雅內建redisson

拔劍起蒿萊👥👥👥👥

今天簡單說一下內建及注意事項。

redisson支援redis環境,單機、叢集、哨兵、雲等。

這裡就講一下叢集模式需要注意的地方,redisson啟動會檢測master/slave節點是否正常,一般來說3分片3主3從是沒有什麼問題的,但是如果測試環境1分片1主1從或者3主都是啟動不了的。

除了環境需要注意,還有注意相容有無密碼的情況。

一般情況下,生産環境都是有密碼的。有密碼的話,建議手動注入redisson配置,不用spring boot來幫你內建,因為可能spring boot識别不了密碼。

@Configuration
public class RedissonConfiguration {

    @Value("${spring.redis.cluster.nodes}")
    private String node;

    @Value("${spring.redis.password:}")
    private String password;


    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        String[] nodes = node.split(",");
        ClusterServersConfig clusterServersConfig = config.useClusterServers();
        for (String nodeAddress : nodes) {
            clusterServersConfig.addNodeAddress(prefixAddress(nodeAddress));
        }
        if (StringUtils.isNotBlank(password)) {
            clusterServersConfig.setPassword(password);
        }
        return Redisson.create(config);
    }

    private String prefixAddress(String) {
        if (!StringUtils.isBlank(address) && !address.startsWith("redis")) {
            return "redis://" + address;
        }
        return      

上面可以根據自己實際情況調優一些配置。​​見官網​​

當然除了密碼需要注意,還有一點就是是否有ssl,目前所在company是用的亞馬遜雲,會有ssl

spring boot 相容 redis 可以在yaml配置裡天際: Ssl:true,但對于redisson來說添加字首就可以啦:

rediss:// + ip:端口或者域名

具體yaml配置如下:

spring:
  redis:
    cluster:
      nodes: rediss://clustercfg.xxx
    password: 'xxx'
    timeout: 30000
    Ssl: true
    lettuce:
      pool:
        max-idle: 100      

願君學長松 慎勿作桃李🏆🏆🏆🏆

既然注意點講完了,那麼在講下怎麼使用吧

利用鎖的互斥政策,想知道有鎖的那些政策,見上一篇​​文章​​。

一開始這樣的

@Scheduled(cron = "${xxx:0 0 */2 * * ?}")
public void createProcess() {
    RLock lock = redisson.getLock(key);
    try {
        if (lock.tryLock()) {
           // 執行運作程式
        } else {
            log.info("createProcess 擷取鎖失敗");
        }
    } catch (Exception e) {
        log.error("xxx", e);
    } finally {
        // 是否有鎖 && 是否目前線程
        if (lock != null && lock.isLocked() && lock.isHeldByCurrentThread()) {
            lock.unlock();
        }
    }
}      

咋一看是沒有什麼問題的,但是本次重構的定時任務比較多,是以會涉及到很多try catch相同的代碼。

解決重複代碼方式之一就是封裝,在就是注解切面,想到注解方式更加靈活

于是

/**
 * Redisson 同步鎖
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RedissonSyncLock {

    String pref();

    /**
     * 鎖字尾,一般字首根據業務來定,字尾是具體的場景
     */
    String keyEL();

    /**
     * 等待時長 【需要區分是互斥還是阻塞,互斥預設0就可以】
     */
    int waitSec() default 0;

}      

需要一個切面

@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class RedissonSyncLockAspect {
    private final Redisson redisson;

    @Around(value = "@annotation(xxx.RedissonSyncLock)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        RedissonSyncLock redissonSyncLock = signature.getMethod().getAnnotation(RedissonSyncLock.class);
        Object[] args = joinPoint.getArgs();
        String key = SpelUtil.parseSpel(signature.getMethod(), args, redissonSyncLock.keyEL());
        RLock lock = null;
        try {
            if (StringUtils.isNotBlank(key) && !StringUtils.equals(key, "null")
                lock = redisson.getLock(redissonSyncLock.pref().concat(key));
                if (lock.tryLock(redissonSyncLock.waitSec(), TimeUnit.SECONDS)) {
                    return joinPoint.proceed();
                }
            }
            log.info("RedissonSyncLockAspect 上鎖失敗 {}", key);
        } finally {
            if (lock != null && lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }
}      
@RedissonSyncLock(pref = KeyConstant.xxx, keyEL = "#bean.accountNo")
private void xxx(Bean bean){
     // 程式執行      

繼續閱讀