具有佇列的特性,再給它附加一個延遲消費佇列訊息的功能,也就是說可以指定佇列中的訊息在哪個時間點被消費。
延時佇列在專案中的應用還是比較多的,尤其像電商類平臺:
該介紹來自其他文章
下面的例子沒有進行封裝,所以程式碼僅供參考
注意:
不保證在設定的過期時間立即刪除並行送通知,資料量大的時候會延遲比較大
不保證一定送達
傳送即忘策略,不包含持久化
但是比如有些場景,對這個時間不是那麼看重,並且有其他措施雙層保障,該實現方案是比較簡單。
redis自2.8.0之後版本提供Keyspace Notifications功能,允許客戶訂閱Pub / Sub頻道,以便以某種方式接收影響Redis資料集事件。
需要修改設定啟用過期事件,比如在windows使用者端中,需要修改redis.windows.conf檔案,在linux中需要修改redis.conf,修改內容是:
-- 取消註釋
notify-keyspace-events Ex
-- 註釋
#notify-keyspace-events ""
然後重新啟動伺服器,比如windows
.\redis-server.exe .\redis.windows.conf
或者linux中使用docker-compose重新部署redis
redis:
container_name: redis
image: redis
hostname: redis
restart: always
ports:
- "6379:6379"
volumes:
- $PWD/redis/redis.conf:/etc/redis.conf
- /root/common-docker-compose/redis/data:/data
command:
/bin/bash -c "redis-server /etc/redis.conf" #啟動執行指定的redis.conf檔案
然後使用使用者端訂閱事件
-- windows
.\redis-cli
-- linux
docker exec -it 容器標識 redis-cli
psubscribe __keyevent@0__:expired
使用StackExchange.Redis元件訂閱過期事件
var connectionMultiplexer = ConnectionMultiplexer.Connect(_redisConnection);
var db = connectionMultiplexer.GetDatabase(0);
db.StringSet("orderno:123456", "訂單建立", TimeSpan.FromSeconds(10));
Console.WriteLine("開始訂閱");
var subscriber = connectionMultiplexer.GetSubscriber();
//訂閱庫0的過期通知事件
subscriber.Subscribe("__keyevent@0__:expired", (channel, key) =>
{
Console.WriteLine($"key過期 channel:{channel} key:{key}");
});
Console.ReadLine();
輸出結果:
key過期 channel:keyevent@0:expired key:orderno:123456
如果啟動多個使用者端監聽,那麼多個使用者端都可以收到過期事件。
建立RedisListenService繼承自:BackgroundService
public class RedisListenService : BackgroundService
{
private readonly ISubscriber _subscriber;
public RedisListenService(IServiceScopeFactory serviceScopeFactory)
{
using var scope = serviceScopeFactory.CreateScope();
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
var connectionMultiplexer = ConnectionMultiplexer.Connect(configuration["redis"]);
var db = connectionMultiplexer.GetDatabase(0);
_subscriber = connectionMultiplexer.GetSubscriber();
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
//訂閱庫0的過期通知事件
_subscriber.Subscribe("__keyevent@0__:expired", (channel, key) =>
{
Console.WriteLine($"key過期 channel:{channel} key:{key}");
});
return Task.CompletedTask;
}
}
註冊該後臺服務
services.AddHostedService<RedisListenService>();
啟用專案,給redis指定庫設定值,等過期後會接收到過期通知事件。
版本資訊 Rabbitmq版本:3.10.5 Erlang版本:24.3.4.2
要使用rabbitmq做延遲是需要安裝外掛(rabbitmq_delayed_message_exchange)的
外掛介紹:https://blog.rabbitmq.com/posts/2015/04/scheduling-messages-with-rabbitmq
下載地址:https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases
將下載好的外掛(d:/Download/rabbitmq_delayed_message_exchange-3.10.2.ez)對映到容器的plugins目錄下:
docker run -d --name myrabbit -p 9005:15672 -p 5672:5672 -e RABBITMQ_DEFAULT_VHOST=customer -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=123456 -v d:/Download/rabbitmq_delayed_message_exchange-3.10.2.ez:/plugins/rabbitmq_delayed_message_exchange-3.10.2.ez rabbitmq:3-management-alpine
進入容器
docker exec -it 容器名稱/標識 bash
啟用外掛
rabbitmq-plugins enable rabbitmq_delayed_message_exchange
檢視是否啟用
rabbitmq-plugins list
[E]和[e]表示啟用,然後重啟服務
rabbitmq-server restart
然後在管理介面新增交換機都可以看到
傳送的訊息型別是:x-delayed-message
[HttpGet("send/delay")]
public string SendDelayedMessage()
{
var factory = new ConnectionFactory()
{
HostName = "localhost",//IP地址
Port = 5672,//埠號
UserName = "admin",//使用者賬號
Password = "123456",//使用者密碼
VirtualHost = "customer"
};
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
var exchangeName = "delay-exchange";
var routingkey = "delay.delay";
var queueName = "delay_queueName";
//設定Exchange佇列型別
var argMaps = new Dictionary<string, object>()
{
{"x-delayed-type", "topic"}
};
//設定當前訊息為延時佇列
channel.ExchangeDeclare(exchange: exchangeName, type: "x-delayed-message", true, false, argMaps);
channel.QueueDeclare(queueName, true, false, false, argMaps);
channel.QueueBind(queueName, exchangeName, routingkey);
var time = 1000 * 5;
var message = $"傳送時間為 {DateTime.Now:yyyy-MM-dd HH:mm:ss} 延時時間為:{time}";
var body = Encoding.UTF8.GetBytes(message);
var props = channel.CreateBasicProperties();
//設定訊息的過期時間
props.Headers = new Dictionary<string, object>()
{
{ "x-delay", time }
};
channel.BasicPublish(exchange: exchangeName, routingKey: routingkey, basicProperties: props, body: body);
Console.WriteLine("成功傳送訊息:" + message);
return "success";
}
消費訊息我是弄了一個後臺任務(RabbitmqDelayedHostService)在處理
public class RabbitmqDelayedHostService : BackgroundService
{
private readonly IModel _channel;
private readonly IConnection _connection;
public RabbitmqDelayedHostService()
{
var connFactory = new ConnectionFactory//建立連線工廠物件
{
HostName = "localhost",//IP地址
Port = 5672,//埠號
UserName = "admin",//使用者賬號
Password = "123456",//使用者密碼
VirtualHost = "customer"
};
_connection = connFactory.CreateConnection();
_channel = _connection.CreateModel();
//交換機名稱
var exchangeName = "exchangeDelayed";
var queueName = "delay_queueName";
var routingkey = "delay.delay";
var argMaps = new Dictionary<string, object>()
{
{"x-delayed-type", "topic"}
};
_channel.ExchangeDeclare(exchange: exchangeName, type: "x-delayed-message", true, false, argMaps);
_channel.QueueDeclare(queueName, true, false, false, argMaps);
_channel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: routingkey);
//宣告為手動確認
_channel.BasicQos(0, 1, false);
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
var queueName = "delay_queueName";
var consumer = new EventingBasicConsumer(_channel);
consumer.Received += (model, ea) =>
{
var message = Encoding.UTF8.GetString(ea.Body.ToArray());
var routingKey = ea.RoutingKey;
Console.WriteLine($"接受到訊息的時間為 {DateTime.Now:yyyy-MM-dd HH:mm:ss},routingKey:{routingKey} message:{message} ");
//手動確認
_channel.BasicAck(ea.DeliveryTag, true);
};
_channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer);
return Task.CompletedTask;
}
public override void Dispose()
{
_connection.Dispose();
_channel.Dispose();
base.Dispose();
}
}
註冊該後臺任務
services.AddHostedService<RabbitmqDelayedHostService>();
輸出結果
成功傳送訊息:傳送時間為 2022-07-02 18:54:22 延時時間為:5000
成功傳送訊息:傳送時間為 2022-07-02 18:54:22 延時時間為:5000
成功傳送訊息:傳送時間為 2022-07-02 18:54:22 延時時間為:5000
成功傳送訊息:傳送時間為 2022-07-02 18:54:23 延時時間為:5000
成功傳送訊息:傳送時間為 2022-07-02 18:54:23 延時時間為:5000
成功傳送訊息:傳送時間為 2022-07-02 18:54:23 延時時間為:5000
接受到訊息的時間為 2022-07-02 18:54:27,routingKey:delay.delay message:傳送時間為 2022-07-02 18:54:22 延時時間為:5000
接受到訊息的時間為 2022-07-02 18:54:27,routingKey:delay.delay message:傳送時間為 2022-07-02 18:54:22 延時時間為:5000
接受到訊息的時間為 2022-07-02 18:54:27,routingKey:delay.delay message:傳送時間為 2022-07-02 18:54:22 延時時間為:5000
接受到訊息的時間為 2022-07-02 18:54:28,routingKey:delay.delay message:傳送時間為 2022-07-02 18:54:23 延時時間為:5000
接受到訊息的時間為 2022-07-02 18:54:28,routingKey:delay.delay message:傳送時間為 2022-07-02 18:54:23 延時時間為:5000
接受到訊息的時間為 2022-07-02 18:54:28,routingKey:delay.delay message:傳送時間為 2022-07-02 18:54:23 延時時間為:5000
BackgroundJob.Schedule(
() => Console.WriteLine("Delayed!"),
TimeSpan.FromDays(7));