【原创】RabbitMQ 之 Federation 插件(翻译)

发布于 2016/04/19 13:30
字数 3003
阅读 425
收藏 2

Federation Plugin



The high-level goal of the federation plugin is to transmit messages between brokers without requiring clustering. This is useful for various reasons:
federation 插件的 high level 目标是,在不同 broker 之间进行消息传递而无需建立集群;该功能在很多场景下非常有用:

Loose coupling
The federation plugin can transmit messages between brokers (or clusters) in different administrative domains:
federation 插件能够在分属不同管理域的 broker 或 cluster 之间传递消息:

  • they may have different users and virtual hosts;
    他们可能设置了不同的 user 和 vhost ;
  • they may run on different versions of RabbitMQ and Erlang.
    他们可能运行在不同版本的 RabbitMQ 和 Erlang 上;

WAN 友好性
The federation plugin uses AMQP 0-9-1 to communicate between brokers, and is designed to tolerate intermittent connectivity.
federation 插件基于 AMQP 0-9-1 协议在不同 broker 之间进行通信,并设计成能够容忍不稳定的网络连通情况;

A broker can contain federated and local-only components - you don't need to federate everything if you don't want to.
一个 broker 可以同时包含 federated 组件和 local-only 组件;你只需要对特定组件创建 federation ,而其他组件维持原状;

Federation does not require O(n2) connections between n brokers (although this is the easiest way to set things up), which should mean it scales better.
federation 不需要在 n 个 broker 之间建立 O(n^2) 个连接(尽管这是最简单的使用模式),这也就意味着 federation 在使用时更容易扩展;

What Does It Do?

The federation plugin allows you to make exchanges and queues federated. A federated exchange or queue can receive messages from one or more upstreams (remote exchanges and queues on other brokers). A federated exchange can route messages published upstream to a local queue. A federated queue lets a local consumer receive messages from an upstream queue.

federation 插件允许你将多个 exchange 或多个 queue 进行 federate ;
federated exchange 或 federated queue 能够从一个或多个 upstream 接收到消息;
upstream 是指位于其他 broker 上的、远端 exchange 和 queue ;
federated exchange 能够将发给 upstream 的消息路由到本地的某个 queue 中;
federated queue 则允许一个本地消费者接收到来自 upstream queue 的消息;

Federation links connect to upstreams using RabbitMQ Erlang client. Therefore they can connect to a specific vhost, use TLS, use multiple authentication mechanisms.
通过 federation 与 upstream 建立的连接是基于 RabbitMQ 的 Erlang 客户端实现的;故该连接是针对特定的 vhost 的,并且使用使用 TLS 以及多种鉴权机制

For more details, see the documentation on federated exchanges and federated queues.
若想获取更多细节信息, 请参考 federated exchanges 和 federated queues 文档。

How is Federation Set Up?
如何建立 Federation 模型?

To use federation, one needs to configure two things
为了使用 federation 功能,需要配置一下两个内容。

One or more upstreams that define federation connections to other nodes. This can be done via runtime parameters or the federation management plugin which adds a federation management tab to the management UI.
需要配置一个或多个 upstream ,每个 upstream 均定义了到其他节点的 Federation 连接;
配置行为可以 通过运行时 parameter 完成,也可以通过 federation management 插件 成;

One or more policies that match exchanges/queues and makes them federated.
需要定义匹配 exchange/queue 的一种或多种 policy ,并令其建立 federation ;

Getting Started

The federation plugin is included in the RabbitMQ distribution. To enable it, use rabbitmq-plugins:
federation 插件默认包含在 RabbitMQ 发布包中;若要使能该功能, 需要使用 rabbitmq-plugins 脚本
rabbitmq-plugins enable rabbitmq_federation
When using the management plugin, you will also want to enable rabbitmq_federation_management:
当使用该 management 插件时,同样需要使能 rabbitmq_federation_management 插件:
rabbitmq-plugins enable rabbitmq_federation_management
When using a federation in a cluster, all the nodes of the cluster should have the federation plugin enabled.
当在集群中使用 federation 功能时,集群中的所有节点都应该将这两插件使能;

