您现在的位置是:Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款 >
01 redis消息訂閱與發布springboot(Spring+Boot對Redis整合時,需要添加對應的依賴啟動器是什么?)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款2024-07-21 04:20:22【】8人已围观
简介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
很赞哦!(74)
相关文章
- 為什么掛梯子看不了tiktok(2022最新版從0-1運營TikTok的保姆級教程) 01
- 做tiktok用什么手機最好(TikTok運營手機選擇是選擇蘋果還是安卓?root后安卓手機...) 01
- 哪些買粉絲可以看tiktok(為什么tiktok掛了買粉絲也看不了) 01
- 中國人怎么用tiktok(tiktok視頻全是國人咋整) 01
- 免費的tiktok郵箱賬號(tiktok賬號能看到注冊郵箱嗎) 01
- 01 03 幫粉絲砍價買新車豐田(一汽豐田凱美瑞2022怎么砍價)
- 01 03 幫美女粉絲買車(2022抖音熱門曬提車的秒贊句子五十句)
- 01 03 去珠寶店買首飾結果看到了粉絲(憑什么不是真金白銀的CHANEL配飾,有這么多人愿意買單)
- 01 03 巧遇粉絲買車(講笑話的)
- 01 03 帶粉絲買奧迪a4l新款(奧迪a4l粉色多少錢)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款的名片
职业:程序员,设计师
现居:贵州黔东南麻江县
工作室:小组
Email:[email protected]