在配置文件中定义rabbitmq策略

时间:2015-07-27 20:32:45

标签: rabbitmq rabbitmqctl

我想默认为我的所有队列定义镜像。一旦节点启动,我目前必须使用rabbitmqctl

rabbitmqctl set_policy ha-all "" '{"ha-mode":"all"}'

如果我的一个节点发生故障,我必须记得在重启时重新执行该代码。

有没有办法自动配置我的节点以使用镜像队列?

5 个答案:

答案 0 :(得分:13)

可以在定义文件中指定策略,该文件可以从配置文件中引用。

我如何设置特定策略的示例(不确定是否可以在策略中指定ha):

<强> /etc/rabbitmq/rabbitmq.config

[
{rabbit,
    [{vm_memory_high_watermark, 0.8}]
},
{rabbitmq_management,
    [{listener, [{port, 15672}]},
     {load_definitions, "/etc/rabbitmq/rabbitmq_definitions.json"},
     {http_log_dir, "/var/log/rabbitmq/management_http.log"}]
}
].

<强> /etc/rabbitmq/rabbitmq_definitions.json

{       "users":[
            {"name":"rabbot","password_hash":"Cvse5iGOg20UqUq7Za9D1tatOJnMVDru4GHtxqc02g7zj5ur","tags":""},
            {"name":"rabnet","password_hash":"CqqG2fwvH6xz64NpibGJx2M7ZCyFnR1BQBM+C0KH2qRPmVxF","tags":"administrator"}],
    "vhosts":[
            {"name":"/"}],
    "permissions":[
            {"user":"viabot","vhost":"VIA","configure":".*","write":".*","read":".*"},
            {"user":"vianet","vhost":"VIA","configure":".*","write":".*","read":".*"}],
    "parameters":[],
    "policies":[
            {"vhost":"VIA","name":"DLX","pattern":".*","apply-to":"queues","definition":{"dead-letter-exchange":"dead_letter"},"priority":0}
            ],
    "queues":[
            {"name":"store_to_es","vhost":"VIA","durable":true,"auto_delete":false,"arguments":{}},
            {"name":"store_to_mongodb","vhost":"VIA","durable":true,"auto_delete":false,"arguments":{}}
            ],
    "exchanges":[
            {"name":"data_incoming","vhost":"VIA","type":"fanout","durable":true,"auto_delete":false,"internal":false,"arguments":{}},
            {"name":"sms_incoming","vhost":"VIA","type":"fanout","durable":true,"auto_delete":false,"internal":false,"arguments":{}}
            ],
    "bindings":[
            {"source":"data_incoming","vhost":"VIA","destination":"store_to_es","destination_type":"queue","routing_key":"","arguments":{}},
            {"source":"sms_incoming","vhost":"VIA","destination":"store_to_mongodb","destination_type":"queue","routing_key":"","arguments":{}}
    ]
}

我正在分享这个配置文件和定义文件,因为无法从RabbitMQ网站上找到它。

注意:此配置适用于在Ubuntu 14.04上运行的RabbitMQ 3.6.1

答案 1 :(得分:4)

我搜索了同样的东西,发现了这个问题。要在IvanD的答案中添加更多细节,我就这样做了:

首先:sudo nano /etc/rabbitmq/rabbitmq.config(此命令可能因您的操作系统而异)

[
  {rabbit,
    [
        {default_vhost,<<"/">>},
        {default_user,<<"someuser">>},
        {default_pass,<<"somepassword">>},
        {default_permissions, [<<".*">>, <<".*">>, <<".*">>]},
        {default_user_tags, [administrator]}
    ]
  },
  {rabbitmq_management,
    [{listener, [{port, 15672}]},
        {load_definitions, "/etc/rabbitmq/rabbitmq_definitions.json"},
        {http_log_dir, "/var/log/rabbitmq/management_http.log"}]
  }
].

然后创建额外的json:sudo nano /etc/rabbitmq/rabbitmq_definitions.json(此命令可能因操作系统而异) 它的内容是:

{
  "vhosts":[
        {"name":"/"}
  ],
  "policies":[
        {"vhost":"/","name":"ha","pattern":"", "definition":{"ha-mode":"all","ha-sync-mode":"automatic","ha-sync-batch-size":5}}
  ]
}

重要说明:仅在3.6.0以上的rabbitmq版本中支持ha-sync-batch-size! https://www.rabbitmq.com/ha.html#sync-batch-size 如果您的兔子比那个旧,请从rabbitmq_definitions.json

中删除该设置

我正在使用ubuntu 14.04 Trusty和Rabbitmq v.3.6.2。

答案 2 :(得分:1)

政策无法在rabbitmq.config文件中设置。一种解决方法是使用init脚本启动rmq并将rabbitmqctl命令放在其中,以便在rmq启动或重新启动时运行它。

答案 3 :(得分:1)

最后,我发现了一些可行的方法: 不需要configMap或rabbitmq.config文件。 在

containers:
...
...
...

    lifecycle:
              postStart:
                exec:
                  command: ["/bin/sh","-c","rabbitmq-plugins --offline enable rabbitmq_management;until rabbitmqctl node_health_check; do  sleep 5;done;rabbitmqctl set_policy ha-all \".\" '{\"ha-mode\":\"all\", \"ha-sync-mode\":\"automatic\"}' --apply-to all --priority 0;"]

答案 4 :(得分:1)

是的,您可以使用definition import at load time 在重新启动时直接加载策略,队列,交换,绑定,用户等。

  1. 预配置兔子

    转到orders“管理”->“策略”页面并创建所需的新策略:

    Creating new policy

    您还可以预先配置队列和其他内容。

  2. 转储定义

    shipped_volume > 0

    您还可以使用Web界面转储定义。打开概述标签,向下滚动:

    Downloading definitions

    (!!!)更改http://localhost:15672,以便保留仅在重新启动时所需的内容。

    将有与您的政策有关的部分,请保留:

    curl -s -H "Accept:application/json" -u guest:guest http://localhost:15672/api/definitions > definitions.json
  3. 重新启动时加载定义

    definitions.json放在兔子附近,然后将此行添加到... "policies": [ { "vhost": "/", "name": "ha-all", "pattern": "", "apply-to": "all", "definition": { "ha-mode": "all" }, "priority": 0 } ] ... 。无需使用旧格式:

    definitions.json

要在重启后加载某些内容,请使用cli tools


向@IvanD表示敬意,因为我的答案与his own几乎相同,但揭示了详细的步骤和新的配置格式用法。评论中没有足够的空间。