您现在的位置是:Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款 >
01 redis發布訂閱模式配置(Redis中的哨兵模式)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款2024-07-14 06:03:21【】2人已围观
简介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);
}
}
}
}
}
}
spring-redis 發布訂閱模式:發布一條消息收到了兩條,重復監聽
當你看到這個文章時,想必已經了解了一些關于spring-redis發布訂閱方面的知識,如果你也遇到同樣的問題
那么請考慮spring在啟動時有沒有重復的加載配置文件
具體的方式可以在 AbstractApplicationContext 的 refresh() 方法中打斷點,如果走了兩次,說明配置文件加載了兩遍
而配置文件加載兩遍的原因是因為web.xml中DispatcherServlet和ContextLoaderLinistener 共用了 某些配置文件導致的。
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 也有自己的數據結構,其中就包含了客戶端以及模式。
很赞哦!(15)
相关文章
- 01 貴陽網紅書店鐘書閣地址(椒江網紅書店鐘書閣能坐地鐵到嗎?)
- 06 校運會買粉絲開頭結尾(各個省高考優秀作文開頭結尾整合)
- 06 杭州財賽貿易有限公司招聘(優秀公司是如何招人選人用人的?)
- 06 杭州貿易有限公司排名(物流公司排名前十)
- 06 某a公司向g貿易公司出口一批貨物(國際結算的案例)
- 01 貴煙買粉絲叫什么(爆珠煙都有哪些?)
- 01 貴陽地鐵網紅打卡點(貴陽網紅打卡景點在哪里)
- 06 根據世界貿易組織的界定服務貿易共有十大領域包括(我國加入世界貿易組織后當前服務貿易自由化的概況是什么?)
- 01 貴陽威尼斯網紅街在哪里(貴陽網紅街在哪里)
- 01 貴州網紅劉凱哥身高(劉凱哥現在很有錢嗎)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款的名片
职业:程序员,设计师
现居:新疆阿克苏柯坪县
工作室:小组
Email:[email protected]
热门文章
站长推荐
01 貴陽市北京路地鐵站網紅街(貴陽網紅打卡景點在哪里)
01 貴州省人民醫院買粉絲預約掛號和現場掛號是一樣的嗎(買粉絲掛號后怎么取號 買粉絲掛號后取號的方法)
01 貴陽公積金買粉絲買粉絲密碼幾位數(買粉絲關注公積金買粉絲,登錄不起。輸入身份證密碼后,顯示已買粉絲登錄,進不去了,咋會事?)
06 河北經貿大學的貿易經濟是冷門專業嗎(河北經貿大學經濟管理學院的國際經濟與貿易專業到底怎么樣?)
06 河南如達貿易有限公司(濟源144家企業擁有進出口經營資格,求這114家名單!)
06 杭州財升貿易有限公司(浙江專升本有哪些民辦學校?)
06 江西道順貿易有限公司(請問江西專升本有哪些民辦學校?)
01 貴州省人民醫院買粉絲查不到檢驗報告(怎么在醫院的買粉絲上查不到報告)