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

标签 azure azureservicebus

我有以下场景:我在 Azure 服务总线中订阅了 2 个微服务,一个用于发送推送通知,另一个用于发送电子邮件,这些微服务永远不会一起工作,因此该消息将仅由一个微服务执行。

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

所以我有两个问题:

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

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

enter image description here

谢谢

最佳答案

Should I use Topics and filters? Not sure if's the better practices.

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

If yes, exist a way to send the message to the correct subscription? For example, I publish a message to subscription X instead of publishing for all subscriptions.

发布的工作方式是将其发布到主题,主题获取消息并根据所有订阅的过滤器进行验证。满足过滤条件的订阅会收到消息。不满足过滤条件的订阅将被跳过。过滤器分为三种类型: bool 型、SQL 型和关联型。 bool 过滤器与您不太相关。相关或 SQL 过滤器可以完成这项工作。您可以在我的post中找到有关过滤器的更多信息.

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

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"
}));

关于azure - 如何仅向一个 Azure 服务总线订阅发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61393873/

相关文章:

c# - 在 azure 函数中获取队列名称或主题+订阅者名称(Azure 服务总线触发函数)

c# - 将 WindowsAzure.ServiceBus 迁移到 Azure.Messaging.ServiceBus

azure - 使用托管身份将 API 连接到服务总线的 ARM 模板

c# - 如何为 Azure 应用服务上的每个请求设置自定义 header ?

c# - 使用 Azure 的 Windows 8 和 Windows Phone 推送通知

azure - 如何更改Azure上的API应用程序的域

c# - Azure AD - HTTP/1.1 401 未经授权 - Windows 服务

azure - Azure DevOps 上的 Terraform 和 azurerm 后端

apache-kafka - Azure 服务总线上的 Kafka 表相当于什么?

c# - Microsoft ServiceBus - 参数实体名称为 null 或为空