日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

Redis發布訂閱,右手就行!

來源: 責編: 時間:2024-01-10 09:35:17 207觀看
導讀哈嘍,大家好,我是了不起。Redis平常作為緩存使用較多,但是也可以作為發布訂閱的消息隊列來使用,本篇給大家介紹一下如何簡單使用!右手就能操作前言本篇我們會使用Spring Data Redis中集成的發布訂閱功能來展示這個示例,先看

哈嘍,大家好,我是了不起。2pU28資訊網——每日最新資訊28at.com

Redis平常作為緩存使用較多,但是也可以作為發布訂閱的消息隊列來使用,本篇給大家介紹一下如何簡單使用!右手就能操作2pU28資訊網——每日最新資訊28at.com

前言

本篇我們會使用Spring Data Redis中集成的發布訂閱功能來展示這個示例,2pU28資訊網——每日最新資訊28at.com

先看我們需要的依賴, 其實只需要引入spring-boot-starter-data-redis 就夠了,另外再寫一個接口來觸發消息發布。2pU28資訊網——每日最新資訊28at.com

<dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-data-redis</artifactId>  </dependency>  <dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-webflux</artifactId>  </dependency>

Spring Data 為 Redis 提供了專用的消息傳遞集成,其功能和命名與 Spring Framework 中的 JMS 集成類似。2pU28資訊網——每日最新資訊28at.com

Redis 消息傳遞大致可分為兩個功能領域:2pU28資訊網——每日最新資訊28at.com

  • 消息的發布或制作
  • 消息的訂閱或消費

其中主要的類都在這兩個包下面,感興趣的小伙伴可以去看看,原理就先不講了,下期再安排吧。2pU28資訊網——每日最新資訊28at.com

org.springframework.data.redis.connectionorg.springframework.data.redis.listener

發布消息

發布消息我們可以直接使用RedisTemplate的 convertAndSend , 這個方法有兩個參數,分別是channel, 還有消息內容。2pU28資訊網——每日最新資訊28at.com

public Long convertAndSend(String channel, Object message) {        Assert.hasText(channel, "a non-empty channel is required");        byte[] rawChannel = this.rawString(channel);        byte[] rawMessage = this.rawValue(message);        return (Long)this.execute((connection) -> {            return connection.publish(rawChannel, rawMessage);        }, true);    }

本次我們使用如下類來發布消息。作為示例就要簡單粗暴。2pU28資訊網——每日最新資訊28at.com

public interface MessagePublisher {    void publish(String message);}import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.listener.ChannelTopic;public class RedisMessagePublisher implements MessagePublisher {    private RedisTemplate<String, Object> redisTemplate;    private ChannelTopic topic;    public RedisMessagePublisher() {    }    public RedisMessagePublisher(            RedisTemplate<String, Object> redisTemplate, ChannelTopic topic) {        this.redisTemplate = redisTemplate;        this.topic = topic;    }    public void publish(String message) {        redisTemplate.convertAndSend(topic.getTopic(), message);    }}

訂閱消息

訂閱消息需要實現MessageListener的接口 ,onMessage的方法是收到消息后的消費方法。2pU28資訊網——每日最新資訊28at.com

import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.connection.MessageListener;import org.springframework.stereotype.Service;@Servicepublic class RedisMessageSubscriber implements MessageListener {        public void onMessage(Message message, byte[] pattern) {        System.*out*.println("Message received: " + message.toString());    }}// 消息訂閱2@Service("redisMessageSubscriber2")public class RedisMessageSubscriber2 implements MessageListener {    public void onMessage(Message message, byte[] pattern) {        System.out.println("Message received2: " + message.toString());    }}

消息監聽容器和適配器

另外就是訂閱方訂閱發布者,SpringDataRedis這里使用了一個消息監聽容器和適配器來處理。我們直接貼出代碼:2pU28資訊網——每日最新資訊28at.com

