您现在的位置是:Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款 >
01 redis的發布訂閱機制(《Redis設計與實現》pdf下載在線閱讀,求百度網盤云資源)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款2024-06-17 12:16:31【】4人已围观
简介SpringBoot使用Redis進行消息的發布訂閱原創/***Redis相關的配置,包含推送,以及對象編碼的定義*/@ConfigurationpublicclassRedisConfig{publ
Spring Boot使用Redis進行消息的發布訂閱 原創
/
*** Redis 相關的配置,包含推送,以及對象編碼的定義
*/
@Configuration
public class RedisConfig {
public @Bean
RedisTemplate redisTemplate(RedisConnectionFactory 買粉絲nnectionFactory) {
RedisTemplate template = new RedisTemplate>();
template.setConnectionFactory(買粉絲nnectionFactory);
template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
@Bean
ChannelTopic topic() {
return new ChannelTopic("messageQueue");
}
@Bean
RedisMessageListenerContainer 買粉絲ntainer(RedisConnectionFactory 買粉絲nnectionFactory,
MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer 買粉絲ntainer = new RedisMessageListenerContainer();
買粉絲ntainer.setConnectionFactory(買粉絲nnectionFactory);
買粉絲ntainer.addMessageListener(listenerAdapter, new PatternTopic("messageQueue"));
return 買粉絲ntainer;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
}
public enum BusinessTypeEnum {
//修改SystemConfig緩存
UPDATE_SYSTEM_CONFIG;
}
//發布者接口
public interface MessagePublisher {
void publish(String message);
}
@Component
public class RedisMessagePublisher implements MessagePublisher{
private final static Logger logger = LoggerFactory.getLogger(RedisMessagePublisher.class);
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ChannelTopic topic;
@Override
public void publish(String message) {
logger.info("推送信息:"+message);
redisTemplate.買粉絲nvertAndSend(topic.getTopic(), message);
}
}
public interface Receiver { void receiveMessage(String message); }
@Component
public class MessageReceiver implements Receiver {
private final static Logger logger = LoggerFactory.getLogger(MessageReceiver.class);
@Autowired
private ISystemConfigService systemConfigService;
@Override
public void receiveMessage(String message) {
logger.info("消息接收:"+message);
JSONObject object = JSONObject.parseObject(message);
String businessType = object.getString("businessType");
logger.info("處理業務類型:businessType="+businessType);
if(BusinessTypeEnum.UPDATE_SYSTEM_CONFIG.toString().equals(businessType)){ //處理SystemConfig緩存
String jsonStr = object.getString("jsonStr");
if(jsonStr!=null&&!"".equals(jsonStr)){
JSONObject json = JSONObject.parseObject(object.getString("jsonStr"));
for(String key:json.keySet()){
String value = json.getString(key);
SystemConfig systemConfig = SystemConfigContainer.instance().getSystemConfig(key);
if(systemConfig!=null){
systemConfig.setKeyValue(value);
SystemConfigContainer.instance().uptSystemConfig(systemConfig);
}else{
systemConfig = systemConfigService.selectByPrimaryJianMing(key);
systemConfig.setKeyValue(value);
SystemConfigContainer.instance().uptSystemConfig(systemConfig);
}
}
}
}
}
}
Redis發布訂閱和Stream
發布訂單系統是日常開發中經常會用到的功能。簡單來說,就是發布者發布消息,訂閱者就會接受到消息并進行相應的處理,如下圖所示。
Redis為我們提供了發布/訂閱的功能模塊PubSub,可以用于消息傳遞。
其中發布者publisher、訂閱者subscriber都是redis客戶端,channel則是redis服務器。
發布者publisher向channel發送消息,訂閱該channel的subscriber就會接收到消息。
發布消息publish
訂閱test1、test2的客戶端會收到消息
按照上述這種方式,如果 訂閱者subscriber想要訂閱多個channel 則需要同時指定多個channel的名稱,redis為了解決這個問題提供 psubscribe模式匹配 這種訂閱方式,可以通過通配符的方式匹配頻道。
發布消息
之前訂閱ch*的客戶端就會收到cha頻道和買粉絲頻道的消息,這樣就一次性訂閱多個頻道
redis服務端存儲了訂閱頻道/模式的客戶端列表
相當于如果客戶端訂閱一個頻道 ,那么服務端的 pubsub_channels 就會存儲一條數據, pubsub_channels 其實是一個鏈表,key對應channel,value對應客戶端列表,根據key訂閱的頻道,就可以找到訂閱該頻道的所有客戶端。
同時如果客戶端訂閱一個模式 , pubsub_patterns 也會新增一條數據,記錄當前客戶端訂閱的模式, pubsub_patterns 也有自己的數據結構,其中就包含了客戶端以及模式。
當發布者向某個頻道發布消息時,就會遍歷 pubsub_channels 找到訂閱該頻道的客戶端列表,依次向這些客戶端發送消息。
然后遍歷 pubsub_patterns 找到符合當前頻道的模式,同時找到模式對應的客戶端,然后向客戶端發送消息。
雖然Redis提供了發布/訂閱的功能,但是并不完善,導致基本沒有合適的場景能夠使用。
PubSub缺點:
直到Redis5.0出現之后,出現了Stream這種數據結構,才終于完善了Redis的消息機制 。
Stream實際上就是一個消息列表,只是他幾乎
很赞哦!(5157)
相关文章
- 01 中銀香港接收海外匯款(從香港中國銀行手機轉轉到香港匯豐銀行要多長時間才到帳)
- 04 一般貿易模式的流程圖(淘寶上購物需要提供身份證進行物品清關嗎?)
- 01 臨沂大學國際經濟與貿易專業課程有哪些(請問一下臨沂大學的專科專業都有哪些)
- 01 丹東社保中心買粉絲(丹東銀行社保卡怎么用手機認證)
- 04 上市公司海外并購投資回暖(中國對外商股權限制)
- 01 丹索國際貿易有限公司(廣州分公司)(介紹世界名城)
- 04 youtube軟件下載網站免費4k電腦壁紙(電腦不用軟件怎么錄屏)
- 01 臨沂大學國際貿易專升本在哪個校區(臨沂大學專升本國際經濟與貿易在哪個校區2022)
- 01 臨沂蘭山打卡必去網紅景點(臨沂蘭山網紅打卡景點)
- 01 丹索國際貿易有限公司(廣州分公司)(介紹世界名城)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款的名片
职业:程序员,设计师
现居:吉林松原前郭尔罗斯蒙古族自治
工作室:小组
Email:[email protected]
热门文章
站长推荐
04 上海世外教育附屬新吳實驗學校(上海虹口區新市南路1501 屬于哪個居委會? 就近上學的對口小學有哪幾所?對口中學有哪幾所?是否是重點?)
04 youtube音頻下載教程(一般什么視頻剪輯軟件好用,有誰知道Youtube達人一般用什么剪輯軟件制作的視頻。)
04 youtube軟件下載apk文件打不開機了(為什么Android系統應用更新不是讓新apk替換進入/system分區)
04 youtube軟件下載方法插件(日本人看視頻用什么軟件)
01 臨沂的網紅打卡圣地(青島十大網紅景點 青島十大網紅打卡圣地)
01 中韓貿易主要商品(誰知道中韓貿易需要什么流程?懂得來,謝謝了)
04 上海世外小學事件一位母親的信(要小學的關于孫悟空的文章)
04 上海一列國際貿易有限公司(上海國際貿易有限公司怎么樣)