springboot集成redis實(shí)現(xiàn)消息的訂閱與發(fā)布
前言
本節(jié)內(nèi)容主要介紹springboot項(xiàng)目通過集成redis,如何利用redis的訂閱發(fā)布機(jī)制,完成系統(tǒng)消息的發(fā)布與訂閱功能。Redis中的發(fā)布與訂閱是一種消息通信模式,允許發(fā)送者(發(fā)布者)將消息發(fā)送給多個(gè)接收者(訂閱者)。在 Redis中,發(fā)布與訂閱通過PUBLISH和SUBSCRIBE命令實(shí)現(xiàn)。頻道(Channel):頻道是消息的通道,用于區(qū)分不同類型或主題的消息。訂閱者可以選擇訂閱感興趣的頻道,以接收相應(yīng)的消息。Redis的發(fā)布與訂閱模式是無狀態(tài)的,即發(fā)布者在發(fā)送消息之后不需要關(guān)心是否有訂閱者接收到消息,也不需要維護(hù)訂閱者的信息。當(dāng)發(fā)布者向某個(gè)頻道發(fā)布消息時(shí),所有訂閱了該頻道的訂閱者都會(huì)接收到相同的消息。這種機(jī)制使得消息的發(fā)布者和訂閱者之間能夠?qū)崿F(xiàn)解耦,并支持一對多的消息傳遞方式,即廣播形式。
正文
①創(chuàng)建一個(gè)web項(xiàng)目,引入redis啟動(dòng)器的pom依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
② 在application.yml中添加redis的配置
③創(chuàng)建redis的配置類, 初始化redis工具類RedisTemplate和redis訂閱消息的監(jiān)聽容器RedisMessageListenerContainer
package com.yundi.atp.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { /** * 初始化一個(gè)Redis消息監(jiān)聽容器 * @param connectionFactory * @return */ @Bean public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); // 添加其他配置,如線程池大小等 return container; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); redisTemplate.setDefaultSerializer(new StringRedisSerializer()); return redisTemplate; } }
④創(chuàng)建redis消息頻道的常量
package com.yundi.atp.constant; public class ChannelConstant { /** * 廣播通道 */ public static final String CHANNEL_GLOBAL_NAME = "channel-global"; /** * 單播通道 */ public static final String CHANNEL_SINGLE_NAME = "channel-single"; }
⑤ 創(chuàng)建一個(gè)http請求,用于發(fā)布基于redis的消息供客戶端訂閱
package com.yundi.atp.controller; import com.yundi.atp.constant.ChannelConstant; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RequestMapping(value = "base") @RestController public class BaseController { @Resource private RedisTemplate redisTemplate; /** * 發(fā)布廣播消息 * * @param msg */ @GetMapping(value = "/publish/{msg}") public void sendMsg(@PathVariable(value = "msg") String msg) { redisTemplate.convertAndSend(ChannelConstant.CHANNEL_GLOBAL_NAME, msg); } }
⑥ 創(chuàng)建一個(gè)消息訂閱者,實(shí)現(xiàn)MessageListener接口,通過重寫onMessage方法訂閱消息
package com.yundi.atp.listen; import com.yundi.atp.constant.ChannelConstant; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.nio.charset.StandardCharsets; @Slf4j @Component public class RedisMessageSubscriber implements MessageListener { @Autowired private RedisMessageListenerContainer redisMessageListenerContainer; /** * 訂閱消息:將訂閱者添加到指定的頻道 */ @PostConstruct public void subscribeToChannel() { //廣播消息 redisMessageListenerContainer.addMessageListener(this, new ChannelTopic(ChannelConstant.CHANNEL_GLOBAL_NAME)); } @Override public void onMessage(Message message, byte[] bytes) { String channel = new String(message.getChannel(), StandardCharsets.UTF_8); String messageBody = new String(message.getBody(), StandardCharsets.UTF_8); log.info("Received message: " + messageBody + " from channel: " + channel); } }
⑦啟動(dòng)項(xiàng)目,通過http請求發(fā)布消息,查看是否能夠訂閱成功消息
⑧開啟redis客戶端測試,同樣能夠訂閱到消息,證明redis的消息的訂閱與發(fā)布是無狀態(tài)的且是廣播模式
到此這篇關(guān)于springboot集成redis實(shí)現(xiàn)消息的訂閱與發(fā)布的文章就介紹到這了,更多相關(guān)springboot redis消息訂閱與發(fā)布內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot集成Redis實(shí)現(xiàn)消息隊(duì)列的方法
- SpringBoot+Redis實(shí)現(xiàn)消息的發(fā)布與訂閱的示例代碼
- SpringBoot中使用Redis?Stream實(shí)現(xiàn)消息監(jiān)聽示例
- springboot整合redis之消息隊(duì)列
- SpringBoot整合Redis實(shí)現(xiàn)消息發(fā)布與訂閱的示例代碼
- SpringBoot使用Redis實(shí)現(xiàn)消息隊(duì)列的方法小結(jié)
- SpringBoot集成Redisson實(shí)現(xiàn)消息隊(duì)列的示例代碼
- Springboot3+Redis實(shí)現(xiàn)消息隊(duì)列的多種方法小結(jié)
相關(guān)文章
java使用wait和notify實(shí)現(xiàn)線程通信
這篇文章主要為大家詳細(xì)介紹了java如何使用wait和notify實(shí)現(xiàn)線程之間通信,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10JAVA面試題 簡談你對synchronized關(guān)鍵字的理解
這篇文章主要介紹了JAVA面試題 請談?wù)勀銓ychronized關(guān)鍵字的理解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07在Java的Struts框架中ONGL表達(dá)式的基礎(chǔ)使用入門
這篇文章主要介紹了深入解析在Java的Struts框架中ONGL表達(dá)式的基礎(chǔ)使用入門,Struts框架是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-11-11一文探索Apache HttpClient如何設(shè)定超時(shí)時(shí)間
Apache HttpClient是一個(gè)流行的Java庫,用于發(fā)送HTTP請求,這篇文章主要為大家介紹了Apache HttpClient如何設(shè)定超時(shí)時(shí)間,感興趣的小伙伴可以學(xué)習(xí)一下2023-10-10sql于navicat中能運(yùn)行在mybatis中不能運(yùn)行的解決方案
這篇文章主要介紹了sql于navicat中能運(yùn)行在mybatis中不能運(yùn)行的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01log4j2 RollingRandomAccessFile配置過程
這篇文章主要介紹了log4j2 RollingRandomAccessFile配置過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07