import com.north.redis.message.MessagePublisher;import com.north.redis.message.RedisMessagePublisher;import com.north.redis.message.RedisMessageSubscriber;import jakarta.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.MessageListener;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.listener.ChannelTopic;import org.springframework.data.redis.listener.RedisMessageListenerContainer;import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;@Configurationpublic class RedisConfig {    @Autowired    private RedisConnectionFactory redisConnectionFactory;    @Resource    MessageListener redisMessageSubscriber2;        @Bean    public RedisTemplate<String, Object> redisTemplate() {        RedisTemplate<String, Object> template = new RedisTemplate<>();        template.setConnectionFactory(redisConnectionFactory);        // 使用StringRedisSerializer來序列化和反序列化redis的key值        template.setKeySerializer(new StringRedisSerializer());        // 使用GenericJackson2JsonRedisSerializer來序列化和反序列化redis的value值        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());        template.afterPropertiesSet();        return template;    }    @Bean    MessageListenerAdapter messageListener() {        return new MessageListenerAdapter(new RedisMessageSubscriber());    }        @Bean    RedisMessageListenerContainer redisContainer() {        RedisMessageListenerContainer container                = new RedisMessageListenerContainer();        container.setConnectionFactory(redisConnectionFactory);        container.addMessageListener(messageListener(), topic());        container.addMessageListener(redisMessageSubscriber2, topic());        return container;    }    @Bean    MessagePublisher redisPublisher() {        return new RedisMessagePublisher(redisTemplate(), topic());    }    @Bean    ChannelTopic topic() {        return new ChannelTopic("northQueue");    }}

以上代碼中有幾個點:2pU28資訊網——每日最新資訊28at.com

  1. 創建適配器時,這里面我們使用了MessageListener的實現類,簡單容易理解。
  2. 使用消息容器來訂閱消息隊列,其中addMessageListener中可以訂閱多個隊列,其中第二個參數可以傳入隊列名數組。而且可以添加多個訂閱方。

RedisMessageListenerContainer 是處理消費者和發布者的關系的類 ,使用起來也比較簡單。2pU28資訊網——每日最新資訊28at.com

測試

下面我們做一個小測試:2pU28資訊網——每日最新資訊28at.com

寫一個接口來出發消息發布,使用多個訂閱者2pU28資訊網——每日最新資訊28at.com

@RestControllerpublic class TestController {    @Resource    private MessagePublisher redisMessagePublisher;    @GetMapping("/hello")    public Flux<String> hello(@RequestParam String message) {        redisMessagePublisher.publish(message);        return Flux.*just*("Hello", "Webflux");    }}

啟動SpringBoot項目后我們發送消息測試:2pU28資訊網——每日最新資訊28at.com

圖片圖片2pU28資訊網——每日最新資訊28at.com

兩個消費者都接到了消息:2pU28資訊網——每日最新資訊28at.com

圖片圖片2pU28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-59651-0.htmlRedis發布訂閱,右手就行!

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: Spring Boot:玩轉日期時間類型參數轉換技巧

下一篇: 掌握ReflectionUtils:解鎖Java反射的無限可能

標簽:
  • 熱門焦點
  • 5月iOS設備性能榜:M1 M2依舊是榜單前五

    和上個月一樣,沒有新品發布的iOS設備性能榜的上榜設備并沒有什么更替,僅僅只有跑分變化而產生的排名變動,剛剛開始的蘋果WWDC2023,推出的產品也依舊是新款Mac Pro、新款Mac Stu
  • 使用Webdriver-manager解決瀏覽器與驅動不匹配所帶來自動化無法執行的問題

    1、前言在我們使用 Selenium 進行 UI 自動化測試時,常常會因為瀏覽器驅動與瀏覽器版本不匹配,而導致自動化測試無法執行,需要手動去下載對應的驅動版本,并替換原有的驅動,可能還
  • 從零到英雄:高并發與性能優化的神奇之旅

    作者 | 波哥審校 | 重樓作為公司的架構師或者程序員,你是否曾經為公司的系統在面對高并發和性能瓶頸時感到手足無措或者焦頭爛額呢?筆者在出道那會為此是吃盡了苦頭的,不過也得
  • 為什么你不應該使用Div作為可點擊元素

    按鈕是為任何網絡應用程序提供交互性的最常見方式。但我們經常傾向于使用其他HTML元素,如 div span 等作為 clickable 元素。但通過這樣做,我們錯過了許多內置瀏覽器的功能。
  • 大廠卷向扁平化

    來源:新熵作者丨南枝 編輯丨月見大廠職級不香了。俗話說,兵無常勢,水無常形,互聯網企業調整職級體系并不稀奇。7月13日,淘寶天貓集團啟動了近年來最大的人力制度改革,目前已形成一
  • 造車兩年股價跌六成,小米的估值邏輯變了嗎?

    如果從小米官宣造車后的首個交易日起持有小米集團的股票,那么截至2023年上半年最后一個交易日,投資者將浮虧59.16%,同區間的恒生科技指數跌幅為52.78%
  • iQOO 11S新品發布會

    iQOO將在7月4日19:00舉行新品發布會,推出杭州亞運會電競賽事官方用機iQOO 11S。
  • 世界人工智能大會國際日開幕式活動在世博展覽館開啟

    30日上午,世界人工智能大會國際日開幕式活動在世博展覽館開啟,聚集國際城市代表、重量級院士專家、國際創新企業代表,共同打造人工智能交流平臺。上海市副市
  • 親歷馬斯克血洗Twitter,硅谷的苦日子在后頭

    文/劉哲銘  編輯/李薇  馬斯克再次揮下裁員大刀。  美國時間11月14日,Twitter約4400名外包員工遭解雇,此次被解雇的員工的主要工作為內容審核等。此前,T
Top 主站蜘蛛池模板: 鄢陵县| 铜山县| 钦州市| 邮箱| 齐河县| 北安市| 崇明县| 忻州市| 新闻| 麟游县| 大渡口区| 德庆县| 东丰县| 白玉县| 株洲市| 蒙自县| 朝阳市| 湘乡市| 奎屯市| 神农架林区| 威海市| 耒阳市| 长顺县| 宜川县| 汉沽区| 衢州市| 罗源县| 苍南县| 孟州市| 桂平市| 夹江县| 平舆县| 张家港市| 合江县| 霞浦县| 石棉县| 龙游县| 山东省| 前郭尔| 河南省| 南充市|