您现在的位置是:Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款 >
01 redis消息訂閱與發布springboot(Spring+Boot對Redis整合時,需要添加對應的依賴啟動器是什么?)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款2024-07-21 00:35:24【】7人已围观
简介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
很赞哦!(96)
相关文章
- 01 夢見發年貨代表什么意思(夢到喜歡的人是什么意思)
- 01 梅州市黃塘醫院預約掛號買粉絲公眾(請梅州市區的朋友們幫幫忙! 梅州火車站→梅州市人民醫院(黃塘醫院)怎樣坐公交車?)
- 05 youtube官網中文版更新后怎么更新到(PS軟件各個版本區別大嗎?哪個版本最適合新手?)
- 05 youtube官網中文版更新后進不去游戲界面(電腦不用軟件怎么錄屏)
- 01 櫥窗帶貨簡介怎么更吸引人呢(抖音賣貨簡介怎么寫吸引粉絲)
- 01 夢見好多年貨好不好(夢見了置辦年貨的預兆)
- 01 夢見自己遇到網紅預兆什么(夢見自己見到網紅了?)
- 05 youtube官網中文版更新失敗啟動失敗登錄失敗(有沒有 不用ITUNES 直接刷機的辦法啊)
- 05 youtube官網中文版更新到什么版本最好用(怎樣才能學好python語言?)
- 05 youtube官網中文版更新到多少集到多少(如何上聽力公開課)
热门文章
站长推荐
05 youtube官網中文版更新后進不去又還(韓庚...................)
01 模組訂閱后怎么下載(steam創意工坊里的mod下載后在哪)
05 youtube官網中文版更新后怎么關機電腦(you tu be的官網是多少?)
01 夢到置辦年貨是什么意思(夢見過年什么都沒買是什么意思?)
01 樓梯網紅打卡設計(重慶的網紅居民樓白象居,24層高卻沒電梯,它有什么特色?)
05 youtube官網中文版注冊版能玩什么(我現在人在國外 怎么解除限制 看內地網站的視頻啊)
05 youtube官網中文版更新到哪個版本最好哪個(常用的數據可視化軟件有哪些)
05 youtube官網中文版更新時間是否在一個(you tu be的官網是多少?)