寻求对ServiceStack.Redis的理解:IRedisClient.PublishMessage vs IMessageQueueClient.Publish

Seeking an understanding of ServiceStack.Redis: IRedisClient.PublishMessage vs IMessageQueueClient.Publish

我很难区分 IRedisClient.PublishMessageIMessageQueueClient.Publish,我意识到我一定是搞混了。

ServiceStack 为我们提供了监听 pub/sub 广播的选项,如下所示:

    static IRedisSubscription _subscription;
    static IRedisClient redisClientSub;
    static int received = 0;
    static void ReadFromQueue()
    {
        redisClientSub = redisClientManager.GetClient();
        _subscription = redisClientSub.CreateSubscription();

        _subscription.OnMessage = (channel, msg) =>
        {
            try
            {
                received++;
            }
            catch (Exception ex)
            {
                    
            }
        };

        Task.Run(() => _subscription.SubscribeToChannels("Test"));
    }

看起来不错,简单明了。但是制作人呢?

在查看可用的 类 时,我认为可以使用 IRedisClient.PublishMessage(string toChannel, string message)IMessageQueueClient.Publish(string queueName, IMessage message)

redisClient.PublishMessage("Test", json);
// or:     
myMessageQueueClient.Publish("Test", new Message<CoreEvent>(testReq));

在这两种情况下,您都可以自己指定频道名称。这是我看到的行为:

我想了解一下两者的区别。我查看了源代码并尽我所能阅读,但我没有找到任何关于 IRedisClient.PublishMessage.

的文档

Mythz 在 ServiceStack 论坛上回答了这个问题,here

他写道:

These clients should not be used interchangeably, you should only be using ServiceStack MQ clients to send MQ Messages or the Message MQ Message wrapper.

The redis subscription is low level API to create a Redis Pub/Sub subscription, a more useful higher level API is the Managed Pub/Sub Server which wraps the pub/sub subscription behind a managed thread.

Either way, MQ Server is only designed to process messages from MQ clients, if you’re going to implement your own messaging implementation use your own messages & redis clients not the MQ clients or MQ Message class.

No IRedisClient (& ServiceStack.Redis) APIs are for Redis Server, the PublishMessage API sends the redis PUBLISH command. IRedisSubscription creates a Redis Pub/Sub subscription, see Redis docs to learn how Redis Pub/Sub works. The ServiceStack.Redis library and all its APIs are just for Redis Server, it doesn’t contain any ServiceStack.Messaging MQ APIs.

So just use ServiceStack.Redis for your custom Redis Pub/Sub subscription implementation, i.e. don’t use any ServiceStack.Messaging APIs which is for ServiceStack MQ only.