• Redisson 整合Spring测试分布式锁


    一、官网地址
     
     
    二、整合配置
     
    redisson 有些版本配置可能出现bug,开始用的3.13.6版本,哨兵配置一直报错
    redisson 有些版本配置可能出现bug,开始用的3.13.6版本,哨兵配置一直报错
    <dependency>
      <groupId>org.redisson</groupId>
      <artifactId>redisson</artifactId>
      <version>3.12.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
      <exclusions>
        <exclusion>
          <groupId>io.lettuce</groupId>
          <artifactId>lettuce-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
    </dependency>
    View Code

    2)spring中的Bean配置

    mport org.redisson.Redisson;
    import org.redisson.api.RedissonClient;
    import org.redisson.config.Config;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    
    import java.io.IOException;
    
    
    /**
    * @Classname RedssonConfig
    * @Description TODO
    * @Author Jack
    * Date 2020/9/16 20:47
    * Version 1.0
    */
    @Configuration
    public class RedissonConfig {
    
    
        @Bean(name = "redissonClient",destroyMethod = "shutdown")
        public RedissonClient redissonClient() throws IOException {
            Config config = new Config();
            config.useSentinelServers()
                    .setMasterName("mymaster")
                    //可以用"rediss://"来启用SSL连接
                    .addSentinelAddress("redis://192.168.112.131:26379" )
                    .addSentinelAddress("redis://192.168.112.131:26380")
            .addSentinelAddress("redis://192.168.112.131:26381");
    
    
            RedissonClient redisson = Redisson.create(config);
            return redisson;
        }
    
    }

    三、测试代码

    @Autowired
    @Qualifier("redissonClient")
    private RedissonClient redissonClient;
    
    @Test
    public void test5() throws InterruptedException {
        RLock lock = redissonClient.getLock("hello_redisson_lock");
        if(lock.tryLock( )) {
            System.out.println( "加锁成功");
    
    
        }
        lock.unlock();
    }
  • 相关阅读:
    ES6 => 箭头函数
    从零开始, 探索transition(vue-2.0)
    从零开始,零基础,一点一点探索vue-router(vue2.0)
    解决Vue请求 ‘No 'Access-Control-Allow-Origin' header is present on the requested resource’错误
    超详细,用canvas在微信小程序上画时钟教程
    钱兔
    天天飞燕
    小鱼
    键盘处理
    兼容iOS 10 资料整理笔记
  • 原文地址:https://www.cnblogs.com/lean-blog/p/14150147.html
Copyright © 2020-2023  润新知