美文网首页
Java中间件 - Redisson(中) - SpringBo

Java中间件 - Redisson(中) - SpringBo

作者: ElliotG | 来源:发表于2020-07-23 09:36 被阅读0次

1. 添加依赖

<!-- redisson -->
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.13.2</version>
</dependency>

 

2. 配置文件

  • 添加RedissonConfig
@Configuration
public class RedissonConfig {
    // 读取环境变量的实例env
    @Autowired
    private Environment env;

    /**
     * 自定义注入配置操作Redisson客户端实例
     * @return
     */
    @Bean
    public RedissonClient config() {
        // 创建配置实例
        Config config = new Config();

        // 传输模式既可以设置为EPOLL,也可以设置为NIO等
        config.setTransportMode(TransportMode.NIO);

        // 设置服务节点部署模式: 集群、单一节点/主从模式/哨兵模式
        // config.useClusterServers().addNodeAddress(env.getProperty("redisson.host.config"), env.getProperty("redisson.host.config"));
        config.useSingleServer().setAddress(env.getProperty("redisson.host.config")).setKeepAlive(true);

        return Redisson.create(config);
    }
}
  • 在application.properties中添加配置
# redisson配置
redisson.host.config=redis://127.0.0.1:6379

 

3. 编写Redisson通用操作Service

@Service
public class RedissonCommonService {
    @Autowired
    private RedissonClient redissonClient;

    public void getRedissonClient() throws IOException {
        Config config = redissonClient.getConfig();
        System.out.println(config.toJSON().toString());
    }

    /**`
     * 获取字符串对象
     *
     * @param objectName
     * @return
     */
    public <T> RBucket<T> getRBucket(String objectName) {
        RBucket<T> bucket = redissonClient.getBucket(objectName);
        return bucket;
    }

    /**
     * 获取Map对象
     *
     * @param objectName
     * @return
     */
    public <K, V> RMap<K, V> getRMap(String objectName) {
        RMap<K, V> map = redissonClient.getMap(objectName);
        return map;
    }

    /**
     * 获取有序集合
     *
     * @param objectName
     * @return
     */
    public <V> RSortedSet<V> getRSortedSet(String objectName) {
        RSortedSet<V> sortedSet = redissonClient.getSortedSet(objectName);
        return sortedSet;
    }

    /**
     * 获取集合
     *
     * @param objectName
     * @return
     */
    public <V> RSet<V> getRSet(String objectName) {
        RSet<V> rSet = redissonClient.getSet(objectName);
        return rSet;
    }

    /**
     * 获取列表
     *
     * @param objectName
     * @return
     */
    public <V> RList<V> getRList(String objectName) {
        RList<V> rList = redissonClient.getList(objectName);
        return rList;
    }

    /**
     * 获取队列
     *
     * @param objectName
     * @return
     */
    public <V> RQueue<V> getRQueue(String objectName) {
        RQueue<V> rQueue = redissonClient.getQueue(objectName);
        return rQueue;
    }

    /**
     * 获取双端队列
     *
     * @param objectName
     * @return
     */
    public <V> RDeque<V> getRDeque(String objectName) {
        RDeque<V> rDeque = redissonClient.getDeque(objectName);
        return rDeque;
    }


    /**
     * 获取锁
     *
     * @param objectName
     * @return
     */
    public RLock getRLock(String objectName) {
        RLock rLock = redissonClient.getLock(objectName);
        return rLock;
    }

    /**
     * 获取读取锁
     *
     * @param objectName
     * @return
     */
    public RReadWriteLock getRWLock(String objectName) {
        RReadWriteLock rwlock = redissonClient.getReadWriteLock(objectName);
        return rwlock;
    }

    /**
     * 获取原子数
     *
     * @param objectName
     * @return
     */
    public RAtomicLong getRAtomicLong(String objectName) {
        RAtomicLong rAtomicLong = redissonClient.getAtomicLong(objectName);
        return rAtomicLong;
    }

    /**
     * 获取记数锁
     *
     * @param objectName
     * @return
     */
    public RCountDownLatch getRCountDownLatch(String objectName) {
        RCountDownLatch rCountDownLatch = redissonClient.getCountDownLatch(objectName);
        return rCountDownLatch;
    }

    /**
     * 获取消息的Topic
     *
     * @param objectName
     * @return
     */
    public RTopic getRTopic(String objectName) {
        return redissonClient.getTopic(objectName);
    }
}

 

4. 编写测试Controller

@Slf4j
@Api(tags = "Redisson测试接口")
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RedissonCommonService redissonCommonService;

    @ApiOperation(value = "Test RLock")
    @GetMapping(value = "/test-rlock")
    public void testLock(String param) {
        RLock lock = redissonCommonService.getRLock(param);

        try {
            boolean bs = lock.tryLock(5, 6, TimeUnit.SECONDS);
            if (bs) {
                // 业务代码
                log.info("进入业务代码: " + param);

                lock.unlock();
            } else {
                Thread.sleep(300);
            }
        } catch (Exception e) {
            log.error("", e);
            lock.unlock();
        }
    }
}
  • swagger运行界面


    swagger运行界面
  • 控制台运行结果


    运行结果

相关文章

网友评论

      本文标题:Java中间件 - Redisson(中) - SpringBo

      本文链接:https://www.haomeiwen.com/subject/yldclktx.html