您现在的位置是:Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款 >
01 redis的發布訂閱(如何在SQLServer2000中配置發布、分發和訂閱服務)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款2024-07-18 11:55:05【】9人已围观
简介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實際上就是一個消息列表,只是他幾乎
很赞哦!(13615)
相关文章
- 01 中郵海外購稅率(用過中郵海外購轉運的說下,如何才能不被稅)
- 01 youtube dl不能用了還有嗎(國內哪些地方可以沖浪)
- 01 youtube download mp3 player apk(復制網上的視頻(VIDEO)方法)
- 01 youtube downloader hd 中文版(Mac上好用的視頻下載工具求大神推薦!)
- 01 youtube dl不能用了大家都(怎樣從網站上下載在線播放的視頻)
- 01 youtube for tv apk download app(smartyoutubetv目前還能用嗎)
- 01 中視頻伙伴計劃沒有播放量(中視頻我按照規定發布播放量有1萬為什么不記錄中視頻)
- 01 中鐵四局海外事業部劉(中鐵四局海外事業部怎么樣)
- 01 中視頻計劃的播放量怎么計算(中視頻計劃抖音播放量算嗎)
- 01 中鐵十四局海外項目招聘(中鐵十四局好不好待遇怎么樣啊)
热门文章
站长推荐
01 youtube for android tv apk(Android設代理通過webView連接Gmail登錄時,怎么除掉“loading,please wait...”提示空白頁的顯示)
01 youtube download mp3 player download(如何下載)
01 youtube download windows 11 64 bit(手機錄像相機軟件哪個好)
01 中越貿易公司怎樣經營(華人在越南的處境怎樣)
01 youtube diy tools to make at home(手工制作用英語怎么說翻譯及閱讀)
01 中鐵中建國際貿易有限公司官網(中鐵中建國際貿易有限公司是詐騙嗎)
01 youtube for music 買粉絲s 2021(幫忙翻譯一段IPHONE廣告)
01 中視頻計劃快速17000的播放量(頭條發視頻算在中視頻申請計劃的17000里嗎?)