繁体   English   中英

如何仅向一个 Azure 服务总线订阅发送消息?

[英]How to send a message to only one Azure Service Bus Subscription?

我有以下场景:我有 2 个微服务订阅 Azure 服务总线,一个微服务发送推送通知,另一个微服务发送电子邮件,这些微服务永远不会一起工作,所以消息只能由一个微服务执行。

我正在搜索并且看起来将消息发送到主题并有一个过滤器来选择应该执行操作的微服务是一个好主意。

所以我有两个问题:

我应该使用主题和过滤器吗? 不确定是否是更好的做法。

如果是,是否存在将消息发送到正确订阅的方法? 例如,我向订阅 X 发布一条消息,而不是为所有订阅发布。

在此处输入图像描述

谢谢

我应该使用主题和过滤器吗? 不确定是否是更好的做法。

是的。 这是主题和订阅的经典用例。

如果是,是否存在将消息发送到正确订阅的方法? 例如,我向订阅 X 发布一条消息,而不是为所有订阅发布。

发布的工作方式是将其发布到主题,主题获取消息并针对所有订阅的过滤器进行验证。 具有满足过滤条件的订阅会收到该消息。 不满足过滤条件的订阅将被跳过。 滤波器共有三种类型,Boolean、SQL 和 Correlation。 Boolean 过滤器与您不太相关。 相关或 SQL 过滤器将完成这项工作。 您可以在我的帖子中找到有关过滤器的更多信息。

我在这里建议的是用通知的方法“标记”每条消息。 由于您将为每个方法使用一个通知方法,因此 Correlation 过滤器将是最简单和最有效的。 通知方法值可以分配给传出消息的Label系统属性和两个订阅的相关过滤器(一个用于 email 服务,一个用于推送通知服务)。 例如(伪代码):

var message = new Message();

// custom logic to determine what notification method to use

if (notificationMethod == NotificationMethod.Email)
{
  message.Label = "email";
}
else
{
  message.Label = "push";
}

// publish the message to the topic

两个过滤器设置将是:

// email notification service subscription
var emailFilter = new CorrelationFilter();
filter.Label = "email";

// push notifications service subscription
var pushFilter = new new CorrelationFilter();
filter.Label = "push";

使用给定过滤器创建订阅(例如电子邮件):

var management = new ManagementClient(...);
await management.CreateSubscriptionAsync(new SubscriptionDescription("topic", "subscription"));

var client = new SubscriptionClient("topic", "subscription");
await client.AddRuleAsync(new RuleDescription(
{
  Filter = emailFilter,
  Name = "email filter"
}));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM