rabbitmq 学习 之 parameter and policies(12)

https://www.rabbitmq.com/parameters.html

 

A RabbitMQ node can be configured using a number of mechanisms responsible for different areas:

MechanismDescription
Configuration File(s)defines server and plugin settings for and so on.
Environment Variablesdefine node name, file and directory locations, runtime flags taken from the shell, or set in the environment configuration file, rabbitmq-env.conf (Linux, MacOS, BSD) and rabbitmq-env-conf.bat (Windows)
rabbitmqctlWhen internal authentication/authorization backend is used, rabbitmqctl is the tool that manages virtual hosts, users and permissions.
rabbitmq-pluginsrabbitmq-plugins is the tool that manages enabled plugins.
Parameters and Policiesdefines cluster-wide settings which can change at run time as well as settings that are convenient to configure for groups of queues (exchanges, etc) such as including optional queue arguments.
Runtime (Erlang VM) FlagsControl lower-level aspects of the system: memory allocation settings, inter-node communication buffer size, runtime scheduler settings and more.
Operating System Kernel LimitsControl process limits enforced by the kernel: max open file handle limit, max number of processes and kernel threads, max resident set size and so on.

 

大部分配置都可以通过

  • 配置文件 rabbitmq.conf
  • 环境变量文件 rabbitmq-env.conf
  • 补充配置文件 advanced.config

这三个配置文件搞定了,

但是也有例外情况:

1 如果一个集群中的多个节点需要配置同样的内容

2 如果需要在运行时候修改 配置。

这个时候就需要通过 parameters 来搞定了。

 

parameters 可以 用rabbitmqctl 命令设置,也可以在web 页面设置

?2中参数,一种是 全局的,一种是 基于vhost 的

基于vhost 的包含三个属性 component name, a name and a value.

全局的就只有2个属性 a name and a value.

 

policies 是参数的一种特殊使用场景,用来设置  一组 exchange 和 queue 还有 Federation和 shovel  插件。基于vhost 的。

 

以下是操作命令

基于vhost

rabbitmqctlrabbitmqctl set_parameter {-p vhost} component_name name value
rabbitmqctl clear_parameter {-p vhost} component_name name
rabbitmqctl list_parameters {-p vhost}
HTTP APIPUT /api/parameters/component_name/vhost/name
DELETE /api/parameters/component_name/vhost/name
GET /api/parameters

全局的

rabbitmqctlrabbitmqctl set_global_parameter name value
rabbitmqctl clear_global_parameter name
rabbitmqctl list_global_parameters
HTTP APIPUT /api/global-parameters/name
DELETE /api/global-parameters/name
GET /api/global-parameters

 

为什么需要 policies?

在声明exchange or queue 的时候 ,除了强制要求的参数 durable exclusive autodelete 以外,客户端可能会添加好多其他的属性,

但是 在程序运行的时候,如果想要修改 某个属性 就需要修改代码,然后重新发布,这有点麻烦了,怎么办呢 ,policys 来解决

 

policy 怎么生效?

policy 属性介绍

  • name: it can be anything but ASCII-based names without spaces are recommended
  • pattern: a regular expression that matches one or more queue (exchange) names. Any regular expression can be used.
  • definition: a set of key/value pairs (think a JSON document) that will be injected into the map of optional arguments of the matching queues and exchanges
  • priority: see below

policy 去匹配满足条件的exchange和queue ,然后帮他们决定行为,一个队列或exchange 只会有一个 可用的policy,一个policy 内部可以 写入多条限制 (json格式)

 

apply-to 参数决定 policy 用在queue, exchange 还是both 

策略生效时间   添加完毕生效,然后queue or exchang 重新创建的时候 也会生效。

 

示例:

An example of defining a policy looks like:

rabbitmqctl
rabbitmqctl set_policy federate-me \
  "^amq\." '{"federation-upstream-set":"all"}' \
  --priority 1 \
  --apply-to exchanges
