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

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

Redis發布訂閱,右手就行!

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

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

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

前言

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

先看我們需要的依賴, 其實只需要引入spring-boot-starter-data-redis 就夠了,另外再寫一個接口來觸發消息發布。CeN28資訊網——每日最新資訊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 集成類似。CeN28資訊網——每日最新資訊28at.com

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

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

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

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

發布消息

發布消息我們可以直接使用RedisTemplate的 convertAndSend , 這個方法有兩個參數,分別是channel, 還有消息內容。CeN28資訊網——每日最新資訊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);    }

本次我們使用如下類來發布消息。作為示例就要簡單粗暴。CeN28資訊網——每日最新資訊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的方法是收到消息后的消費方法。CeN28資訊網——每日最新資訊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這里使用了一個消息監聽容器和適配器來處理。我們直接貼出代碼:CeN28資訊網——每日最新資訊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");    }}

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

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

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

測試

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

寫一個接口來出發消息發布,使用多個訂閱者CeN28資訊網——每日最新資訊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項目后我們發送消息測試:CeN28資訊網——每日最新資訊28at.com

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

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

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

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

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

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

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

標簽:
  • 熱門焦點
  • 中國家電海外掘金正當時|出海專題

    作者|吳南南編輯|胡展嘉運營|陳佳慧出品|零態LT(ID:LingTai_LT)2023年,出海市場戰況空前,中國創業者在海外紛紛摩拳擦掌,以期能夠把中國的商業模式、創業理念、戰略打法輸出海外,他們依
  • “又被陳思誠騙了”

    作者|張思齊 出品|眾面(ID:ZhongMian_ZM)如今的國產懸疑電影,成了陳思誠的天下。最近大爆電影《消失的她》票房突破30億斷層奪魁暑期檔,陳思誠再度風頭無兩。你可以說陳思誠的
  • 東方甄選單飛:有些鳥注定是關不住的

    文/彭寬鴻編輯/羅卿東方甄選創始人俞敏洪帶隊的&ldquo;7天甘肅行&rdquo;直播活動已在近日順利收官。成立后一年多時間里,東方甄選要脫離抖音自立門戶的傳聞不絕于耳,&ldquo;7
  • 阿里大調整

    來源:產品劉有媒體報道稱,近期淘寶天貓集團啟動了近年來最大的人力制度改革,涉及員工績效、層級體系等多個核心事項,目前已形成一個初步的&ldquo;征求意見版&rdquo;:1、取消P序列
  • 阿里瓴羊One推出背后,零售企業迎數字化新解

    作者:劉曠近年來隨著數字經濟的高速發展,各式各樣的SaaS應用服務更是層出不窮,但本質上SaaS大多局限于單一業務流層面,對用戶核心關切的增長問題等則沒有提供更好的解法。在Saa
  • 華為將推出盤古數字人大模型 可幫助用戶12小時完成數字人生成

    在今日舉行的2023年華為云數字文娛AI創新峰會上,華為云全球Marketing與銷售服務總裁石冀琳表示,華為云將在后續推出盤古數字人大模型,可幫助用戶12小
  • 三星顯示已開始為AR設備研發硅基LED微顯示屏

    7月18日消息,據外媒報道,隨著蘋果首款頭顯產品Vision Pro在6月份正式推出,AR/VR/MR等頭顯產品也就將成為各大公司下一個重要的競爭領域,對顯示屏這一關
  • 由于成本持續增加,筆記本產品價格預計將明顯上漲

    根據知情人士透露,由于材料、物流等成本持續增加,筆記本產品價格預計將在2021年下半年有明顯上漲。進入6月下旬以來,全球半導體芯片缺貨情況加劇,顯卡、處理器
  • 電博會與軟博會實現"線下+云端"的雙線融合

    在本次“電博會”與“軟博會”雙展會利好條件的加持下,既可以發揮展會拉動人流、信息流、資金流實現快速交互流動的作用,繼而推動區域經濟良性發展;又可以聚
Top 主站蜘蛛池模板: 湟源县| 华宁县| 宜君县| 榆中县| 乌鲁木齐县| 内江市| 北流市| 茌平县| 永新县| 蒙城县| 平遥县| 扎赉特旗| 察隅县| 孝义市| 江达县| 友谊县| 灌阳县| 阿克| 祁门县| 凤山县| 房产| 长垣县| 濮阳市| 金寨县| 北辰区| 镶黄旗| 鸡西市| 德昌县| 凤庆县| 营山县| 巴塘县| 九龙坡区| 雅安市| 岳西县| 道孚县| 怀远县| 商南县| 蛟河市| 简阳市| 和林格尔县| 永德县|