您现在的位置是:Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款 >
01 redis消息訂閱與發布springboot(Spring+Boot對Redis整合時,需要添加對應的依賴啟動器是什么?)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款2024-05-30 19:16:33【】5人已围观
简介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+Boot對Redis整合時,需要添加對應的依賴啟動器是什么?
在 Spring Boot 中使用 Redis,需要添加以下依賴啟動器:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
這個依賴啟動器包含了 Spring Data Redis 客戶端庫,可以方便地使用 Redis。
在添加了這個依賴啟動器之后,需要在 `application.properties` 或 `application.yml` 配置文件中添加 Redis 的配置信息,例如:
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=yourpassword
這個配置文件中指定了 Redis 服務器的地址、端口和密碼。如果 Redis 服務器沒有設置密碼,可以省略 `spring.redis.password` 配置項。
在配置文件中添加了 Redis 的配置信息之后,就可以在 Spring Boot 應用程序中使用 Redis 了。可以使用 `@Autowired` 注解注入 `RedisTemplate` 或 `StringRedisTemplate` 對象,然后使用這些對象操作 Redis 數據庫。
例如,以下代碼演示了如何使用 `StringRedisTemplate` 對象向 Redis 中寫入和讀取數據:
@RestController
public class RedisController {
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping("/redis/set")
public String setRedisValue() {
redisTemplate.opsForValue().set("name", "Tom");
return "Set Redis Value Success";
}
@GetMapping("/redis/get")
public String getR
很赞哦!(1)
相关文章
- 03 youtube官網網頁版打開速度很慢上傳速度特別(明星玩的像電腦樣直板的那是什么(就觸屏的,像電腦,但卻是像一塊板,平 平的) 可以用來做什么???)
- download video youtube savefrom downloader(優酷視頻怎么樣下載到電)
- 03 youtube官網網頁版打開網頁怎么關閉快捷功能(Mac無法用蘋果耳機控制音樂播放。 使用網易云音樂的時候按耳機的暫停鍵會打開iTunes。如何更改設置?)
- 03 youtube官網網頁版更新不存在就新(在Ubuntu上有什么必裝的實用軟件?)
- 03 youtube官網網頁版打開速度慢是因為(明星玩的像電腦樣直板的那是什么(就觸屏的,像電腦,但卻是像一塊板,平 平的) 可以用來做什么???)
- download youtube music for windows(如何在youtubemusic中創建快捷指令)
- download youtube music to windows 1(今日新聞淺談:Youtube Music 也加入串流音樂服務大混戰)
- download youtube app for iphone(對于ios8的系統,有哪些插件不兼容)
- download video youtube ios17描述文件下載(ios17怎么下載安裝)
- 03 youtube官網網頁版打開速度特別慢上傳正常(網絡推廣100種方法有哪些)
热门文章
站长推荐
03 youtube官網網頁版打開速度測試工具哪個最好(我用的是Hotspot Shield Launch1.57版本,想去YOUTUBE看視頻,顯示已連接,但就是不能打開任何網頁,急求)
download youtube music app to computer(今日新聞淺談:Youtube Music 也加入串流音樂服務大混戰)
download video youtube ios17 雙開(ios17支持雙開嗎)
download youtube for win 10 64 bit(win10用MicrosoftEdge瀏覽器瀏覽YouTube速度很慢怎么辦)
03 youtube官網網頁版打開速度很慢上傳正常(網絡推廣100種方法有哪些)
download youtube music to windows(youtube music下載的文件在哪里)
download youtube music to android sdk(如何在youtubemusic中創建快捷指令)
download youtube music macos(有沒有抖音剪輯培訓教程,還有抖音書單培訓教程,書單文案如何原創?)