ServiceStack RedisMqServer:无法在运行时添加或删除通道?
ServiceStack RedisMqServer: No way to add or remove channels in runtime?
我的,现在已经是“遗留”了,当它达到 20 个客户端限制时,使用 ServiceStack 快速 运行 实现了一个 pub/sub 解决方案。
我们做这样的事情:
_redisConsumer = MqClientFactory.Instance.GetRedisClient(); // Returns a IRedisClient
_subscription = _redisConsumer.CreateSubscription();
_subscription.OnSubscribe = channel => CoreLog.Instance.Info($"Subscription started on {eventChannelName}");
_subscription.OnUnSubscribe = channel => CoreLog.Instance.Warning($"Unsubscribed from {eventChannelName}");
_subscription.OnMessage = (channel, msg) =>
{
try
{
onMessageReceived(CoreRequestJsonEnvelope.CreateCoreRequestFromJson(msg));
}
catch (Exception ex)
{
CoreLog.Instance.Exception(ex);
}
};
// Since it blocks execution, we put this in a Task:
Task.Run(() =>
{
try
{
_subscription.SubscribeToChannels(eventChannelName); // blocking
}
catch(Exception e)
{
}
});
当我们也有足够多的不同频道可以收听时,它就用完了。
然后我想,也许我可以为所有订阅使用相同的 IRedisClient,而不是为每个订阅使用一个新的 IRedisClient,所以:
_redisConsumer = mySavedRedisClient;
...
但是那个returnsUnknown reply on multi-request
过了几个seconds/executions.
最后,我看了RedisPubSubServer
,但是好像需要在构造函数中指定通道,之后就改不了了。我确实需要在运行时添加和删除频道,而频道从一开始就不知道。
- 推荐的方法是什么?
- 是否增加最大限制并像以前一样继续?
- 是使用RedisPubSub,但是如何处理动态通道?
- “对多请求的未知回复”究竟是什么意思?
谢谢!
不清楚您指的是什么 20 个客户端限制以及客户端限制如何取决于频道或订阅者,但如果这是您的应用程序自己的限制,而不是听起来像是增加它,那将是最简单的解决方案。
ServiceStack.Redis 不支持在订阅开始后更改订阅的频道。与其自己管理 IRedisSubscription
,不如考虑 ServiceStack.Redis Managed Pub/Sub Server,它管理后台订阅线程,增加弹性并支持自动重试。
虽然您无法在运行时更改订阅的频道,但您可以修改 Channels
集合并重新启动订阅以创建对更新频道列表的新订阅,例如:
var pubSub = new RedisPubSubServer(clientsManager, chan1);
pubSub.Start();
//...
pubSub.Channels = new[] { chan1, chan2 };
pubSub.Restart();
否则,根据您的 use-case,您也许可以 subscribe to a channel pattern 订阅与通配符频道模式匹配的多个动态频道:
var pubSub = new RedisPubSubServer(clientsManager) {
ChannelsMatching = new[] { "chan:*" }
}
.Start();
它将在何处处理客户端发送的与通道模式匹配的任何消息。
我的,现在已经是“遗留”了,当它达到 20 个客户端限制时,使用 ServiceStack 快速 运行 实现了一个 pub/sub 解决方案。
我们做这样的事情:
_redisConsumer = MqClientFactory.Instance.GetRedisClient(); // Returns a IRedisClient
_subscription = _redisConsumer.CreateSubscription();
_subscription.OnSubscribe = channel => CoreLog.Instance.Info($"Subscription started on {eventChannelName}");
_subscription.OnUnSubscribe = channel => CoreLog.Instance.Warning($"Unsubscribed from {eventChannelName}");
_subscription.OnMessage = (channel, msg) =>
{
try
{
onMessageReceived(CoreRequestJsonEnvelope.CreateCoreRequestFromJson(msg));
}
catch (Exception ex)
{
CoreLog.Instance.Exception(ex);
}
};
// Since it blocks execution, we put this in a Task:
Task.Run(() =>
{
try
{
_subscription.SubscribeToChannels(eventChannelName); // blocking
}
catch(Exception e)
{
}
});
当我们也有足够多的不同频道可以收听时,它就用完了。
然后我想,也许我可以为所有订阅使用相同的 IRedisClient,而不是为每个订阅使用一个新的 IRedisClient,所以:
_redisConsumer = mySavedRedisClient;
...
但是那个returnsUnknown reply on multi-request
过了几个seconds/executions.
最后,我看了RedisPubSubServer
,但是好像需要在构造函数中指定通道,之后就改不了了。我确实需要在运行时添加和删除频道,而频道从一开始就不知道。
- 推荐的方法是什么?
- 是否增加最大限制并像以前一样继续?
- 是使用RedisPubSub,但是如何处理动态通道?
- “对多请求的未知回复”究竟是什么意思?
谢谢!
不清楚您指的是什么 20 个客户端限制以及客户端限制如何取决于频道或订阅者,但如果这是您的应用程序自己的限制,而不是听起来像是增加它,那将是最简单的解决方案。
ServiceStack.Redis 不支持在订阅开始后更改订阅的频道。与其自己管理 IRedisSubscription
,不如考虑 ServiceStack.Redis Managed Pub/Sub Server,它管理后台订阅线程,增加弹性并支持自动重试。
虽然您无法在运行时更改订阅的频道,但您可以修改 Channels
集合并重新启动订阅以创建对更新频道列表的新订阅,例如:
var pubSub = new RedisPubSubServer(clientsManager, chan1);
pubSub.Start();
//...
pubSub.Channels = new[] { chan1, chan2 };
pubSub.Restart();
否则,根据您的 use-case,您也许可以 subscribe to a channel pattern 订阅与通配符频道模式匹配的多个动态频道:
var pubSub = new RedisPubSubServer(clientsManager) {
ChannelsMatching = new[] { "chan:*" }
}
.Start();
它将在何处处理客户端发送的与通道模式匹配的任何消息。