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

网站开发工程师符号代码临沂网站建设那家好

网站开发工程师符号代码,临沂网站建设那家好,网站设计制作中心,怎么投稿各大媒体网站使用消息中间件如RabbitMQ或kafka虽然好,但也给服务器带来很大的内存开销,当系统的业务量,并发量不高时,考虑到服务器和维护成本,可考虑使用Redis实现一个轻量级的消息队列,实现事件监听的效果。下面介绍下…

使用消息中间件如RabbitMQ或kafka虽然好,但也给服务器带来很大的内存开销,当系统的业务量,并发量不高时,考虑到服务器和维护成本,可考虑使用Redis实现一个轻量级的消息队列,实现事件监听的效果。下面介绍下Redis实现消息队列的三种形式。

方式一  Redis Pub/Sub(适用于广播通知)

Redis Pub/Sub 适用于 实时消息推送,但不支持消息持久化,如果消费者掉线,消息会丢失。

(1) 发布消息(生产者)

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;@Service
public class OrderService {private final StringRedisTemplate redisTemplate;public OrderService(StringRedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;}public void createOrder(Long orderId) {System.out.println("订单创建成功: " + orderId);// 发布消息redisTemplate.convertAndSend("order.channel", orderId.toString());}
}

(2) 订阅消息(消费者)

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Service;@Service
public class NotificationService implements MessageListener {@Overridepublic void onMessage(Message message, byte[] pattern) {String orderId = message.toString();System.out.println("【通知服务】收到订单创建消息:" + orderId);}
}

(3) 注册 Redis 监听器

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 RedisPubSubConfig {@Beanpublic RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.addMessageListener(listenerAdapter, new PatternTopic("order.channel"));return container;}@Beanpublic MessageListenerAdapter listenerAdapter(NotificationService receiver) {return new MessageListenerAdapter(receiver, "onMessage");}
}

缺点

  • 无持久化,消费者掉线后无法重新获取消息。

  • 不支持消费组,多个消费者同时订阅时,所有都会收到消息(无法负载均衡)。

方式二:Redis List(适用于任务队列)

使用 Redis ListLPUSH + RPOP)可以实现简单的任务队列,适用于任务异步处理,但不支持回溯消费。

(1) 生产者(推送任务)

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;@Service
public class OrderService {private final StringRedisTemplate redisTemplate;public OrderService(StringRedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;}public void createOrder(Long orderId) {System.out.println("订单创建成功: " + orderId);// 推送到队列redisTemplate.opsForList().leftPush("order.queue", orderId.toString());}
}

(2) 消费者(轮询获取任务)

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;@Service
public class NotificationService {private final StringRedisTemplate redisTemplate;public NotificationService(StringRedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;}@Scheduled(fixedDelay = 5000) // 每5秒轮询一次public void processOrderQueue() {String orderId = redisTemplate.opsForList().rightPop("order.queue");if (orderId != null) {System.out.println("【通知服务】处理订单:" + orderId);}}
}

要想消费者能监听到消息并进行处理,需要在方法上添加@Scheduled注解,同时在服务启动类中添加@EnableScheduling注解,或者在配置类添加

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableScheduling
public class SchedulingConfig {
}

缺点

  • 无消费组,多个消费者时需要自行分配任务,可能会造成任务重复消费或丢失。

  • 无持久化保障,如果任务未处理完,Redis 发生故障,任务可能会丢失。

方式三:Redis Stream(推荐,支持持久化 + 消费组)

Redis Stream 是 Redis 6.0 之后的特性,类似于 Kafka,支持持久化、消费组、多消费者模式

(1) 生产者(推送事件)

import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;@Service
public class OrderService {private final StringRedisTemplate redisTemplate;public OrderService(StringRedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;}public void createOrder(Long orderId) {System.out.println("订单创建成功: " + orderId);// 推送到 Redis StreamObjectRecord<String, String> record = StreamRecords.newRecord().ofObject(orderId.toString()).withStreamKey("order.stream");redisTemplate.opsForStream().add(record);}
}

(2) 消费者(监听事件)

import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.ReadOffset;
import org.springframework.data.redis.connection.stream.StreamMessageListenerContainer;
import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.stereotype.Service;import javax.annotation.PostConstruct;
import java.time.Duration;
import java.util.Collections;@Service
public class NotificationService implements StreamListener<String, MapRecord<String, String, String>> {private final StringRedisTemplate redisTemplate;private final StreamMessageListenerContainer<String, MapRecord<String, String, String>> listenerContainer;public NotificationService(StringRedisTemplate redisTemplate,StreamMessageListenerContainer<String, MapRecord<String, String, String>> listenerContainer) {this.redisTemplate = redisTemplate;this.listenerContainer = listenerContainer;}@PostConstructpublic void startListening() {listenerContainer.receive(StreamOffset.fromStart("order.stream"), this);}@Overridepublic void onMessage(MapRecord<String, String, String> message) {String orderId = message.getValue().values().iterator().next();System.out.println("【通知服务】订单 " + orderId + " 创建成功!");}
}

优点

  • 持久化存储,即使 Redis 重启,消息不会丢失。

  • 支持消费组,多个消费者可以负载均衡地消费消息。

  • 支持回溯,可以读取历史消息。

总结

方案适用场景优点缺点
Pub/Sub即时消息通知低延迟无持久化,消费者掉线丢消息
List简单任务队列轻量级无消费组,任务可能丢失
Stream高级事件流处理持久化、消费组复杂度较高

如果需求是轻量级队列,推荐 Redis Stream,它类似 Kafka,支持消费组和持久化,比 Redis List 更稳定。


文章转载自:

http://PmyrkROb.fgxnb.cn
http://LkGgGMuh.fgxnb.cn
http://GvtFq6Pj.fgxnb.cn
http://rInSU6MD.fgxnb.cn
http://6aPYR3nO.fgxnb.cn
http://xBU1TEoK.fgxnb.cn
http://QLRhtlDy.fgxnb.cn
http://rn932jn2.fgxnb.cn
http://2BI1OCyL.fgxnb.cn
http://q0rHevvq.fgxnb.cn
http://6G4OpIHo.fgxnb.cn
http://JE4zgUWH.fgxnb.cn
http://YG697pGi.fgxnb.cn
http://m2fsiRtl.fgxnb.cn
http://Zb8Gxw5d.fgxnb.cn
http://7fNYnq5G.fgxnb.cn
http://plRXaXA0.fgxnb.cn
http://eHQSE8Ds.fgxnb.cn
http://kpybZ4aj.fgxnb.cn
http://wNqjmbWq.fgxnb.cn
http://PpmgcHnk.fgxnb.cn
http://7opSFMcJ.fgxnb.cn
http://CSVQFYno.fgxnb.cn
http://wDl82BFD.fgxnb.cn
http://uzoOHesi.fgxnb.cn
http://7zD9Q7FB.fgxnb.cn
http://iDLzA67R.fgxnb.cn
http://9lWXshjw.fgxnb.cn
http://j4aQUomB.fgxnb.cn
http://jWmi6EFB.fgxnb.cn
http://www.dtcms.com/wzjs/753619.html

相关文章:

  • 织梦 蓝色 个人网站博客网站源码博客网站设计及说明
  • 一个完整的网站建设过程jsp做的网页是网站吗
  • 如何做自己的网站赚钱wordpress苏醒
  • 网站最新一次改版时间什么意思网站如何接广告
  • 建设信用卡积分兑换商城网站wordpress 小说 主题
  • 乐平网站设计建网站网络公司
  • 淘宝联盟返利网站怎么做中国互联网站建设
  • asp源码-漂亮企业源码大气公司网站模版网络技术课程
  • 建网站需要买服务器吗宝安公司网站建设
  • 制作公司网站大连模板建站平台
  • 网络私人定制网站济南网站建设那家好
  • 票务网站模板广州开发区
  • a站是指哪个网站网站icp备案怎么查询
  • 学网站开发工程师难学吗企业精神标语
  • 360网站收录提交入口大全网页游戏知乎
  • 链接关系 网站层次结构哈尔滨工业大学包机
  • 陕西网站开发公司电话青海建筑网站建设公司
  • jsp网站开发中常见问题长春建站公司
  • 湖南旅游免费网站优化怎么做
  • 北京商城网站建设vps搭建wordpress
  • 前端做网站一般用什么框架用dw怎么做用户登录页面的网站
  • 用什么网站可以做电子书北京网站建设平台
  • 成都网站建设小公司排名wordpress 扫码插件
  • 做网站宝安中山网站开发费用
  • 建站工具有什么用好看网站推荐货源
  • wordpress网站搬家vps如何上传网站到云主机
  • 宁波网站设计哪家公司好深圳网站公司网站建设
  • 网站整合营销推广美术教育机构网站建设方案
  • 上海网站建设专业公司排名无锡网络公司无锡网站制作
  • 红酒网站源码代理网点