当前位置: 首页 > news >正文

业务服务:redisson

文章目录

  • 前言
  • 一、配置
    • 1. 添加依赖
    • 2. 配置文件/类
    • 3. 注入redission
    • 3. 封装工具类
  • 二、应用
    • 1. RedisUtils工具类的基本使用
  • 三、队列
    • 1. 工具类
    • 2. 普通队列
    • 3. 有界队列(限制数据量)
    • 4. 延迟队列(延迟获取数据)
    • 5. 优先队列(数据可插队)


前言

redission是一个开源的java redis的客户端,在其基础上进行了进一步扩展。这些扩展极大地丰富了Redis的应用场景,尤其是在构建分布式系统时。


一、配置

1. 添加依赖

<!--redisson-->
<dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>${redisson.version}</version><exclusions><exclusion><groupId>org.redisson</groupId><artifactId>redisson-spring-data-30</artifactId></exclusion></exclusions>
</dependency>
<dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-data-27</artifactId><version>${redisson.version}</version>
</dependency>
<dependency><groupId>com.baomidou</groupId><artifactId>lock4j-redisson-spring-boot-starter</artifactId><version>${lock4j.version}</version>
</dependency>

在这里插入图片描述

2. 配置文件/类

spring:redis:# 地址host: localhost# 端口,默认为6379port: 6379# 数据库索引database: 0# 密码(如没有密码请注释掉)password: asd60787533# 连接超时时间timeout: 10s# 是否开启sslssl: falseredisson:# redis key前缀keyPrefix: demo# 线程池数量threads: 4# Netty线程池数量nettyThreads: 8# 单节点配置singleServerConfig:# 客户端名称clientName: demo# 最小空闲连接数connectionMinimumIdleSize: 8# 连接池大小connectionPoolSize: 32# 连接空闲超时,单位:毫秒idleConnectionTimeout: 10000# 命令等待超时,单位:毫秒timeout: 3000# 发布和订阅连接池大小subscriptionConnectionPoolSize: 50
@Data
@Component
@ConfigurationProperties(prefix = "redisson")
public class RedissonProperties {/*** redis缓存key前缀*/private String keyPrefix;/*** 线程池数量,默认值 = 当前处理核数量 * 2*/private int threads;/*** Netty线程池数量,默认值 = 当前处理核数量 * 2*/private int nettyThreads;/*** 单机服务配置*/private SingleServerConfig singleServerConfig;/*** 集群服务配置*/private ClusterServersConfig clusterServersConfig;@Data@NoArgsConstructorpublic static class SingleServerConfig {/*** 客户端名称*/private String clientName;/*** 最小空闲连接数*/private int connectionMinimumIdleSize;/*** 连接池大小*/private int connectionPoolSize;/*** 连接空闲超时,单位:毫秒*/private int idleConnectionTimeout;/*** 命令等待超时,单位:毫秒*/private int timeout;/*** 发布和订阅连接池大小*/private int subscriptionConnectionPoolSize;}@Data@NoArgsConstructorpublic static class ClusterServersConfig {/*** 客户端名称*/private String clientName;/*** master最小空闲连接数*/private int masterConnectionMinimumIdleSize;/*** master连接池大小*/private int masterConnectionPoolSize;/*** slave最小空闲连接数*/private int slaveConnectionMinimumIdleSize;/*** slave连接池大小*/private int slaveConnectionPoolSize;/*** 连接空闲超时,单位:毫秒*/private int idleConnectionTimeout;/*** 命令等待超时,单位:毫秒*/private int timeout;/*** 发布和订阅连接池大小*/private int subscriptionConnectionPoolSize;/*** 读取模式*/private ReadMode readMode;/*** 订阅模式*/private SubscriptionMode subscriptionMode;}}

3. 注入redission

@Slf4j
@Configuration
@EnableCaching
@EnableConfigurationProperties(RedissonProperties.class)
public class RedisConfig {@Autowiredprivate RedissonProperties redissonProperties;@Autowiredprivate ObjectMapper objectMapper;@Beanpublic RedissonAutoConfigurationCustomizer redissonCustomizer() {return config -> {config.setThreads(redissonProperties.getThreads()).setNettyThreads(redissonProperties.getNettyThreads()).setCodec(new JsonJacksonCodec(objectMapper));RedissonProperties.SingleServerConfig singleServerConfig = redissonProperties.getSingleServerConfig();if (ObjectUtil.isNotNull(singleServerConfig)) {// 使用单机模式config.useSingleServer()//设置redis key前缀.setNameMapper(new KeyPrefixHandler(redissonProperties.getKeyPrefix())).setTimeout(singleServerConfig.getTimeout()).setClientName(singleServerConfig.getClientName()).setIdleConnectionTimeout(singleServerConfig.getIdleConnectionTimeout()).setSubscriptionConnectionPoolSize(singleServerConfig.getSubscriptionConnectionPoolSize()).setConnectionMinimumIdleSize(singleServerConfig.getConnectionMinimumIdleSize()).setConnectionPoolSize(singleServerConfig.getConnectionPoolSize());}// 集群配置方式 参考下方注释RedissonProperties.ClusterServersConfig clusterServersConfig = redissonProperties.getClusterServersConfig();if (ObjectUtil.isNotNull(clusterServersConfig)) {config.useClusterServers()//设置redis key前缀.setNameMapper(new KeyPrefixHandler(redissonProperties.getKeyPrefix())).setTimeout(clusterServersConfig.getTimeout()).setClientName(clusterServersConfig.getClientName()).setIdleConnectionTimeout(clusterServersConfig.getIdleConnectionTimeout()).setSubscriptionConnectionPoolSize(clusterServersConfig.getSubscriptionConnectionPoolSize()).setMasterConnectionMinimumIdleSize(clusterServersConfig.getMasterConnectionMinimumIdleSize()).setMasterConnectionPoolSize(clusterServersConfig.getMasterConnectionPoolSize()).setSlaveConnectionMinimumIdleSize(clusterServersConfig.getSlaveConnectionMinimumIdleSize()).setSlaveConnectionPoolSize(clusterServersConfig.getSlaveConnectionPoolSize()).setReadMode(clusterServersConfig.getReadMode()).setSubscriptionMode(clusterServersConfig.getSubscriptionMode());}log.info("初始化 redis 配置");};}/*** redis集群配置 yml** --- # redis 集群配置(单机与集群只能开启一个另一个需要注释掉)* spring:*   redis:*     cluster:*       nodes:*         - 192.168.0.100:6379*         - 192.168.0.101:6379*         - 192.168.0.102:6379*     # 密码*     password:*     # 连接超时时间*     timeout: 10s*     # 是否开启ssl*     ssl: false** redisson:*   # 线程池数量*   threads: 16*   # Netty线程池数量*   nettyThreads: 32*   # 集群配置*   clusterServersConfig:*     # 客户端名称*     clientName: ${ruoyi.name}*     # master最小空闲连接数*     masterConnectionMinimumIdleSize: 32*     # master连接池大小*     masterConnectionPoolSize: 64*     # slave最小空闲连接数*     slaveConnectionMinimumIdleSize: 32*     # slave连接池大小*     slaveConnectionPoolSize: 64*     # 连接空闲超时,单位:毫秒*     idleConnectionTimeout: 10000*     # 命令等待超时,单位:毫秒*     timeout: 3000*     # 发布和订阅连接池大小*     subscriptionConnectionPoolSize: 50*     # 读取模式*     readMode: "SLAVE"*     # 订阅模式*     subscriptionMode: "MASTER"*/}
public class KeyPrefixHandler implements NameMapper {private final String keyPrefix;public KeyPrefixHandler(String keyPrefix) {//前缀为空 则返回空前缀this.keyPrefix = StringUtils.isBlank(keyPrefix) ? "" : keyPrefix + ":";}/*** 增加前缀*/@Overridepublic String map(String name) {if (StringUtils.isBlank(name)) {return null;}if (StringUtils.isNotBlank(keyPrefix) && !name.startsWith(keyPrefix)) {return keyPrefix + name;}return name;}/*** 去除前缀*/@Overridepublic String unmap(String name) {if (StringUtils.isBlank(name)) {return null;}if (StringUtils.isNotBlank(keyPrefix) && name.startsWith(keyPrefix)) {return name.substring(keyPrefix.length());}return name;}
}

3. 封装工具类

@NoArgsConstructor(access = AccessLevel.PRIVATE)
@SuppressWarnings(value = {"unchecked", "rawtypes"})
public class RedisUtils {private static final RedissonClient CLIENT = SpringUtils.getBean(RedissonClient.class);/*** 限流** @param key          限流key* @param rateType     限流类型* @param rate         速率* @param rateInterval 速率间隔* @return -1 表示失败*/public static long rateLimiter(String key, RateType rateType, int rate, int rateInterval) {RRateLimiter rateLimiter = CLIENT.getRateLimiter(key);rateLimiter.trySetRate(rateType, rate, rateInterval, RateIntervalUnit.SECONDS);if (rateLimiter.tryAcquire()) {return rateLimiter.availablePermits();} else {return -1L;}}/*** 获取客户端实例*/public static RedissonClient getClient() {return CLIENT;}/*** 发布通道消息** @param channelKey 通道key* @param msg        发送数据* @param consumer   自定义处理*/public static <T> void publish(String channelKey, T msg, Consumer<T> consumer) {RTopic topic = CLIENT.getTopic(channelKey);topic.publish(msg);consumer.accept(msg);}public static <T> void publish(String channelKey, T msg) {RTopic topic = CLIENT.getTopic(channelKey);topic.publish(msg);}/*** 订阅通道接收消息** @param channelKey 通道key* @param clazz      消息类型* @param consumer   自定义处理*/public static <T> void subscribe(String channelKey, Class<T> clazz, Consumer<T> consumer) {RTopic topic = CLIENT.getTopic(channelKey);topic.addListener(clazz, (channel, msg) -> consumer.accept(msg));}/*** 缓存基本的对象,Integer、String、实体类等** @param key   缓存的键值* @param value 缓存的值*/public static <T> void setCacheObject(final String key, final T value) {setCacheObject(key, value, false);}/*** 缓存基本的对象,保留当前对象 TTL 有效期** @param key       缓存的键值* @param value     缓存的值* @param isSaveTtl 是否保留TTL有效期(例如: set之前ttl剩余90 set之后还是为90)* @since Redis 6.X 以上使用 setAndKeepTTL 兼容 5.X 方案*/public static <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) {RBucket<T> bucket = CLIENT.getBucket(key);if (isSaveTtl) {try {bucket.setAndKeepTTL(value);} catch (Exception e) {long timeToLive = bucket.remainTimeToLive();setCacheObject(key, value, Duration.ofMillis(timeToLive));}} else {bucket.set(value);}}/*** 缓存基本的对象,Integer、String、实体类等** @param key      缓存的键值* @param value    缓存的值* @param duration 时间*/public static <T> void setCacheObject(final String key, final T value, final Duration duration) {RBatch batch = CLIENT.createBatch();RBucketAsync<T> bucket = batch.getBucket(key);bucket.setAsync(value);bucket.expireAsync(duration);batch.execute();}/*** 注册对象监听器* <p>* key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置** @param key      缓存的键值* @param listener 监听器配置*/public static <T> void addObjectListener(final String key, final ObjectListener listener) {RBucket<T> result = CLIENT.getBucket(key);result.addListener(listener);}/*** 设置有效时间** @param key     Redis键* @param timeout 超时时间* @return true=设置成功;false=设置失败*/public static boolean expire(final String key, final long timeout) {return expire(key, Duration.ofSeconds(timeout));}/*** 设置有效时间** @param key      Redis键* @param duration 超时时间* @return true=设置成功;false=设置失败*/public static boolean expire(final String key, final Duration duration) {RBucket rBucket = CLIENT.getBucket(key);return rBucket.expire(duration);}/*** 获得缓存的基本对象。** @param key 缓存键值* @return 缓存键值对应的数据*/public static <T> T getCacheObject(final String key) {RBucket<T> rBucket = CLIENT.getBucket(key);return rBucket.get();}/*** 获得key剩余存活时间** @param key 缓存键值* @return 剩余存活时间*/public static <T> long getTimeToLive(final String key) {RBucket<T> rBucket = CLIENT.getBucket(key);return rBucket.remainTimeToLive();}/*** 删除单个对象** @param key 缓存的键值*/public static boolean deleteObject(final String key) {return CLIENT.getBucket(key).delete();}/*** 删除集合对象** @param collection 多个对象*/public static void deleteObject(final Collection collection) {RBatch batch = CLIENT.createBatch();collection.forEach(t -> {batch.getBucket(t.toString()).deleteAsync();});batch.execute();}/*** 检查缓存对象是否存在** @param key 缓存的键值*/public static boolean isExistsObject(final String key) {return CLIENT.getBucket(key).isExists();}/*** 缓存List数据** @param key      缓存的键值* @param dataList 待缓存的List数据* @return 缓存的对象*/public static <T> boolean setCacheList(final String key, final List<T> dataList) {RList<T> rList = CLIENT.getList(key);return rList.addAll(dataList);}/*** 注册List监听器* <p>* key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置** @param key      缓存的键值* @param listener 监听器配置*/public static <T> void addListListener(final String key, final ObjectListener listener) {RList<T> rList = CLIENT.getList(key);rList.addListener(listener);}/*** 获得缓存的list对象** @param key 缓存的键值* @return 缓存键值对应的数据*/public static <T> List<T> getCacheList(final String key) {RList<T> rList = CLIENT.getList(key);return rList.readAll();}/*** 缓存Set** @param key     缓存键值* @param dataSet 缓存的数据* @return 缓存数据的对象*/public static <T> boolean setCacheSet(final String key, final Set<T> dataSet) {RSet<T> rSet = CLIENT.getSet(key);return rSet.addAll(dataSet);}/*** 注册Set监听器* <p>* key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置** @param key      缓存的键值* @param listener 监听器配置*/public static <T> void addSetListener(final String key, final ObjectListener listener) {RSet<T> rSet = CLIENT.getSet(key);rSet.addListener(listener);}/*** 获得缓存的set** @param key 缓存的key* @return set对象*/public static <T> Set<T> getCacheSet(final String key) {RSet<T> rSet = CLIENT.getSet(key);return rSet.readAll();}/*** 缓存Map** @param key     缓存的键值* @param dataMap 缓存的数据*/public static <T> void setCacheMap(final String key, final Map<String, T> dataMap) {if (dataMap != null) {RMap<String, T> rMap = CLIENT.getMap(key);rMap.putAll(dataMap);}}/*** 注册Map监听器* <p>* key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置** @param key      缓存的键值* @param listener 监听器配置*/public static <T> void addMapListener(final String key, final ObjectListener listener) {RMap<String, T> rMap = CLIENT.getMap(key);rMap.addListener(listener);}/*** 获得缓存的Map** @param key 缓存的键值* @return map对象*/public static <T> Map<String, T> getCacheMap(final String key) {RMap<String, T> rMap = CLIENT.getMap(key);return rMap.getAll(rMap.keySet());}/*** 获得缓存Map的key列表** @param key 缓存的键值* @return key列表*/public static <T> Set<String> getCacheMapKeySet(final String key) {RMap<String, T> rMap = CLIENT.getMap(key);return rMap.keySet();}/*** 往Hash中存入数据** @param key   Redis键* @param hKey  Hash键* @param value 值*/public static <T> void setCacheMapValue(final String key, final String hKey, final T value) {RMap<String, T> rMap = CLIENT.getMap(key);rMap.put(hKey, value);}/*** 获取Hash中的数据** @param key  Redis键* @param hKey Hash键* @return Hash中的对象*/public static <T> T getCacheMapValue(final String key, final String hKey) {RMap<String, T> rMap = CLIENT.getMap(key);return rMap.get(hKey);}/*** 删除Hash中的数据** @param key  Redis键* @param hKey Hash键* @return Hash中的对象*/public static <T> T delCacheMapValue(final String key, final String hKey) {RMap<String, T> rMap = CLIENT.getMap(key);return rMap.remove(hKey);}/*** 获取多个Hash中的数据** @param key   Redis键* @param hKeys Hash键集合* @return Hash对象集合*/public static <K, V> Map<K, V> getMultiCacheMapValue(final String key, final Set<K> hKeys) {RMap<K, V> rMap = CLIENT.getMap(key);return rMap.getAll(hKeys);}/*** 设置原子值** @param key   Redis键* @param value 值*/public static void setAtomicValue(String key, long value) {RAtomicLong atomic = CLIENT.getAtomicLong(key);atomic.set(value);}/*** 获取原子值** @param key Redis键* @return 当前值*/public static long getAtomicValue(String key) {RAtomicLong atomic = CLIENT.getAtomicLong(key);return atomic.get();}/*** 递增原子值** @param key Redis键* @return 当前值*/public static long incrAtomicValue(String key) {RAtomicLong atomic = CLIENT.getAtomicLong(key);return atomic.incrementAndGet();}/*** 递减原子值** @param key Redis键* @return 当前值*/public static long decrAtomicValue(String key) {RAtomicLong atomic = CLIENT.getAtomicLong(key);return atomic.decrementAndGet();}/*** 获得缓存的基本对象列表** @param pattern 字符串前缀* @return 对象列表*/public static Collection<String> keys(final String pattern) {Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(pattern);return stream.collect(Collectors.toList());}/*** 删除缓存的基本对象列表** @param pattern 字符串前缀*/public static void deleteKeys(final String pattern) {CLIENT.getKeys().deleteByPattern(pattern);}/*** 检查redis中是否存在key** @param key 键*/public static Boolean hasKey(String key) {RKeys rKeys = CLIENT.getKeys();return rKeys.countExists(key) > 0;}
}

二、应用

1. RedisUtils工具类的基本使用

创建接口

@GetMapping("key")
public String getKey(String key){return RedisUtils.getCacheObject(key);
}@GetMapping("setKey")
public String setKey(String key,String value){RedisUtils.setCacheObject(key,value);return "success";
}

设置key

在这里插入图片描述
在这里插入图片描述

获取key对应的值

在这里插入图片描述
其他方法的作用,可以自行测试。这里就不再演示使用

三、队列

redission也支持队列,下面封装了一些队列的相关方法。可以处理了一些简单的队列任务,如果业务复杂可以选择mq

1. 工具类

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class QueueUtils {private static final RedissonClient CLIENT = SpringUtil.getBean(RedissonClient.class);/*** 获取客户端实例*/public static RedissonClient getClient() {return CLIENT;}/*** 添加普通队列数据** @param queueName 队列名* @param data      数据*/public static <T> boolean addQueueObject(String queueName, T data) {RBlockingQueue<T> queue = CLIENT.getBlockingQueue(queueName);return queue.offer(data);}/*** 通用获取一个队列数据 没有数据返回 null(不支持延迟队列)** @param queueName 队列名*/public static <T> T getQueueObject(String queueName) {RBlockingQueue<T> queue = CLIENT.getBlockingQueue(queueName);return queue.poll();}/*** 通用删除队列数据(不支持延迟队列)*/public static <T> boolean removeQueueObject(String queueName, T data) {RBlockingQueue<T> queue = CLIENT.getBlockingQueue(queueName);return queue.remove(data);}/*** 通用销毁队列 所有阻塞监听 报错(不支持延迟队列)*/public static <T> boolean destroyQueue(String queueName) {RBlockingQueue<T> queue = CLIENT.getBlockingQueue(queueName);return queue.delete();}/*** 添加延迟队列数据 默认毫秒** @param queueName 队列名* @param data      数据* @param time      延迟时间*/public static <T> void addDelayedQueueObject(String queueName, T data, long time) {addDelayedQueueObject(queueName, data, time, TimeUnit.MILLISECONDS);}/*** 添加延迟队列数据** @param queueName 队列名* @param data      数据* @param time      延迟时间* @param timeUnit  单位*/public static <T> void addDelayedQueueObject(String queueName, T data, long time, TimeUnit timeUnit) {RBlockingQueue<T> queue = CLIENT.getBlockingQueue(queueName);RDelayedQueue<T> delayedQueue = CLIENT.getDelayedQueue(queue);delayedQueue.offer(data, time, timeUnit);}/*** 获取一个延迟队列数据 没有数据返回 null** @param queueName 队列名*/public static <T> T getDelayedQueueObject(String queueName) {RBlockingQueue<T> queue = CLIENT.getBlockingQueue(queueName);RDelayedQueue<T> delayedQueue = CLIENT.getDelayedQueue(queue);return delayedQueue.poll();}/*** 删除延迟队列数据*/public static <T> boolean removeDelayedQueueObject(String queueName, T data) {RBlockingQueue<T> queue = CLIENT.getBlockingQueue(queueName);RDelayedQueue<T> delayedQueue = CLIENT.getDelayedQueue(queue);return delayedQueue.remove(data);}/*** 销毁延迟队列 所有阻塞监听 报错*/public static <T> void destroyDelayedQueue(String queueName) {RBlockingQueue<T> queue = CLIENT.getBlockingQueue(queueName);RDelayedQueue<T> delayedQueue = CLIENT.getDelayedQueue(queue);delayedQueue.destroy();}/*** 添加优先队列数据** @param queueName 队列名* @param data      数据*/public static <T> boolean addPriorityQueueObject(String queueName, T data) {RPriorityBlockingQueue<T> priorityBlockingQueue = CLIENT.getPriorityBlockingQueue(queueName);return priorityBlockingQueue.offer(data);}/*** 优先队列获取一个队列数据 没有数据返回 null(不支持延迟队列)** @param queueName 队列名*/public static <T> T getPriorityQueueObject(String queueName) {RPriorityBlockingQueue<T> queue = CLIENT.getPriorityBlockingQueue(queueName);return queue.poll();}/*** 优先队列删除队列数据(不支持延迟队列)*/public static <T> boolean removePriorityQueueObject(String queueName, T data) {RPriorityBlockingQueue<T> queue = CLIENT.getPriorityBlockingQueue(queueName);return queue.remove(data);}/*** 优先队列销毁队列 所有阻塞监听 报错(不支持延迟队列)*/public static <T> boolean destroyPriorityQueue(String queueName) {RPriorityBlockingQueue<T> queue = CLIENT.getPriorityBlockingQueue(queueName);return queue.delete();}/*** 尝试设置 有界队列 容量 用于限制数量** @param queueName 队列名* @param capacity  容量*/public static <T> boolean trySetBoundedQueueCapacity(String queueName, int capacity) {RBoundedBlockingQueue<T> boundedBlockingQueue = CLIENT.getBoundedBlockingQueue(queueName);return boundedBlockingQueue.trySetCapacity(capacity);}/*** 尝试设置 有界队列 容量 用于限制数量** @param queueName 队列名* @param capacity  容量* @param destroy   已存在是否销毁*/public static <T> boolean trySetBoundedQueueCapacity(String queueName, int capacity, boolean destroy) {RBoundedBlockingQueue<T> boundedBlockingQueue = CLIENT.getBoundedBlockingQueue(queueName);if (boundedBlockingQueue.isExists() && destroy) {destroyQueue(queueName);}return boundedBlockingQueue.trySetCapacity(capacity);}/*** 添加有界队列数据** @param queueName 队列名* @param data      数据* @return 添加成功 true 已达到界限 false*/public static <T> boolean addBoundedQueueObject(String queueName, T data) {RBoundedBlockingQueue<T> boundedBlockingQueue = CLIENT.getBoundedBlockingQueue(queueName);return boundedBlockingQueue.offer(data);}/*** 有界队列获取一个队列数据 没有数据返回 null(不支持延迟队列)** @param queueName 队列名*/public static <T> T getBoundedQueueObject(String queueName) {RBoundedBlockingQueue<T> queue = CLIENT.getBoundedBlockingQueue(queueName);return queue.poll();}/*** 有界队列删除队列数据(不支持延迟队列)*/public static <T> boolean removeBoundedQueueObject(String queueName, T data) {RBoundedBlockingQueue<T> queue = CLIENT.getBoundedBlockingQueue(queueName);return queue.remove(data);}/*** 有界队列销毁队列 所有阻塞监听 报错(不支持延迟队列)*/public static <T> boolean destroyBoundedQueue(String queueName) {RBoundedBlockingQueue<T> queue = CLIENT.getBoundedBlockingQueue(queueName);return queue.delete();}/*** 订阅阻塞队列(可订阅所有实现类 例如: 延迟 优先 有界 等)*/public static <T> void subscribeBlockingQueue(String queueName, Consumer<T> consumer, boolean isDelayed) {RBlockingQueue<T> queue = CLIENT.getBlockingQueue(queueName);if (isDelayed) {// 订阅延迟队列CLIENT.getDelayedQueue(queue);}queue.subscribeOnElements(consumer);}}

2. 普通队列

添加数据到队列

@GetMapping("add")
public String add(){QueueUtils.addQueueObject("queue:simple",1);QueueUtils.addQueueObject("queue:simple",2);QueueUtils.addQueueObject("queue:simple",3);return "ok";
}

在这里插入图片描述

消费队列数据

遵循先进先出,获取数据后就会删除。如果队列中没有数据,获取到的就为null

@GetMapping("get")
public Integer get(){return QueueUtils.getQueueObject("queue:simple");
}

在这里插入图片描述

移除队列数据

@GetMapping("remove")
public String remove(){QueueUtils.removeQueueObject("queue:simple",3);return "ok";
}

在这里插入图片描述

销毁队列

@GetMapping("destroy")
public String destroy(){QueueUtils.destroyQueue("queue:simple");return "ok";
}

在这里插入图片描述

订阅队列消息

  • 订阅的消息一般在项目启动的时候使用,只能订阅一次
  • 当监听到队列新增数据的时候会立即取出来进行消费
@PostConstruct
public void sub(){QueueUtils.subscribeBlockingQueue("queue:simple",(o)->{System.out.println("接收到消息:"+o);},false);
}

我们再次调用新增

在这里插入图片描述

3. 有界队列(限制数据量)

设置队列最大容量

有界队列在使用前必须设置容量

@GetMapping("set")
public String set(){boolean b = QueueUtils.trySetBoundedQueueCapacity("queue:bound", 10);return  "ok";
}

在这里插入图片描述

新增有界队列数据

@GetMapping("add")
public String add(){QueueUtils.addBoundedQueueObject("queue:bound",1);return "ok";
}

新增完毕后我们可以发现,我们直接设置的最大容量变成来了9。每次添加数据都会查询当前最大容量是否>0,如果大于0添加成功并且减一,否则添加失败

在这里插入图片描述
在这里插入图片描述

获取有界队列数据

@GetMapping("get")
public Integer get(){return QueueUtils.getBoundedQueueObject("queue:bound");
}

我们可以看到当获取数据的时候,容量+1,数据从redis中删除

在这里插入图片描述
在这里插入图片描述

其他用法与普通队列类似,就不再演示了

4. 延迟队列(延迟获取数据)

添加延迟数据

延迟队列的实现原理是将数据添加到另一个缓存队列中,当到达指定时间才会转移到普通队列中

@GetMapping("add")
public String add(){QueueUtils.addDelayedQueueObject("queue:belay",1,10, TimeUnit.SECONDS);return "ok";
}

获取延迟数据

必须达到指定时间后才能获取

@GetMapping("get")
public Integer get(){return QueueUtils.getDelayedQueueObject("queue:belay");
}

删除延迟数据

@GetMapping("remove")
public String remove(){QueueUtils.removeQueueObject("queue:belay",3);return "ok";
}

清空延迟数据

@GetMapping("destroy")
public String destroy(){QueueUtils.destroyDelayedQueue("queue:belay");return "ok";
}

订阅消息使用方法同普通队列类似,第三个参数需要改为true

5. 优先队列(数据可插队)

插入优先队列的数据我们需要先实现比较接口

@Data
@Accessors(chain = true)
class Order implements Comparable<Order>{private Long id;@Overridepublic int compareTo(Order o) {return Long.compare(getId(), o.id);}
}

新增优先数据

@GetMapping("add")
public String add(){QueueUtils.addPriorityQueueObject("queue:priority",new Order().setId(1L));QueueUtils.addPriorityQueueObject("queue:priority",new Order().setId(6L));QueueUtils.addPriorityQueueObject("queue:priority",new Order().setId(2L));QueueUtils.addPriorityQueueObject("queue:priority",new Order().setId(5L));QueueUtils.addPriorityQueueObject("queue:priority",new Order().setId(22L));QueueUtils.addPriorityQueueObject("queue:priority",new Order().setId(3L));return "ok";
}

我们可以看到插入的数据是有序的

在这里插入图片描述

获取优先队列数据

@GetMapping("get")
public Integer get(){return QueueUtils.getPriorityQueueObject("queue:priority");
}

删除优先队列数据

@GetMapping("remove")
public String remove(){QueueUtils.removeQueueObject("queue:priority",3);return "ok";
}

清空优先队列数据

@GetMapping("destroy")
public String destroy(){QueueUtils.destroyDelayedQueue("queue:priority");return "ok";
}

订阅消息使用方法同普通队列一样

相关文章:

业务服务:redisson

文章目录 前言一、配置1. 添加依赖2. 配置文件/类3. 注入redission3. 封装工具类 二、应用1. RedisUtils工具类的基本使用 三、队列1. 工具类2. 普通队列3. 有界队列&#xff08;限制数据量&#xff09;4. 延迟队列&#xff08;延迟获取数据&#xff09;5. 优先队列&#xff08…...

面试算法-100-零钱兑换

题目 给你一个整数数组 coins &#xff0c;表示不同面额的硬币&#xff1b;以及一个整数 amount &#xff0c;表示总金额。 计算并返回可以凑成总金额所需的 最少的硬币个数 。如果没有任何一种硬币组合能组成总金额&#xff0c;返回 -1 。 你可以认为每种硬币的数量是无限的…...

【Leetcode每日一题】 动态规划 - 使用最小花费爬楼梯(难度⭐)(41)

1. 题目解析 题目链接&#xff1a;746. 使用最小花费爬楼梯 这个问题的理解其实相当简单&#xff0c;只需看一下示例&#xff0c;基本就能明白其含义了。 2.算法原理 一、设定状态表 为了解决这个问题&#xff0c;我们首先要明确一个“状态表”。这个状态表其实就是一个记录…...

Springboot旅游管理系统设计与实现

** &#x1f345;点赞收藏关注 → 私信领取本源代码、数据库&#x1f345; 本人在Java毕业设计领域有多年的经验&#xff0c;陆续会更新更多优质的Java实战项目&#xff0c;希望你能有所收获&#xff0c;少走一些弯路。&#x1f345;关注我不迷路&#x1f345;** 一、研究背景…...

c++学习笔记(10)

1. 二分答案是一种常用的算法思想&#xff0c;用于解决一些需要枚举所有可能答案的问题。它的基本思想是将问题的答案范围缩小到一半&#xff0c;然后根据一定的条件判断&#xff0c;再将答案范围缩小到一半&#xff0c;直到找到正确的答案或者确定不存在正确答案为止。 下面…...

Visual Studio - 添加快捷键图标

Visual Studio - 添加快捷键图标 1. Text Editor Toolbar Options -> Add or Remove Buttons -> Customize2. Toolbars3. Commands -> Debug4. Add Command...References 1. Text Editor Toolbar Options -> Add or Remove Buttons -> Customize 2. Toolbars B…...

突破边界:Web3开启数字化社会的新纪元

引言 随着科技的不断进步和数字化社会的发展&#xff0c;Web3正逐渐成为了人们关注的焦点。作为新一代互联网的演进形态&#xff0c;Web3具有突破传统边界、实现去中心化的特点&#xff0c;被认为将开启数字化社会的新纪元。本文将深入探讨Web3的概念、特点、应用场景&#xf…...

docker仓库登录及配置insecure-registries的方法

docker仓库登录及配置insecure-registries的方法 这篇文章主要介绍了docker仓库登录配置insecure-registries的方法,docker客户端如果配置中添加了insecure-registary配置&#xff0c;就不需要在docker 客户端配置上对应证书&#xff0c;如果不配置要在/etc/docker/certs.d/目…...

Lambda函数与Selenium WebDriverWait类一起使用

Lambda函数是一种匿名函数&#xff0c;也称为内联函数或者lambda表达式。它们在Python中用于创建简短的、一次性的函数。Lambda函数通常用于在代码中传递函数作为参数&#xff0c;或者在需要一个简单的函数&#xff0c;但不想正式定义一个函数的情况下使用。 Lambda函数的特点…...

V R元宇宙平台的未来方向|V R主题馆加 盟|游戏体验馆

未来&#xff0c;VR元宇宙平台可能会呈现出以下发展趋势和可能性&#xff1a; 全面融合现实与虚拟世界&#xff1a; VR元宇宙平台将更加无缝地融合现实世界和虚拟世界&#xff0c;用户可以在虚拟环境中进行各种活动&#xff0c;与现实世界进行互动&#xff0c;并且体验到更加逼…...

sox命令用法

play input.wav 播放音频 sox input.wav -n stat 查看音频文件信息 soxi input.wav 查看音频文件信息input sox input.wav -n stat -v 不失真最大调整量 sox -v 0.8 input.wav output.wav 调整音量0.8,&#xff08;大于1为扩大&#xff0c;小于1为缩小&#xff09; sox *.wav *…...

Android kotlin全局悬浮窗全屏功能和锁屏页面全屏悬浮窗功能二

1.前言 在进行app应用开发中,在实现某些功能中要求实现悬浮窗功能,分为应用内悬浮窗 ,全局悬浮窗和 锁屏页面悬浮窗功能 等,接下来就来实现这些悬浮窗全屏功能,首选看下第二部分功能实现 2.kotlin实现锁屏页面悬浮窗全屏功能二分析 悬浮窗是属于Android系统的一种浮动窗…...

[数据结构]二叉树与递归OJ

上回我们手撕了一棵二叉树,并且通过递归完成了遍历,这回我们将深入理解用递归解决相关的二叉树问题,数量使用分治的思想. 上回的代码: #include<stdio.h> #include<stdlib.h> typedef struct BinTreeNode {struct BinTreeNode* left;struct BinTreeNode* right;i…...

vue iframe实现父页面实时调用子页面方法和内容,已解决

父页面标签添加鼠标按下事件 父页方法中建立iframe通信 实时调用子页面方法 实时更改子页面文本内容...

Spring Cloud Gateway教程

1 微服务网关概述 Spring Cloud Gateway是在 Spring 生态系统之上构建的API网关服务&#xff0c;旨在为微服务架构应用提供一种简单有效的统一的API路由管理方式。 Spring Cloud Gateway主要功能&#xff1a; 反向代理认证鉴权流量控制熔断日志监控 2 Spring Cloud Gateway三…...

解码新时代内存架构:探秘数据在内存中的灵动驻足

欢迎来到白刘的领域 Miracle_86.-CSDN博客 系列专栏 C语言知识 先赞后看&#xff0c;已成习惯 创作不易&#xff0c;多多支持&#xff01; 随着信息技术的飞速发展&#xff0c;我们身处一个数据爆炸的时代。数据的处理和存储方式正日益成为技术革新的重要领域。在新时代的…...

前端基础篇-前端工程化 Vue 项目开发流程(环境准备、Element 组件库、Vue 路由、项目打包部署)

&#x1f525;博客主页&#xff1a; 【小扳_-CSDN博客】 ❤感谢大家点赞&#x1f44d;收藏⭐评论✍ 文章目录 1.0 环境准备 1.1 安装 NodeJs 1.2 验证 NodeJs 环境变量 1.3 配置 npm 的全局安装路径 1.4 切换 npm 的淘宝镜像( npm 使用国内淘宝镜像的方法(最新) ) 1.5 查看镜像…...

【通用人工智能AGI元年-各领域的精彩AI/LLM(持续更新)】

AI元年弄潮儿 通用人工智能AGI时代大模型LLM集成平台&#xff1a;Poe语言大模型&#xff1a;ChatGPT音乐&#xff1a;Suno文生图&#xff1a; [Stable Diffusion整合包](https://www.bilibili.com/video/BV1iM4y1y7oA/?spm_id_from333.999.0.0&vd_source260c69efcf1f56243…...

【微服务】设计弹性微服务架构模式

目录 模式#1 — 超时模式#2 — 重试模式#3— 隔离模式#4— 断路器模式#5 — 冗余推荐超级课程: Docker快速入门到精通Kubernetes入门到大师通关课AWS云服务快速入门实战在微服务架构中,服务通常相互协作以提供业务用例。这些服务可能在可用性、可伸缩性、弹性等方面具有…...

Websocket + Vue使用

这里有一篇文档可以参考一下> 闪现 POM文件 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId><version>2.7.0</version> </dependency> WebSocketConf…...

KubeSphere 容器平台高可用:环境搭建与可视化操作指南

Linux_k8s篇 欢迎来到Linux的世界&#xff0c;看笔记好好学多敲多打&#xff0c;每个人都是大神&#xff01; 题目&#xff1a;KubeSphere 容器平台高可用&#xff1a;环境搭建与可视化操作指南 版本号: 1.0,0 作者: 老王要学习 日期: 2025.06.05 适用环境: Ubuntu22 文档说…...

【ROS】Nav2源码之nav2_behavior_tree-行为树节点列表

1、行为树节点分类 在 Nav2(Navigation2)的行为树框架中,行为树节点插件按照功能分为 Action(动作节点)、Condition(条件节点)、Control(控制节点) 和 Decorator(装饰节点) 四类。 1.1 动作节点 Action 执行具体的机器人操作或任务,直接与硬件、传感器或外部系统…...

RNN避坑指南:从数学推导到LSTM/GRU工业级部署实战流程

本文较长&#xff0c;建议点赞收藏&#xff0c;以免遗失。更多AI大模型应用开发学习视频及资料&#xff0c;尽在聚客AI学院。 本文全面剖析RNN核心原理&#xff0c;深入讲解梯度消失/爆炸问题&#xff0c;并通过LSTM/GRU结构实现解决方案&#xff0c;提供时间序列预测和文本生成…...

项目部署到Linux上时遇到的错误(Redis,MySQL,无法正确连接,地址占用问题)

Redis无法正确连接 在运行jar包时出现了这样的错误 查询得知问题核心在于Redis连接失败&#xff0c;具体原因是客户端发送了密码认证请求&#xff0c;但Redis服务器未设置密码 1.为Redis设置密码&#xff08;匹配客户端配置&#xff09; 步骤&#xff1a; 1&#xff09;.修…...

Device Mapper 机制

Device Mapper 机制详解 Device Mapper&#xff08;简称 DM&#xff09;是 Linux 内核中的一套通用块设备映射框架&#xff0c;为 LVM、加密磁盘、RAID 等提供底层支持。本文将详细介绍 Device Mapper 的原理、实现、内核配置、常用工具、操作测试流程&#xff0c;并配以详细的…...

算法笔记2

1.字符串拼接最好用StringBuilder&#xff0c;不用String 2.创建List<>类型的数组并创建内存 List arr[] new ArrayList[26]; Arrays.setAll(arr, i -> new ArrayList<>()); 3.去掉首尾空格...

Angular微前端架构:Module Federation + ngx-build-plus (Webpack)

以下是一个完整的 Angular 微前端示例&#xff0c;其中使用的是 Module Federation 和 npx-build-plus 实现了主应用&#xff08;Shell&#xff09;与子应用&#xff08;Remote&#xff09;的集成。 &#x1f6e0;️ 项目结构 angular-mf/ ├── shell-app/ # 主应用&…...

LangChain知识库管理后端接口:数据库操作详解—— 构建本地知识库系统的基础《二》

这段 Python 代码是一个完整的 知识库数据库操作模块&#xff0c;用于对本地知识库系统中的知识库进行增删改查&#xff08;CRUD&#xff09;操作。它基于 SQLAlchemy ORM 框架 和一个自定义的装饰器 with_session 实现数据库会话管理。 &#x1f4d8; 一、整体功能概述 该模块…...

C++.OpenGL (20/64)混合(Blending)

混合(Blending) 透明效果核心原理 #mermaid-svg-SWG0UzVfJms7Sm3e {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-icon{fill:#552222;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-text{fill…...

【JVM面试篇】高频八股汇总——类加载和类加载器

目录 1. 讲一下类加载过程&#xff1f; 2. Java创建对象的过程&#xff1f; 3. 对象的生命周期&#xff1f; 4. 类加载器有哪些&#xff1f; 5. 双亲委派模型的作用&#xff08;好处&#xff09;&#xff1f; 6. 讲一下类的加载和双亲委派原则&#xff1f; 7. 双亲委派模…...