您现在的位置是:Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款 >
01 redis消息訂閱與發布springboot(Spring Boot使用Redis進行消息的發布訂閱 原創)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款2024-07-21 00:42:53【】6人已围观
简介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
很赞哦!(41)
相关文章
- 01 我國的海外利益有哪些(中國海外利益包括什么內容)
- 01 我國海外安保行業現狀(安防行業目前面對著怎樣的產業環境?(安防產業的發展現狀及趨勢))
- 01 粉絲不買東西就生氣(一個女孩是個主播,好幾百萬粉絲,每天很多人和自己聊天,就因為沒搭理他,就生氣?還賴女主播不好)
- 01 粉絲20w買車(網上很多的車評人,都會幫粉絲低價買車,他們是在作秀嗎?)
- 01 簡述貿易術語的作用(試簡述《2010年國際貿易術語解釋通則》中,FOB和CIF術語下買賣雙方的權利和義務包括哪幾項?)
- 01 籃球賽買粉絲推文怎么寫(買粉絲推文怎么寫)
- 01 我國一家外貿公司與某外商簽訂了(如果你是某公司的業務員,對外進行貿易往來,如何簽訂合同。假設合同規定以L/C方式付款,簡述流程并解釋)
- 01 粉絲不愿買電影票(你覺得演唱會門票不允許退換合理嗎?)
- 01 我國國際貿易的現狀和發展調查報告(國際貿易調查報告)
- 01 我國的監管機構對中資企業海外(有哪些因素影響跨國公司在海外設立分子機構的形式?不同的形式對母國,東道國影響何在,區別在哪里?)