ホームページ  >  記事  >  Java  >  Spring Boot が Redisson を統合する方法

Spring Boot が Redisson を統合する方法

PHPz
PHPz転載
2023-05-14 19:46:191700ブラウズ

統合と注意事項

redisson は、redis 環境、スタンドアロン、クラスター、センチネル、クラウドなどをサポートします。

ここではクラスタモードについて説明します注意する必要があるのは、redissonを起動するとマスター/スレーブノードが正常かどうかの検出が行われることです。一般的には問題ありません。ただし、テスト環境が 1 シャード、1 マスター、1 スレーブ、または 3 マスターの場合は起動できません。

環境に加えて、パスワードの有無にも注意する必要があります。

redisson 構成を手動で挿入する

通常の状況では、運用環境 にはパスワード があります。パスワードがある場合は、Spring Boot がパスワードを認識できない可能性があるため、統合を容易にするために Spring Boot を使用せずに、手動で redison 構成を挿入することをお勧めします。

@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 address) {
        if (!StringUtils.isBlank(address) && !address.startsWith("redis")) {
            return "redis://" + address;
        }
        return address;
    }
}

実際の状況に基づいて一部の構成を調整できます。

もちろんパスワードだけでなく、 が ssl を持っているかどうかもポイントです。現在の会社は Amazon Cloud を使用しているため、ssl

が存在します。spring boot は互換性があります。 redis を使用します。これは、Tianji の yaml 設定にあります: Ssl: true ですが、redisson の場合は、プレフィックスを追加するだけです:

rediss:// ip: ポートまたはドメイン名

特定の yaml configuration

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();
        }
    }
}

には一見したところ問題はありませんが、この再構築には多くのスケジュールされたタスクがあるため、同じコードをキャッチしてみてください。

アノテーション方法

重複コードを解決する方法の 1 つは カプセル化 であり、これは アノテーションの側面 です。アノテーション方法の方が柔軟だと思います

So

/**
 * 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){
     // 程序执行
}

実際に使用するとさらに便利です。

以上がSpring Boot が Redisson を統合する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。