Information about federation upstreams is stored in the RabbitMQ database, along with users, permissions, queues, etc. There are three levels of configuration involved in federation:
关于 federation upstream 的信息全都保存在 RabbitMQ 的数据库中,其中包括了 user 信息、permission 信息、queue 信息等等;
在 federation 中存在 3 种界别的配置:
  • Upstreams: each upstream defines how to connect to another broker.
                      每一个 upstream 用于定义如何与另外的 broker 建立连接;
  • Upstream sets: each upstream set groups together a set of upstreams to use for federation.
                            每一个 upstream set 用于针对一系列使用 federation 功能 upstream 进行了分组;
  • Policies: each policy selects a set of exchanges, queues or both, and applies a single upstream or an upstream set to those objects. 
                 每一种 policy 会限定(过滤)出一组 exchange ,或者一组 queue ,或者同时针对两者进行限定;policy 最终将作用于一个单独的 upstream 上,或者一个 upstream set 上,并对其他上对象发回作用;

In practice, for simple use cases you can almost ignore the existence of upstream sets, since there is an implicitly-defined upstream set called all to which all upstreams are added.
实际上,在简单使用场景下,你基本上可以忽略 upstream set 的存在,因为存在一种名为 all 的、隐式定义的 upstream set ,并且所有 upstream 都会添加到这个 set 中;

Upstreams and upstream sets are both instances of runtime parameters. Like exchanges and queues, each virtual host has its own distinct set of parameters and policies. For more generic information on parameters and policies, see the documentation on parameters and policies. For full details on the parameters used by federation, see the federation reference.
upstream 和 upstream set 都属于运行时 parameter 的实例;
就像 exchange 和 queue 一样,每一个 vhost 都持有不同的 parameter 和 policy 的集合;
针对 parameter 和 polic y 的更多信息,可以参考文档 parameters 与 policies
若想获取更多与 federation 相关的 parameter 详细信息,可以参考文档 federation reference 

Parameters and policies can be set in three ways - either with an invocation of rabbitmqctl, a call to the management HTTP API, or (usually) through the web UI presented by rabbitmq_federation_management. (The web UI does not present all possibilities - in particular, it does not allow you to manage upstream sets.)
parameter 和 policy 可以通过 3 种方式进行设置:
  • 通过 rabbitmqctl 脚本;
  • 通过 management 插件提供的 HTTP API ;
  • 通过 rabbitmq_federation_management 插件提供的 Web UI(更通用的方式);注意:基于 Web UI 的方式不能提供全部功能,尤其无法针对 upstream set 进行管理;

A simple example

Here we will federate all the built-in exchanges except for the default exchange, with a single upstream. The upstream will be defined to buffer messages when disconnected for up to one hour (3600000ms).
下面的例子中,我们会将全部内置 exchange 配置成 federate 到一个 upstream 上,除了默认 exchange 以外;而该 upstream 将被定义成当连接断开时用于消息缓冲的地方(最多保存 1 小时的消息);

