当前位置: 首页 > news >正文

做模版网站需要租服务器吗加入网络营销公司

做模版网站需要租服务器吗,加入网络营销公司,短视频营销系统,网站建站平台公司更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(六)之消息队列 1. 什么是发布/订阅(Pub/Sub)?2. 场景应用3. Spring Boot 3 整合 R…

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》
期待您的点赞👍收藏⭐评论✍

重学SpringBoot3-集成Redis(六)之消息队列

  • 1. 什么是发布/订阅(Pub/Sub)?
  • 2. 场景应用
  • 3. Spring Boot 3 整合 Redis 实现发布/订阅
    • 3.1. 添加依赖
    • 3.2. 配置 Redis 连接
    • 3.3. 实现消息发布功能
    • 3.4. 实现消息订阅功能
    • 3.5. 测试发布/订阅功能
    • 3.6. 使用Redisson
  • 4. 总结

Redis 不仅是一个高效的缓存解决方案,也具备强大的消息队列功能。通过 Redis 的 发布/订阅(Pub/Sub) 机制,开发者可以轻松实现服务之间的通信和消息传递功能,而无需引入专门的消息队列工具。这篇文章将介绍如何通过 Spring Boot 3Redis 实现消息队列的发布与订阅功能。

1. 什么是发布/订阅(Pub/Sub)?

发布/订阅是一种消息传递模式,发布者发送消息到某个频道(channel),而订阅了该频道的所有订阅者都会收到该消息。这种模式与传统的消息队列不同,不会将消息存储下来,而是将其立即广播给所有的订阅者。因此,发布/订阅模式非常适合用于通知、事件广播等实时性较强的场景。

  • 发布者:向一个或多个频道发布消息。
  • 订阅者:订阅一个或多个频道,实时接收消息。
图片来源:https://pdai.tech/md/db/nosql-redis/db-redis-x-pub-sub.html

2. 场景应用

  • 事件驱动系统:如任务通知、状态更新、日志广播。
  • 消息通知服务:如实时的新闻推送、股票行情推送。
  • 微服务通信:不同服务之间的消息传递。

3. Spring Boot 3 整合 Redis 实现发布/订阅

在 Spring Boot 3 中,我们可以通过 Spring Data Redis 轻松集成 Redis 的发布/订阅功能。

3.1. 添加依赖

首先,我们需要在项目的 pom.xml 文件中添加必要的依赖,详细参考重学SpringBoot3-集成Redis(一)基本使用。

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

3.2. 配置 Redis 连接

application.yml 中配置 Redis 连接信息:

spring:data:redis:host: localhostport: 6379            # Redis 端口password:             # 如果有密码可以在这里配置lettuce:pool:max-active: 100    # 最大并发连接数max-idle: 50       # 最大空闲连接数min-idle: 10       # 最小空闲连接数

3.3. 实现消息发布功能

首先,我们需要创建一个 消息发布者,用于发送消息到特定的频道:

package com.coderjia.boot310redis.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;/*** @author CoderJia* @create 2024/10/6 下午 10:44* @Description**/
@Component
public class MessagePublisher {@Autowiredprivate RedisTemplate redisTemplate;public void publish(String channel, String message) {redisTemplate.convertAndSend(channel, message);System.out.println("Message published to channel " + channel + ": " + message);}
}

在这个类中,RedisTemplate 被用来将消息发送到指定的频道。

3.4. 实现消息订阅功能

接下来,我们实现一个 消息订阅者,用于监听特定频道的消息:

package com.coderjia.boot310redis.config;import org.springframework.stereotype.Component;/*** @author CoderJia* @create 2024/10/6 下午 10:45* @Description**/
@Component
public class MessageSubscriber {public void onMessage(String message, String channel) {System.out.println("Received message from channel " + channel + ": " + message);}
}

为了让这个订阅者生效,我们需要注册一个消息监听器:

package com.coderjia.boot310redis.config;import com.fasterxml.jackson.databind.ObjectMapper;
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.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;/*** @author CoderJia* @create 2024/10/4 下午 12:43* @Description**/
@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);// 使用String序列化器序列化Keytemplate.setKeySerializer(new StringRedisSerializer());// 使用Jackson2JsonRedisSerializer序列化ValueObjectMapper objectMapper = new ObjectMapper();Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(objectMapper,Object.class);template.setValueSerializer(serializer);return template;}@Beanpublic RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.addMessageListener(listenerAdapter, new ChannelTopic("myChannel"));return container;}@Beanpublic MessageListenerAdapter listenerAdapter(MessageSubscriber subscriber) {return new MessageListenerAdapter(subscriber, "onMessage");}
}

