最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot集成redis實現(xiàn)消息的訂閱與發(fā)布

 更新時間:2024年05月23日 11:23:35   作者:厲害哥哥吖  
本文主要介紹了springboot集成redis實現(xiàn)消息的訂閱與發(fā)布,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言 

本節(jié)內(nèi)容主要介紹springboot項目通過集成redis,如何利用redis的訂閱發(fā)布機制,完成系統(tǒng)消息的發(fā)布與訂閱功能。Redis中的發(fā)布與訂閱是一種消息通信模式,允許發(fā)送者(發(fā)布者)將消息發(fā)送給多個接收者(訂閱者)。在 Redis中,發(fā)布與訂閱通過PUBLISH和SUBSCRIBE命令實現(xiàn)。頻道(Channel):頻道是消息的通道,用于區(qū)分不同類型或主題的消息。訂閱者可以選擇訂閱感興趣的頻道,以接收相應(yīng)的消息。Redis的發(fā)布與訂閱模式是無狀態(tài)的,即發(fā)布者在發(fā)送消息之后不需要關(guān)心是否有訂閱者接收到消息,也不需要維護訂閱者的信息。當(dāng)發(fā)布者向某個頻道發(fā)布消息時,所有訂閱了該頻道的訂閱者都會接收到相同的消息。這種機制使得消息的發(fā)布者和訂閱者之間能夠?qū)崿F(xiàn)解耦,并支持一對多的消息傳遞方式,即廣播形式。

正文

①創(chuàng)建一個web項目,引入redis啟動器的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 {
    /**
     * 初始化一個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)建一個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)建一個消息訂閱者,實現(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);
    }
}

 ⑦啟動項目,通過http請求發(fā)布消息,查看是否能夠訂閱成功消息

⑧開啟redis客戶端測試,同樣能夠訂閱到消息,證明redis的消息的訂閱與發(fā)布是無狀態(tài)的且是廣播模式

到此這篇關(guān)于springboot集成redis實現(xiàn)消息的訂閱與發(fā)布的文章就介紹到這了,更多相關(guān)springboot redis消息訂閱與發(fā)布內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • mybatis?mapper.xml中如何根據(jù)數(shù)據(jù)庫類型選擇對應(yīng)SQL語句

    mybatis?mapper.xml中如何根據(jù)數(shù)據(jù)庫類型選擇對應(yīng)SQL語句

    這篇文章主要介紹了mybatis?mapper.xml中如何根據(jù)數(shù)據(jù)庫類型選擇對應(yīng)SQL語句,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java零基礎(chǔ)入門數(shù)組

    Java零基礎(chǔ)入門數(shù)組

    數(shù)組對于每一門編程語言來說都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當(dāng)然不同語言對數(shù)組的實現(xiàn)及處理也不盡相同。Java?語言中提供的數(shù)組是用來存儲固定大小的同類型元素
    2022-04-04
  • Springboot中基于X509完成SSL檢驗的原理與實現(xiàn)

    Springboot中基于X509完成SSL檢驗的原理與實現(xiàn)

    本文詳細解析了HTTPS通信中SSL證書的作用和原理,SSL證書建立在客戶端和服務(wù)器之間的安全通道,確保數(shù)據(jù)傳輸?shù)耐暾院捅C苄?詳細的介紹了Springboot中基于X509完成SSL檢驗的原理與實現(xiàn),感興趣的可以了解一下
    2024-09-09
  • java多線程CountDownLatch與線程池ThreadPoolExecutor/ExecutorService案例

    java多線程CountDownLatch與線程池ThreadPoolExecutor/ExecutorService案

    這篇文章主要介紹了java多線程CountDownLatch與線程池ThreadPoolExecutor/ExecutorService案例,
    2021-02-02
  • JavaSE網(wǎng)絡(luò)原理之UDP和TCP原理詳解

    JavaSE網(wǎng)絡(luò)原理之UDP和TCP原理詳解

    TCP、UDP都是屬于運輸層的協(xié)議,提供端到端的進程之間的邏輯通信,而IP協(xié)議是提供主機間的邏輯通信,應(yīng)用層規(guī)定應(yīng)用進程在通信時所遵循的協(xié)議,這篇文章主要介紹了JavaSE網(wǎng)絡(luò)原理之UDP和TCP原理的相關(guān)資料,需要的朋友可以參考下
    2026-01-01
  • Java8中Optional類型和Kotlin中可空類型的使用對比

    Java8中Optional類型和Kotlin中可空類型的使用對比

    這篇文章主要給大家介紹了關(guān)于Java8中Optional類型和Kotlin中可空類型的使用對比,文中通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • mybatis動態(tài)生成sql語句的實現(xiàn)示例

    mybatis動態(tài)生成sql語句的實現(xiàn)示例

    在MyBatis中,動態(tài)SQL是一個非常重要的特性,它允許我們根據(jù)條件動態(tài)地生成SQL語句,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • java內(nèi)存溢出示例(堆溢出、棧溢出)

    java內(nèi)存溢出示例(堆溢出、棧溢出)

    這篇文章主要介紹了java內(nèi)存溢出示例(堆溢出、棧溢出),需要的朋友可以參考下
    2014-04-04
  • java中堆和棧的區(qū)別分析

    java中堆和棧的區(qū)別分析

    這篇文章主要介紹了java中堆和棧的區(qū)別,分析了Java中堆和棧的原理及使用時的注意事項,需要的朋友可以參考下
    2014-09-09
  • 分析Java中的類加載問題

    分析Java中的類加載問題

    很多時候提到類加載,大家總是沒法馬上回憶起順序,這篇文章會用一個例子為你把類加載的諸多問題一次性澄清
    2021-06-06

最新評論

抚州市| 广安市| 岳普湖县| 民丰县| 平原县| 徐水县| 湘阴县| 多伦县| 济南市| 普安县| 彰化市| 闵行区| 黄山市| 马龙县| 东乌珠穆沁旗| 遵义县| 海安县| 南丹县| 昌黎县| 谷城县| 深州市| 六枝特区| 东山县| 金沙县| 晋中市| 年辖:市辖区| 个旧市| 贡山| 米林县| 蓬莱市| 无棣县| 景德镇市| 津市市| 闽清县| 泰和县| 东至县| 寻乌县| 响水县| 合山市| 苏尼特左旗| 卢氏县|