主题筛选器和操作Topic filters and actions

订阅者可以定义他们希望从主题接收的消息。Subscribers can define which messages they want to receive from a topic. 这些消息采用一个或多个命名订阅规则的形式指定。These messages are specified in the form of one or more named subscription rules. 每个规则都包含一个选择特定的消息的条件和一个对所选消息进行批注的操作。Each rule consists of a condition that selects particular messages and an action that annotates the selected message. 对于每个匹配规则条件,订阅会生成消息的副本,这对于每个匹配规则可能会以不同方式进行批注。For each matching rule condition, the subscription produces a copy of the message, which may be differently annotated for each matching rule.

每个新创建的主题订阅都具有初始默认订阅规则。Each newly created topic subscription has an initial default subscription rule. 如果未显式指定规则的筛选条件,则应用的筛选器是 true 筛选器,通过它可以将所有消息都选择到订阅中。If you don't explicitly specify a filter condition for the rule, the applied filter is the true filter that enables all messages to be selected into the subscription. 默认规则没有关联批注操作。The default rule has no associated annotation action.

服务总线支持三个筛选条件:Service Bus supports three filter conditions:

  • 布尔筛选器 - 通过 TrueFilterFalseFilter 可以为订阅选择所有到达消息 (true) 或不选择任何到达消息 (false)。Boolean filters - The TrueFilter and FalseFilter either cause all arriving messages (true) or none of the arriving messages (false) to be selected for the subscription.

  • SQL 筛选器 - SqlFilter 包含类似 SQL 的条件表达式,它会在代理中针对到达的消息的用户定义属性和系统属性进行计算。SQL Filters - A SqlFilter holds a SQL-like conditional expression that is evaluated in the broker against the arriving messages' user-defined properties and system properties. 所有系统属性在条件表达式中必须带有前缀 sys.All system properties must be prefixed with sys. in the conditional expression. 筛选条件的 SQL 语言子集可测试属性是否存在 (EXISTS),以及是否为 null 值 (IS NULL)、逻辑“非”/“与”/“或”、关系运算符、简单数值算术和简单文本模式匹配(使用 LIKE)。The SQL-language subset for filter conditions tests for the existence of properties (EXISTS), null-values (IS NULL), logical NOT/AND/OR, relational operators, simple numeric arithmetic, and simple text pattern matching with LIKE.

  • 相关筛选器 - CorrelationFilter 包含一组条件,这些条件按照到达消息的一个或多个用户和系统属性进行匹配。Correlation Filters - A CorrelationFilter holds a set of conditions that are matched against one or more of an arriving message's user and system properties. 常见的用法是根据 CorrelationId 属性进行匹配,但应用程序也可以选择根据以下属性进行匹配:A common use is to match against the CorrelationId property, but the application can also choose to match against the following properties:

    • ContentTypeContentType
    • LabelLabel
    • MessageIdMessageId
    • ReplyToReplyTo
    • ReplyToSessionIdReplyToSessionId
    • SessionIdSessionId
    • ToTo
    • 任何用户定义的属性。any user-defined properties.

    当到达消息的某个属性值等于相关筛选器中指定的值时,便存在匹配。A match exists when an arriving message's value for a property is equal to the value specified in the correlation filter. 对于字符串表达式,比较会区分大小写。For string expressions, the comparison is case-sensitive. 指定多个匹配属性时,筛选器会将它们合并为逻辑“与”条件,这意味着若要使筛选器匹配,所有条件都必须匹配。When specifying multiple match properties, the filter combines them as a logical AND condition, meaning for the filter to match, all conditions must match.

所有筛选器都会计算消息属性。All filters evaluate message properties. 筛选器无法评估消息正文。Filters can't evaluate the message body.

复杂筛选器规则需要处理能力。Complex filter rules require processing capacity. 具体而言,使用 SQL 筛选规则会导致订阅、主题和命名空间级别的总体消息吞吐量降低。In particular, the use of SQL filter rules cause lower overall message throughput at the subscription, topic, and namespace level. 应用程序应尽可能选择关联筛选器而不是类似 SQL 的筛选器,因为它们的处理效率要高得多,并且对吞吐量的影响较小。Whenever possible, applications should choose correlation filters over SQL-like filters because they're much more efficient in processing and have less impact on throughput.

操作Actions

使用 SQL 筛选条件,可以定义可通过添加、删除或替换属性及其值对消息进行批注的操作。With SQL filter conditions, you can define an action that can annotate the message by adding, removing, or replacing properties and their values. 操作使用类似 SQL 的表达式,该表达式有一点依赖于 SQL UPDATE 语句语法。The action uses a SQL-like expression that loosely leans on the SQL UPDATE statement syntax. 在消息匹配之后,在将消息选入订阅之前,对消息执行操作。The action is done on the message after it has been matched and before the message is selected into the subscription. 对消息属性进行的更改是复制到订阅中的消息所专有的。The changes to the message properties are private to the message copied into the subscription.

使用模式Usage patterns

主题的最简单使用方案是每个订阅都获取发送到主题的每个消息的副本,这样可实现广播模式。The simplest usage scenario for a topic is that every subscription gets a copy of each message sent to a topic, which enables a broadcast pattern.

筛选器和操作可实现更进一步的两组模式:分区和路由。Filters and actions enable two further groups of patterns: partitioning and routing.

分区使用筛选器以可预测且互相排斥的方式在多个现有主题订阅间分发消息。Partitioning uses filters to distribute messages across several existing topic subscriptions in a predictable and mutually exclusive manner. 当系统进行扩大以在功能相同的隔离舱(每个隔离舱包含总体数据的子集;例如客户配置文件信息)中处理许多不同上下文时,可使用分区模式。The partitioning pattern is used when a system is scaled out to handle many different contexts in functionally identical compartments that each hold a subset of the overall data; for example, customer profile information. 借助分区,发布者可将消息提交到主题中,而无需了解分区模型。With partitioning, a publisher submits the message into a topic without requiring any knowledge of the partitioning model. 消息随后会移动到正确的订阅,分区的消息处理程序随后会从该订阅检索它。The message then is moved to the correct subscription from which it can then be retrieved by the partition's message handler.

路由使用筛选器以可预测、但不一定独占的方式在主题订阅间分发消息。Routing uses filters to distribute messages across topic subscriptions in a predictable fashion, but not necessarily exclusive. 通过与自动转发功能相结合,主题筛选器可以用于在服务总线命名空间中创建复杂路由图,以便在 Azure 区域中进行消息分发。In conjunction with the auto forwarding feature, topic filters can be used to create complex routing graphs within a Service Bus namespace for message distribution within an Azure region. 通过使 Azure Functions 或 Azure 逻辑应用充当 Azure 服务总线命名空间之间的桥梁,可以创建直接集成到业务线应用程序中的复杂全局拓扑。With Azure Functions or Azure Logic Apps acting as a bridge between Azure Service Bus namespaces, you can create complex global topologies with direct integration into line-of-business applications.

备注

由于 Azure 门户现在支持 Service Bus Explorer 功能,因此可以从门户创建或编辑订阅筛选器。Because the Azure portal now supports Service Bus Explorer functionality, subscription filters can be created or edited from the portal.

后续步骤Next steps

请参阅以下示例:See the following samples: