您现在的位置是:Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款 >
01 redis消息訂閱與發布springboot(Spring Boot使用Redis進行消息的發布訂閱 原創)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款2024-07-06 09:06:11【】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
很赞哦!(992)
相关文章
- 01 拼多多海外版本簡介(揭秘!拼多多Temu出海項目正式上線,海外版本有何不同?)
- 01 拼多多海外店開通(拼多多怎么入駐海外?)
- 01 拼多多海外購可以退款嗎(拼多多的海外購物化妝品要填寫身份證號,不要了可以退款嗎?)
- 01 海南注冊外貿公司的優勢(海南大學好的專業)
- 01 按播放量結算好還是一口價好(抖音收益怎么算錢是按播放量算還是點贊一千萬播放量賺多少錢)
- 01 海南注冊外貿公司注冊資金最低多少(你好,注冊商貿公司與貿易公司納稅有什么不同哪個方便點望指教)
- 01 拿手機怎么發買粉絲(用手機可以發買粉絲內容嗎)
- 01 海南注冊外貿公司的條件及流程(如何申請進出口經營權)
- 01 拼多多海外直郵保真嗎(拼多多上海外直郵顯示物流信息嗎)
- 01 拼多多海外建倉(美股集體“啞火”倫鎳驚魂一夜!高瓴、橋水、景林“神同步”抄底一只股票)