rabbitmqctl (Windows)
rabbitmqctl.bat set_policy federate-me ^
  "^amq\." "{""federation-upstream-set"":""all""}" ^
  --priority 1 ^
  --apply-to exchanges
HTTP API
PUT /api/policies/%2f/federate-me
                    {"pattern": "^amq\.",
                     "definition": {"federation-upstream-set":"all"},
                     "priority": 1,
                    "apply-to": "exchanges"}
Web UI
  • Navigate to Admin > Policies > Add / update a policy.
  • Enter "federate-me" next to Name, "^amq\." next to Pattern, and select "Exchanges" next to Apply to.
  • Enter "federation-upstream-set" = "all" in the first line next to Policy.
  • Click Add policy.

This matches the value "all" with the key "federation-upstream-set" for all exchanges with names beginning with "amq.", in the virtual host "/".

The "pattern" argument is a regular expression used to match exchange or queue names.

In the event that more than one policy can match a given exchange or queue, the policy with the greatest priority applies.

The "apply-to" argument can be "exchanges", "queues" or "all". The "apply-to" and "priority" settings are optional, in which case the defaults are "all" and "0" respectively.

组合策略

In some cases we might want to apply more than one policy definition to a resource. For example we might need a queue to be federated and mirrored. At most one policy will apply to a resource at any given time, but we can apply multiple definitions in that policy.

A federation policy definition would require an upstream set to be specified, so we would need the federation-upstream-set key in our definition. On the other hand to define some queues as mirrored, we would need the ha-mode key to be defined as well for the policy. Since the policy definition is just a JSON object, we can have both keys in the same policy definition.

Here's an example:

rabbitmqctl
rabbitmqctl set_policy ha-fed \
  "^hf\." '{"federation-upstream-set":"all","ha-mode":"all"}' \
  --priority 1 \
  --apply-to queues
rabbitmqctl (Windows)
rabbitmqctl set_policy ha-fed ^
  "^hf\." "{""federation-upstream-set"":""all"", ""ha-mode"":""all""}" ^
  --priority 1 ^
  --apply-to queues
HTTP API
PUT /api/policies/%2f/ha-fed
{"pattern": "^hf\.",
 "definition": {"federation-upstream-set":"all", "ha-mode": "all"},
 "priority": 1,
 "apply-to": "queues"}
Web UI
  • Navigate to Admin > Policies > Add / update a policy.
  • Enter "ha-fed" next to Name, "^hf\." next to Pattern, and select "Queues" next to Apply to.
  • Enter "federation-upstream-set" = "all" in the first line next to Policy.
  • Enter "ha-mode" = "all" on the next line.
  • Click Add policy.

By doing that all the queues matched by the pattern "^hf\." will have the "federation-upstream-set" and the "ha-mode" definitions applied to them.

 

以上讲述的是常规policy,还有一种policy 叫做 operator policy,他只能定义四个参数

  • expires
  • message-ttl
  • max-length
  • max-length-bytes

他和 常规policy 的关系是  互相补充+ 合并的 关系。

如果常规policy 和 operator policy 都设置了同一个 参数 ,那就以比较小的值为准。

rabbitmqctl
rabbitmqctl set_operator_policy transient-queue-ttl \
  "^amq\." '{"expires":1800000}' \
  --priority 1 \
  --apply-to queues
rabbitmqctl (Windows)
rabbitmqctl.bat set_operator_policy transient-queue-ttl ^
  "^amq\." "{""expires"": 1800000}" ^
  --priority 1 ^
  --apply-to queues
HTTP API
PUT /api/operator-policies/%2f/transient-queue-ttl
                        {"pattern": "^amq\.",
                         "definition": {"expires": 1800000},
                         "priority": 1,
                         "apply-to": "queues"}
Web UI
  • Navigate to Admin > Policies > Add / update an operator policy.
  • Enter "transient-queue-ttl" next to Name, "^amq\." next to Pattern, and select "Queues" next to Apply to.
  • Enter "expires" = 1800000 in the first line next to Policy.
  • Click Add policy.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值