7

我正在使用 Redis pubsub 通道将消息从工作进程池发送到我的 ASP.NET 应用程序。收到消息后,我的应用程序使用 SignalR 将消息转发到客户端的浏览器。

我找到了维护与 Redis 的开放连接的解决方案,但它在重新创建连接时不考虑订阅。

我目前正在 Global.asax 文件中处理 Redis pubsub 消息:

public class Application : HttpApplication
{
    protected void Application_Start()
    {
        var gateway = Resolve<RedisConnectionGateway>();
        var connection = gateway.GetConnection();
        var channel = connection.GetOpenSubscriberChannel();

        channel.PatternSubscribe("workers:job-done:*", OnExecutionCompleted);
    }

    /// <summary>
    /// Handle messages received from workers through Redis.</summary>
    private static void OnExecutionCompleted(string key, byte[] message)
    {
        /* forwarded the response to the client that requested it */
    }
}

当当前的 RedisConnection 出于某种原因关闭时,就会出现问题。问题的最简单解决方案是RedisConnectionGateway在连接重置后从类中触发一个事件,然后使用新的RedisSubscriberChannel. 但是,在重置连接时发布到通道的任何消息都将丢失。

是否有处理这种情况的推荐方法示例?

4

1 回答 1

8

Yes, if the connection dies (network instability, re-mastering, whatever) then you will need to re-apply any subscriptions you have made. An event to reconnect and resubscribe is pretty normal, and not very different to what we use here on SE/SO (except we typically track more granular subscriptions, and have some wrapper code that handles all that).

Yes, any events published while your connection was broken are gone. That is the nature of redis pub/sub; it does not guarantee delivery to disconnected clients. Either use a tool that does promise this, or use redis to drive a queue instead - pushing/popping to/from opposite ends of a list is usually a reasonable alternative, and ensures nothing is lost (as long as your software doesn't drop it after popping it from the list). If it helps, I have on my list a request to add the blocking pop methods - they totally destroy the multiplexer intent, but they have genuine use in some cases, so I'm not against adding them.

于 2012-04-10T13:08:13.777 回答