SpringBoot集成redis的JedisCluster和RedisTemplate 实现redis的消息队列消费者-生产者模式,订阅者发布者模式

本文主要是介绍SpringBoot集成redis的JedisCluster和RedisTemplate 实现redis的消息队列消费者-生产者模式,订阅者发布者模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式。利用redis这两种场景的消息队列都能够实现。
定义:

  • 生产者消费者模式 :生产者生产消息放到队列里,多个消费者同时监听队列,谁先抢到消息谁就会从队列中取走消息;即对于每个消息只能被最多一个消费者拥有。
  • 发布者订阅者模式:发布者生产消息放到队列里,多个监听队列的消费者都会收到同一份消息;即正常情况下每个消费者收到的消息应该都是一样的。

Redis不仅可作为缓存服务器,还可用作消息队列。它的列表类型天生支持用作消息队列。如下图所示:
在这里插入图片描述
由于Redis的列表是使用双向链表实现的,保存了头尾节点,所以在列表头尾两边插取元素都是非常快的。

在Redis中,List类型是按照插入顺序排序的字符串链表。和数据结构中的普通链表一样,我们可以在其头部(left)和尾部(right)添加新的元素。在插入时,如果该键并不存在,Redis将为该键创建一个新的链表。与此相反,如果链表中所有的元素均被移除,那么该键也将会被从数据库中删除。List中可以包含的最大元素数量是4294967295。
从元素插入和删除的效率视角来看,如果我们是在链表的两头插入或删除元素,这将会是非常高效的操作,即使链表中已经存储了百万条记录,该操作也可以在常量时间内完成。然而需要说明的是,如果元素插入或删除操作是作用于链表中间,那将会是非常低效的。相信对于有良好数据结构基础的开发者而言,这一点并不难理解。

Redis List的主要操作为lpush/lpop/rpush/rpop四种,分别代表从头部和尾部的push/pop,除此之外List还提供了两种pop操作的阻塞版本blpop/brpop,用于阻塞获取一个对象。

Redis通常都被用做一个处理各种后台工作或消息任务的消息服务器。 一个简单的队列模式就是:生产者把消息放入一个列表中,等待消息的消费者用 RPOP 命令(用轮询方式), 或者用 BRPOP 命令(如果客户端使用阻塞操作会更好)来得到这个消息。

以下列举SpringBoot集成redis的JedisCluster和RedisTemplate

引入依赖到pom.xml

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency>

application.yml增加redis集群配置

spring:redis:password:clusters: 10.10.1.238:7000,10.10.1.238:7001,10.10.1.238:7002,10.10.1.238:7003,10.10.1.238:7004,10.10.1.238:7005

RedisConfig配置

package com.example.myframe.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;import java.lang.reflect.Field;@Configuration
public class RedisConfig extends CachingConfigurerSupport {/**redis密码**/@Value("${spring.redis.password}")public String password;@Value("${spring.redis.clusters}")public String cluster;@Beanpublic KeyGenerator keyGenerator() {return (target, method, params) -> {StringBuilder sb = new StringBuilder();sb.append(target.getClass().getName());sb.append(method.getName());for (Object obj : params) {sb.append(obj.toString());}return sb.toString();};}public  Object getFieldValueByObject (Object object , String targetFieldName) throws Exception {// 获取该对象的ClassClass objClass = object.getClass();// 获取所有的属性数组Field[] fields = objClass.getDeclaredFields();for (Field field:fields) {// 属性名称field.setAccessible(true);String currentFieldName = field.getName();if(currentFieldName.equals(targetFieldName)){return field.get(object); // 通过反射拿到该属性在此对象中的值(也可能是个对象)}}return null;}/*** 通过反射获取JedisCluster* @param factory* @return*/@Beanpublic JedisCluster redisCluster(RedisConnectionFactory factory){Object object =null;try {object= getFieldValueByObject(factory,"cluster");} catch (Exception e) {e.printStackTrace();}return (JedisCluster)object;}@Beanpublic RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {StringRedisTemplate template = new StringRedisTemplate(factory);Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);template.setValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}@Bean(name="factory")public RedisConnectionFactory factory(RedisClusterConfiguration clusterConfig){JedisConnectionFactory redisConnectionFactory=new JedisConnectionFactory(clusterConfig);String redisPassword = password;redisConnectionFactory.setPassword(redisPassword);redisConnectionFactory.setPoolConfig(createJedisPoolConfig());redisConnectionFactory.setTimeout(30000);redisConnectionFactory.setUsePool(true);  return redisConnectionFactory;  }@Bean(name="clusterConfig")public RedisClusterConfiguration clusterConfig(){RedisClusterConfiguration config = new RedisClusterConfiguration();String[] nodes = cluster.split(",");for(String node : nodes){String[] host =  node.split(":");RedisNode redis = new RedisNode(host[0], Integer.parseInt(host[1]));config.addClusterNode(redis);}return config;}public JedisPoolConfig createJedisPoolConfig(){JedisPoolConfig config = new JedisPoolConfig();//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认trueconfig.setBlockWhenExhausted(false);//设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");//是否启用pool的jmx管理功能, 默认trueconfig.setJmxEnabled(true);//MBean ObjectName = new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=" + "pool" + i); 默 认为"pool", JMX不熟,具体不知道是干啥的...默认就好.config.setJmxNamePrefix("pool");//是否启用后进先出, 默认trueconfig.setLifo(true);//最大空闲连接数, 默认8个config.setMaxIdle(2000);//最大连接数, 默认8个config.setMaxTotal(5000);//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1config.setMaxWaitMillis(10000);//逐出连接的最小空闲时间 默认1800000毫秒(30分钟)config.setMinEvictableIdleTimeMillis(1800000);//最小空闲连接数, 默认0config.setMinIdle(0);//每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3config.setNumTestsPerEvictionRun(3);//对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断  (默认逐出策略)   config.setSoftMinEvictableIdleTimeMillis(1800000);//在获取连接的时候检查有效性, 默认falseconfig.setTestOnBorrow(false);//在空闲时检查有效性, 默认falseconfig.setTestWhileIdle(false);//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1config.setTimeBetweenEvictionRunsMillis(-1);return config;}}

RedisService.java辅助类

package com.example.myframe.redis.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;@Service("redisService")
public class RedisService {@Autowiredprivate RedisTemplate redisTemplate;/*** 写入缓存* @param key* @param value* @return*/public boolean set(final String key, Object value) {boolean result = false;try {ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();operations.set(key, value);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** 写入缓存设置时效时间* @param key* @param value* @return*/public boolean set(final String key, Object value, Long expireTime) {boolean result = false;try {ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();operations.set(key, value);redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** 批量删除对应的value* @param keys*/public void remove(final String... keys) {for (String key : keys) {remove(key);}}/*** 批量删除key* @param pattern*/public void removePattern(final String pattern) {Set<Serializable> keys = redisTemplate.keys(pattern);if (keys.size() > 0){redisTemplate.delete(keys);}}/*** 删除对应的value* @param key*/public void remove(final String key) {if (exists(key)) {redisTemplate.delete(key);}}/*** 判断缓存中是否有对应的value* @param key* @return*/public boolean exists(final String key) {return redisTemplate.hasKey(key);}/*** 读取缓存* @param key* @return*/public Object get(final String key) {Object result = null;ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();result = operations.get(key);return result;}/*** 哈希 添加* @param key* @param hashKey* @param value*/public void hmSet(String key, Object hashKey, Object value){HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();hash.put(key,hashKey,value);}/*** 哈希 获取哈希的key集合* @param key* @return*/public Set<Object> hmKeys(String key){HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();return hash.keys(key);        }/*** 哈希 删除哈希的key* @param key* @param hashKey*/public void hmDelete(String key,String hashKey){HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();hash.delete(key, hashKey);       }    /*** 哈希获取数据* @param key* @param hashKey* @return*/public Object hmGet(String key, Object hashKey){HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();return hash.get(key,hashKey);}/*** 获取所有key值* @param key* @return*/public Set<Object>  hmKeySet(String key){HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();return hash.keys(key);	}/*** 获取所有key值* @param key* @return*/public void  hmRemove(String key, Object hashKey){HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();hash.delete(key, hashKey); 	}/*** 列表添加* @param k* @param v*/public void lPush(String k,Object v){ListOperations<String, Object> list = redisTemplate.opsForList();list.rightPush(k,v);}/*** 列表获取* @param k* @param l* @param l1* @return*/public List<Object> lRange(String k, long l, long l1){ListOperations<String, Object> list = redisTemplate.opsForList();return list.range(k,l,l1);}/*** 集合添加* @param key* @param value*/public void add(String key,Object value){SetOperations<String, Object> set = redisTemplate.opsForSet();set.add(key,value);}/*** 集合获取* @param key* @return*/public Set<Object> setMembers(String key){SetOperations<String, Object> set = redisTemplate.opsForSet();return set.members(key);}/*** 集合长度* @param key* @return*/public Long setSize(String key){SetOperations<String, Object> set = redisTemplate.opsForSet();return set.size(key);}/*** 集合获取* @param key* @param count* @return*/public Set<Object> setMembers(String key, int count){SetOperations<String, Object> set = redisTemplate.opsForSet();return set.distinctRandomMembers(key, count);}/*** 删除集合数据* @param key* @param value*/public void remove(String key, Object value){SetOperations<String, Object> set = redisTemplate.opsForSet();set.remove(key, value);}/*** 有序集合添加* @param key* @param value* @param scoure*/public void zAdd(String key,Object value,double scoure){ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();zset.add(key,value,scoure);}/*** 有序集合获取* @param key* @param scoure* @param scoure1* @return*/public Set<Object> rangeByScore(String key,double scoure,double scoure1){ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();return zset.rangeByScore(key, scoure, scoure1);}/*** 消息队列实现* @param channel* @param message*/public void convertAndSend(String channel, Object message){redisTemplate.convertAndSend(channel, message);}/*** 数列添加* @param key* @param value*/public void addList(String key,Object value){ListOperations<String, Object> list = redisTemplate.opsForList();list.rightPush(key, value);}/*** 数列获取* @param key* @return*/public List<Object> getList(String key){ListOperations<String, Object> list = redisTemplate.opsForList();return list.range(key, 0, list.size(key));}/*** 左弹出数列* @param key* @return*/public Object popList(String key) {ListOperations<String, Object> list = redisTemplate.opsForList();return list.leftPop(key);}public Long increment(String k, Long l) {return redisTemplate.opsForValue().increment(k, l);}}

TestRedisController.java

package com.example.myframe.controller;import com.example.myframe.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping(value = "/redis")
public class TestRedisController {@AutowiredRedisUtil redisUtil;/*** 生产者 通过此方法来往redis的list的尾部插入数据*/@RequestMapping("/shengchangzhe")public void shengChangZhe() {redisUtil.dealShengChangZhe();}/*** 消费者 ,通过此方法往redis的list的头部获取数据,直到list没有数据 阻塞,等到一有数据又继续获取*/@RequestMapping("/xiaoFeiZhe")public void xiaoFeiZhe() {redisUtil.dealXiaoFeiZhe();}/*** 发布者,发布信息,监听器一旦接收到监听,就进行操作*/@RequestMapping("/faBuDingYue")public void faBuDingYue() {redisUtil.dealFaBuDingYue();}}
package com.example.myframe.redis;import com.example.myframe.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;import java.util.List;@Component
public class RedisUtil {@AutowiredJedisCluster jedisCluster;@Autowiredprivate RedisService redisService;/*** 生产者*/public void dealShengChangZhe() {for (int i = 0; i < 10; i++) {jedisCluster.rpush("ceshi", "value1_" + i);}}/*** 消费者*/public void dealXiaoFeiZhe() {while (true) {//阻塞式brpop,List中无数据时阻塞,参数0表示一直阻塞下去,直到List出现数据List<String> listingList = jedisCluster.blpop(0, "ceshi");System.out.println("线程取数据:{}" + listingList.get(1));}}/*** 发布订阅模式*/public void dealFaBuDingYue() {redisService.convertAndSend("dealFaBuDingYue", "我是来发布信息的");}
}

redis消息队列监听信息

package com.example.myframe.redis.msg;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;@Configuration
public class RedisMsgListener {@AutowiredReceiver receiver;/*** redis消息队列监听信息* * @param connectionFactory* @param listenerAdapter* @return*/@BeanRedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.addMessageListener(listenerAdapter, new PatternTopic("dealFaBuDingYue"));return container;}/*** 监听方法* * @return*/@Bean(name = "listenerAdapter")MessageListenerAdapter listenerAdapter() {// 回调数据处理方法return new MessageListenerAdapter(receiver, "dealJt");}
}
package com.example.myframe.redis.msg;import com.example.myframe.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;@Service("receiver")
public class Receiver{@Autowiredprivate RedisService redisService;@Autowiredprivate StringRedisTemplate redisTemplate;/*** 清除外部广告位本地缓存* @param message*/public  void dealJt(String message){System.out.println("我是用来监听信息的");System.out.println(message);}/*** 清除外部广告位本地缓存* @param message*/public  void dealJt1(String message){System.out.println("我是用来监听信息的1");System.out.println(message);}}

这篇关于SpringBoot集成redis的JedisCluster和RedisTemplate 实现redis的消息队列消费者-生产者模式,订阅者发布者模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/400031

相关文章

Redis中6种缓存更新策略详解

《Redis中6种缓存更新策略详解》Redis作为一款高性能的内存数据库,已经成为缓存层的首选解决方案,然而,使用缓存时最大的挑战在于保证缓存数据与底层数据源的一致性,本文将介绍Redis中6种缓存更... 目录引言策略一:Cache-Aside(旁路缓存)策略工作原理代码示例优缺点分析适用场景策略二:Re

Flutter实现文字镂空效果的详细步骤

《Flutter实现文字镂空效果的详细步骤》:本文主要介绍如何使用Flutter实现文字镂空效果,包括创建基础应用结构、实现自定义绘制器、构建UI界面以及实现颜色选择按钮等步骤,并详细解析了混合模... 目录引言实现原理开始实现步骤1:创建基础应用结构步骤2:创建主屏幕步骤3:实现自定义绘制器步骤4:构建U

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St