• 周六. 7 月 13th, 2024

    RabbitMQ 1.安装

    root

    12 月 21, 2021 #RabbitMQ

    RabbitMQ 消息队列,也可以称为消息队列或者消息中间件,它一般有两种传递模式:点对点(p2p, Point-to-Point)和发布订阅模式(Pub/Sub)。

    1. 点对点模式是基于队列的, 消息生产者发送消息到队列,消息消费者从队列中接收消息。
    2. 发布订阅模式定义 了如何向一个内容节点发布和订阅消息,这个内容节点称为主题(topic), 主题可以认为是消息传递的中介, 消息发布者将消息发布到某个主题,而消息订阅者则从主题中订阅消息。 主题使得消息的订阅者与消息的发布者互相保持独立, 不需要进行接触即可保证消息的传递,发布订阅模式在消息的一对多广播时采用。

    主流消息中间件: RabbitMQ, Kafka, RocketMQ

    消息中间件作用

    • 解耦:
    • 冗余(存储):消息中间件可以把数据持久化直接到它们已经被完全处理。
    • 扩展性:只要另外增加处理过程即可。
    • 削峰: 流量削峰
    • 可恢复性:当系统一部分组件失败时,不会影响整个系统。消息中间件降低了进程间的耦合度,所以即使唤一个处理消息的进程挂掉,加入消息中间件中的消息仍然可以在系统恢复后进行处理。
    • 缓存: 消息中间件通过一个缓存层来提高效率,写入速度可以大于处理
    • 异步通信

    1、安装系统centos7

    2、安装Erlang语言

    需要先安装erlang语言。 这里用yum的方式进行安装,最开始尝试了源码编译,发现太多的依赖,解决起来太麻烦

    curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash
    yum install -y erlang
    erl

    3. 安装RabbitMQ

    下载 RabbitMQ在github.com可以找到预编译好的rpm安装包, 这里我们的系统是centos,所以选择rpm包即可

    复制链接地址,在centos中执行

    wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.9.11/rabbitmq-server-3.9.11-1.el7.noarch.rpm

    安装

    [root@VM-0-3-centos soft]# rpm -ivh rabbitmq-server-3.9.11-1.el7.noarch.rpm

    安装成功后
    3. 启动RabbitMQ服务

    rabbitmq-server -detached

    命令后面添加一个”-detached”参数是为了能够让RabbitMQ服务以守护进程的方式在后台运行,这样就不会因为当前shell窗口关闭影响

    [root@VM-0-3-centos soft]# rabbitmqctl status
    Status of node rabbit@VM-0-3-centos ...
    Runtime
    
    OS PID: 8841
    OS: Linux
    Uptime (seconds): 8
    Is under maintenance?: false
    RabbitMQ version: 3.9.11
    Node name: rabbit@VM-0-3-centos
    Erlang configuration: Erlang/OTP 23 [erts-11.2.2.7] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [hipe]
    Erlang processes: 265 used, 1048576 limit
    Scheduler run queue: 1
    Cluster heartbeat timeout (net_ticktime): 60
    
    Plugins
    
    Enabled plugin file: /etc/rabbitmq/enabled_plugins
    Enabled plugins:
    
    
    Data directory
    
    Node data directory: /var/lib/rabbitmq/mnesia/rabbit@VM-0-3-centos
    Raft data directory: /var/lib/rabbitmq/mnesia/rabbit@VM-0-3-centos/quorum/rabbit@VM-0-3-centos
    
    Config files
    
    
    Log file(s)
    
     * /var/log/rabbitmq/rabbit@VM-0-3-centos.log
     * /var/log/rabbitmq/rabbit@VM-0-3-centos_upgrade.log
     * <stdout>
    
    Alarms
    
    (none)
    
    Memory
    
    Total memory used: 0.0709 gb
    Calculation strategy: rss
    Memory high watermark setting: 0.4 of available memory, computed to: 0.7709 gb
    
    code: 0.0242 gb (34.2 %)
    other_proc: 0.0187 gb (26.42 %)
    other_system: 0.0118 gb (16.61 %)
    allocated_unused: 0.0069 gb (9.67 %)
    reserved_unallocated: 0.0046 gb (6.54 %)
    other_ets: 0.0029 gb (4.09 %)
    atom: 0.0013 gb (1.87 %)
    binary: 0.0002 gb (0.28 %)
    mnesia: 0.0001 gb (0.13 %)
    metrics: 0.0001 gb (0.08 %)
    plugins: 0.0 gb (0.05 %)
    msg_index: 0.0 gb (0.04 %)
    quorum_ets: 0.0 gb (0.01 %)
    stream_queue_procs: 0.0 gb (0.0 %)
    stream_queue_replica_reader_procs: 0.0 gb (0.0 %)
    connection_channels: 0.0 gb (0.0 %)
    connection_other: 0.0 gb (0.0 %)
    connection_readers: 0.0 gb (0.0 %)
    connection_writers: 0.0 gb (0.0 %)
    mgmt_db: 0.0 gb (0.0 %)
    queue_procs: 0.0 gb (0.0 %)
    queue_slave_procs: 0.0 gb (0.0 %)
    quorum_queue_procs: 0.0 gb (0.0 %)
    stream_queue_coordinator_procs: 0.0 gb (0.0 %)
    
    File Descriptors
    
    Total: 2, limit: 65438
    Sockets: 0, limit: 58892
    
    Free Disk Space
    
    Low free disk space watermark: 0.05 gb
    Free disk space: 22.9509 gb
    
    Totals
    
    Connection count: 0
    Queue count: 0
    Virtual host count: 1
    
    Listeners
    
    Interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
    Interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0

    输出以上信息,表示启动成功

    通过rabbitmqctl cluster_status查看集群信息, 目前只有一个RabbitMQ服务节点,可以看作单节点的集群

    [root@VM-0-3-centos soft]# rabbitmqctl cluster_status
    Cluster status of node rabbit@VM-0-3-centos ...
    Basics
    
    Cluster name: rabbit@VM-0-3-centos
    
    Disk Nodes
    
    rabbit@VM-0-3-centos
    
    Running Nodes
    
    rabbit@VM-0-3-centos
    
    Versions
    
    rabbit@VM-0-3-centos: RabbitMQ 3.9.11 on Erlang 23.3.4.8
    
    Maintenance status
    
    Node: rabbit@VM-0-3-centos, status: not under maintenance
    
    Alarms
    
    (none)
    
    Network Partitions
    
    (none)
    
    Listeners
    
    Node: rabbit@VM-0-3-centos, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
    Node: rabbit@VM-0-3-centos, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
    
    Feature flags
    
    Flag: implicit_default_bindings, state: enabled
    Flag: maintenance_mode_status, state: enabled
    Flag: quorum_queue, state: enabled
    Flag: stream_queue, state: enabled
    Flag: user_limits, state: enabled
    Flag: virtual_host_metadata, state: enabled

    4. 开启web管理界面

    RabbitMQ默认安装启动以后,是没有开启web管理界面的, 通过rabbitmq-plugins list命令可以列出插件的启用和禁用状态

    [root@VM-0-3-centos soft]# rabbitmq-plugins list
    Listing plugins with pattern ".*" ...
     Configured: E = explicitly enabled; e = implicitly enabled
     | Status: * = running on rabbit@VM-0-3-centos
     |/
    [  ] rabbitmq_amqp1_0                  3.9.11
    [  ] rabbitmq_auth_backend_cache       3.9.11
    [  ] rabbitmq_auth_backend_http        3.9.11
    [  ] rabbitmq_auth_backend_ldap        3.9.11
    [  ] rabbitmq_auth_backend_oauth2      3.9.11
    [  ] rabbitmq_auth_mechanism_ssl       3.9.11
    [  ] rabbitmq_consistent_hash_exchange 3.9.11
    [  ] rabbitmq_event_exchange           3.9.11
    [  ] rabbitmq_federation               3.9.11
    [  ] rabbitmq_federation_management    3.9.11
    [  ] rabbitmq_jms_topic_exchange       3.9.11
    [  ] rabbitmq_management               3.9.11
    [  ] rabbitmq_management_agent         3.9.11
    [  ] rabbitmq_mqtt                     3.9.11
    [  ] rabbitmq_peer_discovery_aws       3.9.11
    [  ] rabbitmq_peer_discovery_common    3.9.11
    [  ] rabbitmq_peer_discovery_consul    3.9.11
    [  ] rabbitmq_peer_discovery_etcd      3.9.11
    [  ] rabbitmq_peer_discovery_k8s       3.9.11
    [  ] rabbitmq_prometheus               3.9.11
    [  ] rabbitmq_random_exchange          3.9.11
    [  ] rabbitmq_recent_history_exchange  3.9.11
    [  ] rabbitmq_sharding                 3.9.11
    [  ] rabbitmq_shovel                   3.9.11
    [  ] rabbitmq_shovel_management        3.9.11
    [  ] rabbitmq_stomp                    3.9.11
    [  ] rabbitmq_stream                   3.9.11
    [  ] rabbitmq_stream_management        3.9.11
    [  ] rabbitmq_top                      3.9.11
    [  ] rabbitmq_tracing                  3.9.11
    [  ] rabbitmq_trust_store              3.9.11
    [  ] rabbitmq_web_dispatch             3.9.11
    [  ] rabbitmq_web_mqtt                 3.9.11
    [  ] rabbitmq_web_mqtt_examples        3.9.11
    [  ] rabbitmq_web_stomp                3.9.11
    [  ] rabbitmq_web_stomp_examples       3.9.11

    这里使用rabbitmq-plugins enable rabbitmq_management开启web界面管理插件

    [root@VM-0-3-centos soft]# rabbitmq-plugins enable rabbitmq_management
    Enabling plugins on node rabbit@VM-0-3-centos:
    rabbitmq_management
    The following plugins have been configured:
      rabbitmq_management
      rabbitmq_management_agent
      rabbitmq_web_dispatch
    Applying plugin configuration to rabbit@VM-0-3-centos...
    The following plugins have been enabled:
      rabbitmq_management
      rabbitmq_management_agent
      rabbitmq_web_dispatch
    
    started 3 plugins.
    [root@VM-0-3-centos soft]# rabbitmq-plugins list
    Listing plugins with pattern ".*" ...
     Configured: E = explicitly enabled; e = implicitly enabled
     | Status: * = running on rabbit@VM-0-3-centos
     |/
    [  ] rabbitmq_amqp1_0                  3.9.11
    [  ] rabbitmq_auth_backend_cache       3.9.11
    [  ] rabbitmq_auth_backend_http        3.9.11
    [  ] rabbitmq_auth_backend_ldap        3.9.11
    [  ] rabbitmq_auth_backend_oauth2      3.9.11
    [  ] rabbitmq_auth_mechanism_ssl       3.9.11
    [  ] rabbitmq_consistent_hash_exchange 3.9.11
    [  ] rabbitmq_event_exchange           3.9.11
    [  ] rabbitmq_federation               3.9.11
    [  ] rabbitmq_federation_management    3.9.11
    [  ] rabbitmq_jms_topic_exchange       3.9.11
    [E*] rabbitmq_management               3.9.11
    [e*] rabbitmq_management_agent         3.9.11
    [  ] rabbitmq_mqtt                     3.9.11
    [  ] rabbitmq_peer_discovery_aws       3.9.11
    [  ] rabbitmq_peer_discovery_common    3.9.11
    [  ] rabbitmq_peer_discovery_consul    3.9.11
    [  ] rabbitmq_peer_discovery_etcd      3.9.11
    [  ] rabbitmq_peer_discovery_k8s       3.9.11
    [  ] rabbitmq_prometheus               3.9.11
    [  ] rabbitmq_random_exchange          3.9.11
    [  ] rabbitmq_recent_history_exchange  3.9.11
    [  ] rabbitmq_sharding                 3.9.11
    [  ] rabbitmq_shovel                   3.9.11
    [  ] rabbitmq_shovel_management        3.9.11
    [  ] rabbitmq_stomp                    3.9.11
    [  ] rabbitmq_stream                   3.9.11
    [  ] rabbitmq_stream_management        3.9.11
    [  ] rabbitmq_top                      3.9.11
    [  ] rabbitmq_tracing                  3.9.11
    [  ] rabbitmq_trust_store              3.9.11
    [e*] rabbitmq_web_dispatch             3.9.11
    [  ] rabbitmq_web_mqtt                 3.9.11
    [  ] rabbitmq_web_mqtt_examples        3.9.11
    [  ] rabbitmq_web_stomp                3.9.11
    [  ] rabbitmq_web_stomp_examples       3.9.11

    Web管理默认端口为15672

    默认用户名和密码为guest/guest, 登录后如上图 仅可以localhost登录,

    禁用rabbitmq-management插件的命令为:

     rabbitmq-plugins disable rabbitmq_management


    这时我们另外添加一个用户,并设置相应的访问权限

    添加新用户, 用户名为root, 密码为:XXXXXX

    [root@VM-0-3-centos soft]# rabbitmqctl add_user root '**********'
    Adding user "root" ...
    Done. Don't forget to grant the user permissions to some virtual hosts! See 'rabbitmqctl help set_permissions' to learn more.

    为root用户设置所有权限

    [root@VM-0-3-centos soft]# rabbitmqctl set_permissions -p / root ".*" ".*" ".*"
    Setting permissions for user "root" in vhost "/" ...

    设置root用户为管理员角色

    [root@VM-0-3-centos soft]# rabbitmqctl set_user_tags root administrator
    Setting tags for user "root" to [administrator] ...

    然后我们即可以web管理页面登录

    至此,我们的RabbitMQ已经安装完成

    root