Define an upstream:
定义 upstream :
rabbitmqctl set_parameter federation-upstream my-upstream \
rabbitmqctl (Windows)
rabbitmqctl set_parameter federation-upstream my-upstream ^
PUT /api/parameters/federation-upstream/%2f/my-upstream
Web UI
Navigate to Admin > Federation Upstreams > Add a new upstream. Enter "my-upstream" next to Name, "amqp://server-name" next to URI, and 36000000 next to Expiry. Click Add upstream.
Then define a policy that will match built-in exchanges and use this upstream:
之后,定义一个 policy 用于匹配全部的内置 exchange ,并使用该 upstream :
rabbitmqctl set_policy --apply-to exchanges federate-me "^amq\." \
rabbitmqctl (Windows)
rabbitmqctl set_policy --apply-to exchanges federate-me "^amq\." ^
PUT /api/policies/%2f/federate-me
{"pattern":"^amq\.", "definition":{"federation-upstream-set":"all"}, \
Web UI
Navigate to Admin > Policies > Add / update a policy. Enter "federate-me" next to "Name", "^amq\." next to "Pattern", choose "Exchanges" from the "Apply to" drop down list and enter "federation-upstream-set" = "all" in the first line next to "Policy". Click "Add" policy.

We tell the policy to federate all exchanges whose names begin with "amq." (i.e. all the built in exchanges except for the default exchange) with (implicit) low priority, and to federate them using the implicitly created upstream set "all", which includes our newly-created upstream. Any other matching policy with a priority greater than 0 will take precedence over this policy. Keep in mind that federate-me is just a name we used for this example, you can use any string you want there.
上面通过定义 policy 将全部以 "amq." 作为名字开头的 exchange 按照 low priority 进行了 federate 处理,即将这些 exchange 都 federate 到了隐式创建的 upstream set "all" 上,这其中也会包含我们新创建的 upstream ;
任何其他可以匹配的 policy 若含有大于 0 的优先级,则具有比当前 policy 更高的优先级;

The built in exchanges should now be federated because they are matched by the policy. You can check that the policy has applied to the exchanges by checking the exchanges list in management or with:
现在,所有内置的 exchange 都应该建立了 federation ,因为他们都能匹配上面的 policy ;
可以通过 management 插件中的 exchange 列表,或者下面的命令输出,确认上述 policy 已经作用到了 exchange 上;
rabbitmqctl list_exchanges name policy | grep federate-me
And you can check that federation links for each exchange have come up with Admin > Federation Status > Running Links or with:
另外,你还可以通过 Admin > Federation Status > Running Links 查看针对每个 exchange 的 federation 连接:
rabbitmqctl eval 'rabbit_federation_status:status().'
In general there will be one federation link for each upstream that is applied to an exchange. So for example with three exchanges and two upstreams for each there will be six links.
通常情况下,针对每个 upstream 都会有一条 federation 连接,该 federation 连接对应到一个 exchange 上;例如 3 个 exchange 与 2 个 upstream 分别建立 federation 的情况下,会有 6 条连接。

For simple use this should be all you need - you will probably want to look at the AMQP URI reference. The federation reference contains more details on setting up upstreams and upstream sets.
对于简单使用来说,上述内容就是你需要的了。其余的信息可以查看一下 AMQP URI reference 的内容;
关于建立 upstream 和 upstream set 的更多信息可以查看 federation reference 。

Federating Clusters
针对集群进行 federation

Clusters can be linked together with federation just as single brokers can. To summarise how clustering and federation interact:
集群之间可以通过 federation 连接在一起,就像针对单个 broker 操作一样;下面描述一下集群和 federation 的交互过程:

  • You can define policies and parameters on any node in the downstream cluster; once defined on one node they will apply on all nodes.
    你可以属于 downstream 集群的任意一个节点上定义 policy 和 parameter ;只要在一个节点上进行了定义,其内容就可以在所有节点上生效;
  • Exchange federation links will start on any node in the downstream cluster. They will fail over to other nodes if the node they are running on crashes or stops.
    Exchange federation 连接可以创建于属于 downstream 集群的任意节点上;这些连接将会在自身所属节点崩溃或停止时,失效转移到其他节点。
  • Queue federation links will start on the same node as the downstream queue. If the downstream queue is mirrored, they will start on the same node as the master, and will be recreated on the same node as the new master if the node the existing master is running on crashes or stops.
    Queue federation 连接创建于和 downstream queue 所属相同的节点上;如果 downstream queue 被镜像了,这些连接将会重建于作为 master 的相同节点上,并且会在当前运行中的 master 所在节点发生崩溃或者停止时 被作为新的 master 重新创建于相同节点上。
  • To connect to an upstream cluster, you can specify multiple URIs in a single upstream. The federation link process will choose one of these URIs at random each time it attempts to connect.
    为了连接到 upstream 集群,你可以在一个单独的 upstream 中指定多个 URI ;federation 连接处理进程会在每次进行连接时,随机选择这些 URI 中的一个;
Securing Federation Connections with TLS
基于 TLS 加密 federation 连接

Federation connections (links) can be secured with TLS. Because Federation uses a RabbitMQ client under the hood, it is necessary to both configure source broker to listen for TLS connections and Federation/Erlang client to use TLS.
federation 连接可以通过 TLS 进行加密;因为 federation 是基于 RabbitMQ 的 Erlang 客户端实现的,所以有必要同时配置 source brok er 监听 TLS 连接,以及 f ederation/Erlang 客户端使用 TLS 进行连接。

To configure Federation to use TLS, one needs to
配置基于 TLS 的 federation 需要执行
  • Use the amqps URI scheme instead of amqp
    使用 amqps 作为 URI scheme ,而不是 amqp
  • Specify CA certificate and client certificate/key pair via URI query parameters when configuring upstream(s)
    在配置 upstream 时,通过 URI 查询参数指定 CA 证书,以及客户端证书/密钥对信息;
  • Configure Erlang client to use TLS
    配置 Erlang 客户端使用 TLS
Just like with "regular" client connections, server's CA should be trusted on the node where federation link(s) runs, and vice versa.
正如“常规”客户端连接一样,服务器提供的 CA 需要在使用 federation 连接的节点上被信任;反之亦然。

Getting Help

If you have any questions or comments regarding RabbitMQ, feel free to ask them on RabbitMQ mailing list.

© 著作权归作者所有

共有 人打赏支持
粉丝 368
博文 534
码字总数 952694
作品 0
私信 提问
【原创】RabbitMQ 之 Plugins(翻译)

为了方便工作中使用,对 RabbitMQ 的【插件】相关文档进行了翻译,鉴于自己水平有限,翻译中难免有纰漏产生,如果疑问,欢迎指出探讨。此文以中英对照方式呈现。 官方原文:http://www.rabb...

RabbitMQ 3.7.0 Milestone 14 发布

RabbitMQ 3.7.0 Milestone 14 发布了,这是预计在 2017 年上半年发布的版本的功能预览版。 值得注意的变化有: Minimum required Erlang/OTP version is now 18.3. Recent Erlang versions ...

RabbitMQ 3.7.0 Milestone 13 发布

RabbitMQ 3.7.0 Milestone 13 发布了,这是预计在 2017 年上半年发布的版本的功能预览。 有以下值得关注的更新: Minimum required Erlang/OTP version is now 18.3. Recent Erlang version...

RabbitMQ 3.6.1 RC2 发布

RabbitMQ 3.6.1 RC2 发布了,下载地址: https://github.com/rabbitmq/rabbitmq-server/releases/tag/rabbitmq_v3_6_1_rc2 完整改进记录: Server Bug Fixes Windows installer failed to c......

RabbitMQ 3.7.0 Milestone 11 发布

RabbitMQ 3.7.0 Milestone 11 发布了。这是预计在 2017 年上半年发布的功能版本的预览。部分更新内容如下: Breaking Changes Minimum required Erlang/OTP version is now 18.3. Recent Erl...






数据库的操作总结就是:增删改查(CURD),今天记录一下基础的检索查询工作。 检索MySQL 1.查询表中所有的记录 mysql> select * from apps;+----+------------+-----------------------+------...




摘要: 最强NLP模型谷歌BERT狂破11项纪录,全面超越人类,本文通过可视化带你直观了解它。 2018年是自然语言处理(Natural Language Processing, NLP)领域的转折点,一系列深度学习模型在智...


public void downloadD(HttpServletRequest request, HttpServletResponse res,String contractName, String contractPath) throws IOException {// FileAttach fileAttach = fileA......

gradle 打包可执行jar包

group 'android.com'version '1.0-SNAPSHOT'apply plugin: 'java'sourceCompatibility = 1.8repositories { mavenCentral()}jar { manifest { attributes ('Main-......