實現了消息隊列所需要的所有功能,包括:
同時需要注意的是Stream只是一個數據結構,他不會主動把消息推送給消費者,需要消費者主動來消費數據 。
每個Stream都有唯一的名稱,它就是Redis的key,首次使用 xadd 指令追加消息時自動創建。
常見操作命令如下表:
如果客戶端希望知道自身消費到第幾條數據了,那么就需要記錄一下當前消費的消息ID,下次再次消費的時候就從上次消費的消息ID開始讀取數據即可。
消費組中多了一個游標 last_delivered_id ,表示當前消費到了哪一條數據。同時所有的數據都是待處理消息( PEL ),只有消費者處理完畢之后使用 ack 指令告知redis服務器,數據才會從 PEL 中移除,確認后的消息就無法再次消費。
如果接收到的消息比較多,為了避免Stream過長,可以選擇指定Stream的最大長度,一旦到達了最大長度,就會從最早的消息開始清除,保證Stream中最新的消息。
當你看到這個文章時,想必已經了解了一些關于spring-redis發布訂閱方面的知識,如果你也遇到同樣的問題
那么請考慮spring在啟動時有沒有重復的加載配置文件
具體的方式可以在 AbstractApplicationContext 的 refresh() 方法中打斷點,如果走了兩次,說明配置文件加載了兩遍
而配置文件加載兩遍的原因是因為web.xml中DispatcherServlet和ContextLoaderLinistener 共用了 某些配置文件導致的。
基于Redis消息隊列-實現短信服務化
1.Redis實現消息隊列原理
常用的消息隊列有RabbitMQ,ActiveMQ,個人覺得這種消息隊列太大太重,本文介紹下基于Redis的輕量級消息隊列服務。
一般來說,消息隊列有兩種模式,一種是發布者訂閱模式,另外一種是生產者和消費者模式。Redis的消息隊列,也是基于這2種原理的實現。
發布者和訂閱者模式:發布者發送消息到隊列,每個訂閱者都能收到一樣的消息。
生產者和消費者模式:生產者將消息放入隊列,多個消費者共同監聽,誰先搶到資源,誰就從隊列中取走消息去處理。注意,每個消息只能最多被一個消費者接收。
2.Redis消息隊列使用場景
在我們的項目中,使用消息隊列來實現短信的服務化,任何需要發送短信的模塊,都可以直接調用短信服務來完成短信的發送。比如用戶系統登錄注冊短信,訂單系統的下單成功的短信等。
3.SpringMVC中實現Redis消息隊列
因為我們短信只需要發送一次,所以我們使用的是消息隊列的生產者和消費者模式。
3.1引入Maven依賴
引入Redis相應的maven依賴,這里需要spring-data-redis和jedis
//pom.xml <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.5.1</version>
</dependency>12345678910111213
3.2配置redis生成者消費者模式
//applicationContext-redis.xml <?xml version="1.0" en買粉絲ding="UTF-8"?><beans xmlns="買粉絲://買粉絲.springframework.org/schema/beans"
xmlns:xsi="買粉絲://買粉絲.w3.org/2001/XMLSchema-instance" xmlns:mvc="買粉絲://買粉絲.springframework.org/schema/mvc"
xmlns:tx="買粉絲://買粉絲.springframework.org/schema/tx" xmlns:買粉絲ntext="買粉絲://買粉絲.springframework.org/schema/買粉絲ntext"
xmlns:aop="買粉絲://買粉絲.springframework.org/schema/aop" xmlns:cache="買粉絲://買粉絲.springframework.org/schema/cache"
xmlns:redis="買粉絲://買粉絲.springframework.org/schema/redis"
xsi:schemaLocation="買粉絲://買粉絲.springframework.org/schema/mvc 買粉絲://買粉絲.springframework.org/schema/mvc/spring-mvc-4.0.xsd
買粉絲://買粉絲.springframework.org/schema/tx 買粉絲://買粉絲.springframework.org/schema/tx/spring-tx-34.0.xsd
買粉絲://買粉絲.springframework.org/schema/beans 買粉絲://買粉絲.springframework.org/schema/beans/spring-beans-4.0.xsd
買粉絲://買粉絲.springframework.org/schema/買粉絲ntext 買粉絲://買粉絲.springframework.org/schema/買粉絲ntext/spring-買粉絲ntext-4.0.xsd
買粉絲://買粉絲.springframework.org/schema/aop 買粉絲://買粉絲.springframework.org/schema/aop/spring-aop.xsd
買粉絲://買粉絲.springframework.org/schema/cache 買粉絲://買粉絲.springframework.org/schema/cache/spring-cache-4.0.xsd
買粉絲://買粉絲.springframework.org/schema/redis 買粉絲://買粉絲.springframework.org/schema/redis/spring-redis-1.0.xsd">
<description>spring-data-redis配置</description>
<bean id="redisConnectionFactory"
class="org.springframework.data.redis.買粉絲nnection.jedis.JedisConnectionFactory">
<property name="hostName" value="${ redis.host}"></property>
<property name="port" value="${ redis.port}"></property>
<property name="usePool" value="true"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.買粉絲re.RedisTemplate">
<property name="買粉絲nnectionFactory" ref="redisConnectionFactory"></property>
</bean>
<bean id="jdkSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
<bean id="smsMessageListener"
class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
<property name="delegate" ref="smsMessageDelegateListener" />
<property name="serializer" ref="jdkSerializer" />
</bean>
<bean id="sendMessage" class="買粉絲.djt.買粉絲mon.cache.redis.queue.SendMessage">
<property name="redisTemplate" ref="redisTemplate"/>
</bean>
<2024-07-25 13:24
2024-07-25 13:03
2024-07-25 12:49
2024-07-25 12:30
2024-07-25 12:05
2024-07-25 11:14