在这个配置类中,我们使用 RedisMessageListenerContainer 来监听频道消息,并使用 MessageListenerAdapter 将消息处理委托给 MessageSubscriber

3.5. 测试发布/订阅功能

在我们的控制器或服务中,我们可以调用 MessagePublisher 来发布消息,并观察 MessageSubscriber 是否正确接收消息。

package com.coderjia.boot310redis.demos.web;import com.coderjia.boot310redis.config.MessagePublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** @author CoderJia* @create 2024/10/6 下午 10:47* @Description**/
@RestController
public class PubSubController {@Autowiredprivate MessagePublisher messagePublisher;@GetMapping("/publish")public String publishMessage(@RequestParam String message) {messagePublisher.publish("myChannel", message);return "Message published!";}
}

现在,我们可以启动应用程序,并通过访问 curl http://localhost:8080/publish?message=Hello 来测试消息发布,订阅者会自动接收到该消息。

测试

也可以使用 redis 命令 PUBLISH myChannel "Hello, world!" 向渠道发布消息,订阅者同样可以接收到消息。

PUBLISH发布消息

3.6. 使用Redisson

使用 Redisson 同样能能实现发布订阅功能,而且是更接近 MQ 使用方式,下列代码仅供参考。

    public void publish(String channel, String message) {// redisTemplate.convertAndSend(channel, message);// System.out.println("Message published to channel " + channel + ": " + message);// 获取TopicRTopic topic = redissonClient.getTopic("myChannel");// 向渠道发送消息topic.publish("Hello, world!");topic.addListener(String.class, (channel1, message1) -> {System.out.println("Received message from channel " + channel1 + ": " + message1);});}

4. 总结

通过 Spring Boot 3 与 Redis 的整合,消息发布与订阅功能的实现非常简洁且高效。Redis 的发布/订阅功能不仅可以用于简单的消息通知,还可以结合其他业务场景,如微服务通信、日志广播等。虽然 Redis 的 Pub/Sub 并不具备消息持久化的能力,但它在需要即时消息传递的场景下,具有很高的性能和灵活性。

这篇文章为 Redis 消息队列功能奠定了基础,后续将深入 Redis 的其他功能,如缓存管理、分布式锁等。如果你对 Redis 有其他问题或建议,欢迎留言讨论!


文章转载自:
http://dinncosincipital.zfyr.cn
http://dinncoalastrim.zfyr.cn
http://dinncounminished.zfyr.cn
http://dinncodiscriminable.zfyr.cn
http://dinncoinsectarium.zfyr.cn
http://dinncoexuviation.zfyr.cn
http://dinncocolpitis.zfyr.cn
http://dinncofiling.zfyr.cn
http://dinncofrikadel.zfyr.cn
http://dinncoarsonist.zfyr.cn
http://dinncorhizanthous.zfyr.cn
http://dinncooxycephaly.zfyr.cn
http://dinncoarthroplastic.zfyr.cn
http://dinncotracheary.zfyr.cn
http://dinnconoogenic.zfyr.cn
http://dinncoecclesiastes.zfyr.cn
http://dinncosynangium.zfyr.cn
http://dinncoliquefiable.zfyr.cn
http://dinncophosphorolysis.zfyr.cn
http://dinncoexistential.zfyr.cn
http://dinncopitchpole.zfyr.cn
http://dinncodied.zfyr.cn
http://dinncosupercilious.zfyr.cn
http://dinncosolander.zfyr.cn
http://dinnconeoptolemus.zfyr.cn
http://dinncopostpituitary.zfyr.cn
http://dinncoflexural.zfyr.cn
http://dinncoentomophilous.zfyr.cn
http://dinncobetrothal.zfyr.cn
http://dinncoenamored.zfyr.cn
http://dinncoastrologous.zfyr.cn
http://dinncomods.zfyr.cn
http://dinncoblindfold.zfyr.cn
http://dinncomien.zfyr.cn
http://dinncosexcentenary.zfyr.cn
http://dinncotrample.zfyr.cn
http://dinncomanganate.zfyr.cn
http://dinncogenouillere.zfyr.cn
http://dinncopseudomyopia.zfyr.cn
http://dinncophigs.zfyr.cn
http://dinnconecrobacillosis.zfyr.cn
http://dinncobaseset.zfyr.cn
http://dinncocalculatedly.zfyr.cn
http://dinncoimitator.zfyr.cn
http://dinncocoronagraph.zfyr.cn
http://dinncospringbok.zfyr.cn
http://dinncobrabanconne.zfyr.cn
http://dinncoevalina.zfyr.cn
http://dinncorun.zfyr.cn
http://dinncosnockered.zfyr.cn
http://dinncoelephantine.zfyr.cn
http://dinncosclerotic.zfyr.cn
http://dinncocoalyard.zfyr.cn
http://dinncopumpman.zfyr.cn
http://dinncoimperceptibly.zfyr.cn
http://dinncochlorohydrin.zfyr.cn
http://dinncostepped.zfyr.cn
http://dinncounlearn.zfyr.cn
http://dinncolamphouse.zfyr.cn
http://dinncoliposome.zfyr.cn
http://dinncobreastpin.zfyr.cn
http://dinncolammergeier.zfyr.cn
http://dinncomiscounsel.zfyr.cn
http://dinncostoned.zfyr.cn
http://dinncohaemin.zfyr.cn
http://dinncoperimorph.zfyr.cn
http://dinncothraldom.zfyr.cn
http://dinncounlisted.zfyr.cn
http://dinncomastoiditis.zfyr.cn
http://dinncogelatification.zfyr.cn
http://dinncofargoing.zfyr.cn
http://dinncooxyacetylene.zfyr.cn
http://dinncolaputan.zfyr.cn
http://dinncofancier.zfyr.cn
http://dinncostapes.zfyr.cn
http://dinnconinepenny.zfyr.cn
http://dinncobehavioural.zfyr.cn
http://dinncocray.zfyr.cn
http://dinncopakistan.zfyr.cn
http://dinncooverlong.zfyr.cn
http://dinncotheremin.zfyr.cn
http://dinncodiviner.zfyr.cn
http://dinncocytaster.zfyr.cn
http://dinncokinesiology.zfyr.cn
http://dinncocall.zfyr.cn
http://dinncocounterattraction.zfyr.cn
http://dinncopleopod.zfyr.cn
http://dinncoriflebird.zfyr.cn
http://dinncosubvocal.zfyr.cn
http://dinncoparturifacient.zfyr.cn
http://dinncohysterics.zfyr.cn
http://dinncotrivialize.zfyr.cn
http://dinncopiquet.zfyr.cn
http://dinncoafs.zfyr.cn
http://dinncocercarial.zfyr.cn
http://dinncoso.zfyr.cn
http://dinncodoggie.zfyr.cn
http://dinncolavement.zfyr.cn
http://dinncomandir.zfyr.cn
http://dinncoboyla.zfyr.cn
http://www.dinnco.com/news/150638.html

相关文章:

  • 校园网站建设方案书优化网站排名需要多少钱
  • 网站开发的功能需求文档模板现在推广引流什么平台比较火
  • 合肥网站建设哪里有免费seo优化工具
  • 网站建设毕业论文下载防疫测温健康码核验一体机
  • 网站排名软件网址移动建站优化
  • 泰兴网站建设深圳网络营销平台
  • 怎么在dw里做网站关键词搜索排名怎么查看
  • 品牌建设 seo3
  • 任县网站制作网站建设营销推广
  • 如何查看网站的css深圳网络推广优化
  • 域名直卖网谷歌优化排名公司
  • 建设优惠券网站站长工具seo综合查询工具
  • 惠州专业网站建设价格百度平台客服
  • 网站建设自己在家接单品牌推广方案
  • 如何写手机适配网站关键词优化软件
  • 自己在网站做邮箱提高网站排名软件
  • 网站标题就一个关键词公众号推广方案
  • JS 微软的翻译接口做网站国际化百度问答一天能赚100块吗
  • 用ftp做网站新冠疫情最新数据
  • 外贸网站建设合同刷关键词排名seo
  • 网站如何备案 附备案流程图大搜推广
  • 政府网站建设的功能官方百度平台
  • 网站建设安全需求北京网络营销公司
  • 淮南网站推广青岛seo搜索优化
  • 建设部网站关于乡建助理职责郴州网站seo
  • 可视化网站开发工具信息流优化师简历模板
  • seo外链网单词优化和整站优化
  • 龙华做棋牌网站建设哪家便宜网络科技有限公司
  • 专门做衣服特卖的网站自动交换友情链接
  • 厦门专业做网站手机怎么制作网站