欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

rabbitmq配置

程序员文章站 2022-07-12 13:10:57
...

项目中使用的,肯定好用的,谁用谁知道

RabbitMQ是流行的开源消息队列系统,是AMQP(Advanced Message Queuing Protocol高级消息队列协议)的标准实现,用erlang语言开发。RabbitMQ据说具有良好的性能和时效性,同时还能够非常好的支持集群和负载部署,非常适合在较大规模的分布式系统中使用,具体特性还在验证中,待检验。由于项目需要,安装并配置了RabbitMQ,服务器操作系统是CentOS7,服务器是虚拟机。

具体步骤如下:

1 安装依赖文件:

 yum install gcc glibc-devel make ncurses-devel openssl-devel xmlto


2.Erlang安装配置

  下载安装包,地址http://www.erlang.org/downloads,我选择的是otp_src_18.3.tar.gz。需要对erlang有所了解。

  然后解压文件:


[[email protected] rabbitmq]# tar -xzvf otp_src_18.3.tar.gz
[[email protected] rabbitmq]# cd otp_src_18.3/

  配置安装路径编译代码:

[[email protected] otp_src_18.3]# ./configure --prefix=/opt/erlang

  执行编译结果:

[[email protected] otp_src_18.3]# make && make install

   然后在配置Erlang环境变量,vi /etc/profile文件,增加下面的环境变量:

#set erlang environment
export PATH=$PATH:/opt/erlang/bin

  source  /etc/profile使得文件生效

完成后进入/opt/erlang查看执行结果

[[email protected] rabbitmq]# cd /opt/erlang/
[[email protected] erlang]# erl
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V7.3  (abort with ^G)
1> 

  当出现以上信息时表示安装完成。然后输入’halt().’退出即可。

 

  在安装Erlang过程中,可能会遇到以下问题,一般都是因为系统中缺少相应的包引起的,缺少什么包直接yum安装即可。

  3.下载安装RabbitMq

[[email protected] rabbitmq]# weget http://www.rabbitmq.com/releases/rabbitmq-server/v3.5.7/rabbitmq-server-generic-unix-3.5.7.tar.xz

  解压文件

[[email protected] rabbitmq]# xz -d rabbitmq-server-generic-unix-3.5.7.tar.xz
[[email protected] rabbitmq]# tar -xvf rabbitmq-server-generic-unix-3.5.7.tar  -C /opt

  解压后进入文件夹/opt发现多了个文件夹rabbitmq-server-generic-unix-3.5.7 ,重命名为rabbitmq以便记忆。

  然后在配置rabbitmq环境变量,vi /etc/profile文件,增加下面的环境变量:

#set rabbitmq environment
export PATH=$PATH:/opt/rabbitmq/sbin

  source  /etc/profile使得文件生效

  4.RabbitMQ服务启动关闭

  以上就已经完成了RabbitMq的安装,怎么启动服务呢?

  启动服务:

[[email protected] rabbitmq]# cd sbin/
[[email protected] sbin]# ./rabbitmq-server -detached

 查看服务状态:

[[email protected] sbin]# ./rabbitmqctl status
Status of node [email protected] ...
[{pid,11849},
 {running_applications,
     [{rabbitmq_management,"RabbitMQ Management Console","3.6.1"},
      {rabbitmq_management_agent,"RabbitMQ Management Agent","3.6.1"},
      {rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.6.1"},
      {webmachine,"webmachine","1.10.3"},
      {amqp_client,"RabbitMQ AMQP Client","3.6.1"},
      {mochiweb,"MochiMedia Web Server","2.13.0"},
      {syntax_tools,"Syntax tools","1.7"},
      {ssl,"Erlang/OTP SSL application","7.3"},
      {public_key,"Public key infrastructure","1.1.1"},
      {asn1,"The Erlang ASN1 compiler version 4.0.2","4.0.2"},
      {crypto,"CRYPTO","3.6.3"},
      {compiler,"ERTS  CXC 138 10","6.0.3"},
      {inets,"INETS  CXC 138 49","6.2"},
      {rabbit,"RabbitMQ","3.6.1"},
      {mnesia,"MNESIA  CXC 138 12","4.13.3"},
      {rabbit_common,[],"3.6.1"},
      {xmerl,"XML parser","1.3.10"},
      {os_mon,"CPO  CXC 138 46","2.4"},
      {ranch,"Socket acceptor pool for TCP protocols.","1.2.1"},
      {sasl,"SASL  CXC 138 11","2.7"},
      {stdlib,"ERTS  CXC 138 10","2.8"},
      {kernel,"ERTS  CXC 138 10","4.2"}]},
 {os,{unix,linux}},
 {erlang_version,
     "Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:64] [hipe] [kernel-poll:true]\n"},
 {memory,
     [{total,64111264},
      {connection_readers,0},
      {connection_writers,0},
      {connection_channels,0},
      {connection_other,2808},
      {queue_procs,2808},
      {queue_slave_procs,0},
      {plugins,367288},
      {other_proc,19041296},
      {mnesia,61720},
      {mgmt_db,158696},
      {msg_index,47120},
      {other_ets,1372440},
      {binary,128216},
      {code,27368230},
      {atom,992409},
      {other_system,14568233}]},
 {alarms,[]},
 {listeners,[{clustering,25672,"::"},{amqp,5672,"::"}]},
 {vm_memory_high_watermark,0.4},
 {vm_memory_limit,6556241100},
 {disk_free_limit,50000000},
 {disk_free,37431123968},
 {file_descriptors,
     [{total_limit,65435},
      {total_used,2},
      {sockets_limit,58889},
      {sockets_used,0}]},
 {processes,[{limit,1048576},{used,204}]},
 {run_queue,0},
 {uptime,412681},
 {kernel,{net_ticktime,60}}]

  关闭服务:

[[email protected] sbin]# ./rabbitmqctl stop
Stopping and halting node [email protected] ...

  4. 配置网页插件

  首先创建目录,否则可能报错:

mkdir /etc/rabbitmq 

  然后启用插件:

./rabbitmq-plugins enable rabbitmq_management 

  配置linux 端口 15672 网页管理  5672 AMQP端口

      在/opt/rabbitmq/etc/rabbitmq  下复制rabbitmq.config.example 文件为   rabbitmq.config  ,在rabbitmq.config 中可以配置        {tcp_listeners, [5672]},

  然后访问http://localhost:15672即可,或者虚拟机ip:15672也可 

  默认用户guest 密码guest

  

  5. 远程访问配置

  默认网页是不允许访问的,需要增加一个用户修改一下权限,代码如下:

  添加用户:rabbitmqctl add_user hxb hxb

  添加权限:rabbitmqctl set_permissions -p "/" hxb ".*" ".*" ".*"

      修改用户角色rabbitmqctl set_user_tags hxb administrator

  然后就可以远程访问了,然后可直接配置用户权限等信息。

  

  6. rabbitmq常用命令


      ./rabbitmqctl status                           查看状态

     ./rabbitmq-server -detached  服务器启动

     ./rabbitmqctl stop                   服务关闭

      ./rabbitmq-server &               服务器启动和网页插件启动

    ./rabbitmq-plugins enable rabbitmq_management    启动网页插件

  add_user        <UserName> <Password>    添加登录用户,密码

  delete_user     <UserName>

  change_password <UserName> <NewPassword>

  list_users

  add_vhost    <VHostPath>

  delete_vhost <VHostPath>

  list_vhostsset_permissions   [-p <VHostPath>] <UserName> <Regexp> <Regexp> <Regexp>

  clear_permissions [-p <VHostPath>] <UserName>

  list_permissions  [-p <VHostPath>]

  list_user_permissions <UserName>

  list_queues    [-p <VHostPath>] [<QueueInfoItem> ...]

  list_exchanges [-p <VHostPath>] [<ExchangeInfoItem> ...]

  list_bindings  [-p <VHostPath>]

  list_connections [<ConnectionInfoItem> ...]

7配置集群

(1)Erlang Cookie 文件:$home/.erlang.cookie   如果看不到 请 

[[email protected] ~]# ll -a

将任意一台的cookie文件或者内容替换到集群其他机器。最后确保文件权限为400。 
保证3台机器上cookie相同

[[email protected] ~]# cat /var/lib/rabbitmq/.erlang.cookie 
YZCUPNLBNOMPTDCUALZC

关闭rabbitmq,将cookie复制到其他机器…

[[email protected]~]# rabbitmqctl stop
[[email protected]~]# echo YZCUPNLBNOMPTDCUALZC > /var/lib/rabbitmq/.erlang.cookie 

[[email protected]~]# rabbitmqctl stop
[[email protected]~]# echo YZCUPNLBNOMPTDCUALZC > /var/lib/rabbitmq/.erlang.cookie 


cookie同步完成之后启动 
以detached方式启动


2 配置各节点下的hosts文件,确保节点之间主机名称可以相互解析。

         192.168.1.10   node1

         192.168.1.11   node2

         192.168.1.12   node3

(3)自动配置rabbitmq集群:

    rabbitmq的默认配置脚本为   安装目录下的 例如我的安装目录为/opt/rabbitmq  ,所以我的脚本路径为/opt/rabbitmq/etc/rabbitmq/rabbitmq.conf,

         如果此文件不存在,则拷贝目录下的rabbitmq.config.example 文件,改名称。

    在其中添加

 

          [

           ...

           {rabbit, [

            ...

           {cluster_nodes, ['[email protected]', '[email protected]', '[email protected]']},

            ...

            ]},

            ...

            ].

     分别启动每个rabbitmq节点

            rabbitmq -detached

     rabbitmq就自动组成集群了,用页面实验是否好使,如果不好使

则手动添加下,

将node2节点加入node1 作为RAM类型节点

[[email protected] ~]#rabbitmqctl stop_app
[[email protected] ~]#rabbitmqctl reset
[[email protected] ~]#rabbitmqctl join_cluster [email protected] --ram
[[email protected] ~]#rabbitmqctl start_app

将node3节点加入node1 作为disc类型节点

[[email protected] ~]#rabbitmq stop_app 
[[email protected] ~]#rabbitmqctl reset 
[[email protected] ~]#rabbitmqctl join_cluster [email protected] 
[[email protected] ~]#rabbitmqctl start_app 
(rabbitmq集群节点有disc 和ram两种类型,一个集群中至少要有一个disc类型的节点,不指定默认加入为disc)

注意事项:

1)   每个节点的erlang cookie须相同,可以在启动服务器加–setcookie参数设置相同的参数,      也可以在home目录下设置相同的.erlang.cookie文件。

2)   节点间应该能相互解析,可以通过修改/etc/hosts文件实现