springboot 整合redisson
整合代码已经过测试
1、pom
<!-- redisson -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.5.7</version>
</dependency>
2、properties
# redis
spring.redis.host=
spring.redis.port=
spring.redis.password=
spring.redis.jedis.pool.max-active=500
spring.redis.jedis.pool.max-idle=1000
spring.redis.jedis.pool.max-wait=6000ms
spring.redis.jedis.pool.min-idle=4
3、添加redisson配置类、这里是单机模式
package com.example.common.config; import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
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 配置类
* Created on 2018/6/19
*/
@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(){ Config config = new Config();
config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password);
//添加主从配置
// config.useMasterSlaveServers().setMasterAddress("").setPassword("").addSlaveAddress(new String[]{"",""}); return Redisson.create(config);
} }
4、加入redisson 操作类(redissonService)
package com.example.common.base; import org.redisson.api.*;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.io.IOException; /**
* redisson操作类
*/
@Service("redissonService")
public class RedissonService { @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 <M> RTopic<M> getRTopic(String objectName) {
RTopic<M> rTopic = redissonClient.getTopic(objectName);
return rTopic;
}
}
5.、测试代码
package com.example.test; import com.example.common.base.RedissonService;
import org.redisson.api.RLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.concurrent.TimeUnit; @Controller
@RequestMapping("test")
public class TestService { private static final Logger log = LoggerFactory.getLogger(TestService.class); @Autowired
private RedissonService redissonService; @RequestMapping(value = "/test")
@ResponseBody
public void test(String recordId) { RLock lock = redissonService.getRLock(recordId);
try {
boolean bs = lock.tryLock(5, 6, TimeUnit.SECONDS);
if (bs) {
// 业务代码
log.info("进入业务代码: " + recordId); lock.unlock();
} else {
Thread.sleep(300);
}
} catch (Exception e) {
log.error("", e);
lock.unlock();
}
} }
springboot 整合redisson的更多相关文章
- springboot整合redisson分布式锁
一.通过maven引入redisson的jar包 <dependency> <groupId>org.redisson</groupId> <artifact ...
- Springboot基于Redisson实现Redis分布式可重入锁【案例到源码分析】
一.前言 我们在实现使用Redis实现分布式锁,最开始一般使用SET resource-name anystring NX EX max-lock-time进行加锁,使用Lua脚本保证原子性进行实现释 ...
- spring-boot整合mybatis(1)
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- springboot整合mq接收消息队列
继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...
- springboot整合mybaits注解开发
springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- SpringBoot整合Kafka和Storm
前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...
随机推荐
- STM32单片机串口中断+DMA使用(含CUBE配置)
最近又要重新用32做点东西,发现一两年没怎么碰的结果就是,曾经熟得不行的东西都变得极度陌生,这种重新学习记忆的过程过于痛苦,果然还是要留下一些记录给之后失忆的自己的. 1.STM32CUBE配置 1. ...
- mysql在linux下的安装与优化
mysql5.6 http://www.cnblogs.com/bookwed/p/5896619.html mysql5.7 http://blog.csdn.net/wb96a1007/artic ...
- winform只能打开一个子窗口
源地址:https://zhidao.baidu.com/question/1511266887807047660.html 指定弹出的子窗口为模态窗口就可以了,这样在子窗口没有关闭前,是不能操作父窗 ...
- Qt(Mac) 进程的启动
试了半天,终于成功了!!!!(教程都是Windows的) 1.与Windows不一样,Mac的要在了路径前加上open: 2.例 图为把一个按钮与TextEdit程序进程联系,点击后就可以启动Text ...
- CF709B Checkpoints 模拟
Vasya takes part in the orienteering competition. There are n checkpoints located along the line at ...
- zabbix监控nginx进程
确认nginx有没有安装模块 然后在主站点下添加(二级站点) location /NginxStatus { stub_status on; access_log off; allow 127.0.0 ...
- appium键盘处理
最近对appium感兴趣,就从网上找了些资料,搭建了环境,下载了appium测试代码和测试apk,这方面的东西晚上再写 appium最新版(v1.4.0.0)已经没有sendKeyEvent了,所以现 ...
- window下安装git与git使用
有的eclipse已经自带了Git了,就不用安装了.如果,想重新安装,可以先卸载GIT,卸载 不同eclipse卸载不一样: 1.在Eclipse中依次点击菜单"Help"-> ...
- [Android UI]View滑动方式总结
一.前言 在上一篇文章,介绍了View的坐标等基础知识,有了基础知识后,对下面内容的理解也将会容易很多.那么本文介绍的是View滑动的几种方式,这对于View来说,也是需要重要掌握的内容,因为用户无时 ...
- HDU 3709 Balanced Number 求区间内的满足是否平衡的数量 (数位dp)
平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; 题意:求区间内满足平衡数的数量 : 分析:很好这又是常见的数位dp , 不过不同的是我们这次 ...