Define rabbitmq policies in configuration file

丶灬走出姿态 提交于 2019-12-03 11:55:31

问题


I'd like to define mirroring for all my queues by default. I currently have to use rabbitmqctl once the node is up:

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

If one of my nodes fail, I'd have to remember to re-execute that code on restart.

Is there a way to automatically configure my node to use mirrored queues?


回答1:


Policy CAN be specified in a definition file, which can be referred to from your config file.

Example of how I have set a specific policy (not sure if ha can be specified in policy):

/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":{}}
    ]
}

I am sharing this config file and definitions file as it was impossible to figure it out from the RabbitMQ web site.

Note: This config worked on RabbitMQ 3.6.1 running on Ubuntu 14.04  




回答2:


I searched for this same thing and found this question. To add more details into the IvanD's answer, this how I did it:

First: sudo nano /etc/rabbitmq/rabbitmq.config (this command might be different depending on your OS)

[
  {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"}]
  }
].

Then create the additional json: sudo nano /etc/rabbitmq/rabbitmq_definitions.json (this command might be different depending on your OS) And it's contents:

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

IMPORTANT NOTE: The ha-sync-batch-size is supported only in rabbitmq versions above 3.6.0! https://www.rabbitmq.com/ha.html#sync-batch-size If your rabbit is older than that, remove the setting from the rabbitmq_definitions.json.

I'm using ubuntu 14.04 Trusty and Rabbitmq v.3.6.2.




回答3:


Policy can't be set in the rabbitmq.config file. One workaround is to start rmq using an init script and put the rabbitmqctl command inside there so that it is run whenever rmq starts or restarts.



来源:https://stackoverflow.com/questions/31662727/define-rabbitmq-policies-in-configuration-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!