Rabbitmq安装步骤

参考下面文章

https://cloud.tencent.com/developer/article/1354440

 

采坑

yum不可用则 找个可用的 /etc/yum.repod.d/* 复制,安装需要 epeg所以也需要拷贝 放在 key的目录下 RPM-GPG-KEY-EPEL-7 cp RPM-GPG-KEY-EPEL-7 /etc/pki/rpm-gpg

 阿里云的yum可以直接安装成功

先下载依赖的组件

 

缩减版命令

yum install build-essential openssl openssl-devel unixODBC unixODBC-devel make gcc gcc-c++ kernel-devel m4 ncurses-devel tk tc xz
yum install erlang
wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.8/rabbitmq-server-3.6.8-1.el7.noarch.rpm
rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc

/sbin/service rabbitmq-server start
rabbitmqctl add_user wisx wisx
rabbitmqctl set_permissions -p "/" wisx '.*' '.*' '.*'
rabbitmqctl set_user_tags wisx administrator
rabbitmq-plugins enable rabbitmq_management
rabbitmq-plugins enable rabbitmq_mqtt

 

 yum install build-essential openssl openssl-devel unixODBC unixODBC-devel make gcc gcc-c++ kernel-devel m4 ncurses-devel tk tc xz

658 2019-07-17 15:08:58 yum install erlang
659 2019-07-17 15:12:11 pwd
660 2019-07-17 15:12:16 history
661 2019-07-17 15:12:52 erl
662 2019-07-17 15:13:28 cd /root/soft/
663 2019-07-17 15:13:28 ls
664 2019-07-17 15:13:31 cd rabbitmq/
665 2019-07-17 15:13:32 ls
666 2019-07-17 15:13:41 wget wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.8/rabbitmq-server-3.6.8-1.el7.noarch.rpm
667 2019-07-17 15:15:24 rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
668 2019-07-17 15:15:29 ls
669 2019-07-17 15:15:43 yum install rabbitmq-server-3.6.8-1.el7.noarch.rpm
670 2019-07-17 15:16:06 rpm -i --nodeps rabbitmq-server-3.6.8-1.el7.noarch.rpm
671 2019-07-17 15:16:39 cd /
672 2019-07-17 15:16:40 ls
673 2019-07-17 15:16:42 cd sbin
674 2019-07-17 15:16:43 ls
675 2019-07-17 15:16:57 /sbin/service rabbitmq-server start
676 2019-07-17 15:17:23 rabbitmqctl add_user wisx wisx
677 2019-07-17 15:17:38 rabbitmqctl set_permissions -p "/" wisx '.*' '.*' '.*'
678 2019-07-17 15:18:05 rabbitmqctl set_user_tags wisx administrator
679 2019-07-17 15:18:17 rabbitmq-plugins enable rabbitmq_management
680 2019-07-17 15:19:33 rabbitmq-plugins enable rabbitmq_mqtt

 

 

启动插件不需要 重启服务

 

 

scp -P 22 -r root@148.70.4.44:/etc/yum.repos.d .
scp -P 22 -r root@148.70.4.44:/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 .

-------------------------

集群搭建方法文章:

https://www.cnblogs.com/lixioayi/articles/9993658.html

 

5 如果出现了问题,记得修改clientId 变成随机数,不然会失败

 

6 window 安装版本要正确,不然启动插件会报错

 

window 安装

http://erlang.org/download/otp_win64_21.0.1.exe

ERLANG_HOME


https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.7/rabbitmq-server-3.7.7.exe


或者3.7.8,笨的让人失去了希望

 

RABBITMQ_SERVER


rabbitmq-plugins enable rabbitmq_management

erlang 一定要和 rabbitmq对应

 

 

http://139.129.103.83:15672/
rabbitmqctl status 查看是否启动正常
重启 的方法
service rabbitmq-server restart

 

详情参看搭建集群文档
https://www.rabbitmq.com/clustering.html
注意 rabbitmqctl  和 rabbitmq-server 命令的不同使用
1 搭建集群 需要正常的都启动,连接可以任何一个连接就可以,同时处理失败的连接,是否需要关闭然后重连???
然后查看状态时发现都是独立的
# on rabbit1
rabbitmq-server -detached
# on rabbit2
rabbitmq-server -detached
# on rabbit3
rabbitmq-server -detached
2 停止2 然后加入1 中,再看状态已经加入了,还有reset
rabbitmqctl stop_app
# => Stopping node rabbit@rabbit2 ...done.

rabbitmqctl reset
# => Resetting node rabbit@rabbit2 ...

rabbitmqctl join_cluster rabbit@rabbit1
# => Clustering node rabbit@rabbit2 with [rabbit@rabbit1] ...done.

rabbitmqctl start_app

3 检测集群功能,停止1,2 查看状态,集群中只有 3,重启 detached,发现自动添加到了 集群

下面 命令就是我刚说的功能

# on rabbit1
rabbitmqctl stop
# => Stopping and halting node rabbit@rabbit1 ...done.

# on rabbit2
rabbitmqctl cluster_status
# => Cluster status of node rabbit@rabbit2 ...
# => [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
# =>  {running_nodes,[rabbit@rabbit3,rabbit@rabbit2]}]
# => ...done.

# on rabbit3
rabbitmqctl cluster_status
# => Cluster status of node rabbit@rabbit3 ...
# => [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
# =>  {running_nodes,[rabbit@rabbit2,rabbit@rabbit3]}]
# => ...done.

# on rabbit3
rabbitmqctl stop
# => Stopping and halting node rabbit@rabbit3 ...done.

# on rabbit2
rabbitmqctl cluster_status
# => Cluster status of node rabbit@rabbit2 ...
# => [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
# =>  {running_nodes,[rabbit@rabbit2]}]
# => ...done.

# on rabbit1
rabbitmq-server -detached
rabbitmqctl cluster_status
# => Cluster status of node rabbit@rabbit1 ...
# => [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
# =>  {running_nodes,[rabbit@rabbit2,rabbit@rabbit1]}]
# => ...done.

# on rabbit2
rabbitmqctl cluster_status
# => Cluster status of node rabbit@rabbit2 ...
# => [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
# =>  {running_nodes,[rabbit@rabbit1,rabbit@rabbit2]}]
# => ...done.

# on rabbit3
rabbitmq-server -detached

# on rabbit1
rabbitmqctl cluster_status
# => Cluster status of node rabbit@rabbit1 ...
# => [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
# =>  {running_nodes,[rabbit@rabbit2,rabbit@rabbit1,rabbit@rabbit3]}]
# => ...done.

# on rabbit2
rabbitmqctl cluster_status
# => Cluster status of node rabbit@rabbit2 ...
# => [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
# =>  {running_nodes,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]
# => ...done.

# on rabbit3
rabbitmqctl cluster_status
# => Cluster status of node rabbit@rabbit3 ...
# => [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
# =>  {running_nodes,[rabbit@rabbit2,rabbit@rabbit1,rabbit@rabbit3]}]
# => ...done.

4 考虑一个场景和解决方法
三个节点,b停了然后reset,想重连会被拒绝
A cluster of 3 nodes, A, B and C is formed
Node A is shut down
Node B is reset
Node A is started
Node A tries to rejoin B but B's cluster identity has changed
Node B doesn't recognise A as a known cluster member because it's been reset


7 集群中的 版本需要控制,有的太大混合会版本冲突
ram 就是内存,重启或崩溃会失败
All nodes in a cluster must run the same minor version of Erlang: 21.3.4 and 21.3.6 can be mixed but 21.0.1 and 21.3.6 (or 20.3 and 22.0.6) cannot. Compatibility between individual Erlang/OTP patch versions can vary between releases but that's generally rare.


8 客户端连接推荐不用ip,推荐用动态dns,超出了范围


9 启动一个节点作为 ram节点的方式:
需要先停在启动,通过加上 参数 --ram
然后使用状态查看
# on rabbit2
rabbitmqctl stop_app
# => Stopping node rabbit@rabbit2 ...done.

rabbitmqctl join_cluster --ram rabbit@rabbit1
# => Clustering node rabbit@rabbit2 with [rabbit@rabbit1] ...done.

rabbitmqctl start_app
# => Starting node rabbit@rabbit2 ...done.

# on rabbit1
rabbitmqctl cluster_status
# => Cluster status of node rabbit@rabbit1 ...
# => [{nodes,[{disc,[rabbit@rabbit1]},{ram,[rabbit@rabbit2]}]},
# =>  {running_nodes,[rabbit@rabbit2,rabbit@rabbit1]}]
# => ...done.

# on rabbit2
rabbitmqctl cluster_status
# => Cluster status of node rabbit@rabbit2 ...
# => [{nodes,[{disc,[rabbit@rabbit1]},{ram,[rabbit@rabbit2]}]},
# =>  {running_nodes,[rabbit@rabbit1,rabbit@rabbit2]}]
# => ...done.
10 rabbitmq 从磁盘节点和ram节点相互转换的方法

# on rabbit2
rabbitmqctl stop_app
# => Stopping node rabbit@rabbit2 ...done.

rabbitmqctl change_cluster_node_type disc
# => Turning rabbit@rabbit2 into a disc node ...done.

rabbitmqctl start_app
# => Starting node rabbit@rabbit2 ...done.

# on rabbit1
rabbitmqctl stop_app
# => Stopping node rabbit@rabbit1 ...done.

rabbitmqctl change_cluster_node_type ram
# => Turning rabbit@rabbit1 into a ram node ...done.

rabbitmqctl start_app
# => Starting node rabbit@rabbit1 ...done.



12 集群搭建另外 过程

1 rabbitmq cluster

92.168.128.140  A
             192.168.128.142  B
             192.168.128.143  C    


拷贝 cookie
scp /var/lib/rabbitmq/.erlang.cookie root@B:/var/lib/rabbitmq


启动服务
rabbitmqctl stop
rabbitmq-server -detached 

未建集群先看下
[root@A ~]# rabbitmqctl cluster_status
[root@B ~]# rabbitmqctl cluster_status
[root@C ~]# rabbitmqctl cluster_status

B加入集群A
[root@B ~]#rabbitmqctl stop_app 
[root@B ~]#rabbitmqctl join_cluster rabbit@A
[root@B ~]#rabbitmqctl start_app


c也同理

再次查看集群
 rabbitmqctl cluster_status
 
 关闭、查看防火墙 centos7变了
6 是
 service iptables status
 
 7变成了
 systemctl stop firewalld    // 临时关闭

systemctl disable firewalld   //禁止开机启动
 

6个类型只举一个例子最简单的

1 rabbitmq hello

producte queuename consumer
默认是5672,但是 rabbitmq插件的mqtt插件端口是5672
所以项目都没有用 rabbitmq

完整的发送,引入头,定义队名,发送数据,建立连接

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Send {

    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "Hello World!";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
            System.out.println(" [x] Sent '" + message + "'");
        }
    }
}
查看完整的接收命令

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

public class Recv {

    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        };
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
    }
}
此时需要依赖rabbitmq-client.jar 和相关依赖包

使用命令监控 linux和window的方法
linux:
sudo rabbitmqctl list_queues

window:
rabbitmqctl.bat list_queues

后面的连接有更复杂的三四个例子
https://www.rabbitmq.com/tutorials/tutorial-two-java.html

 

 

 

 

posted @ 2019-07-17 15:13  冰封剑客  阅读(277)  评论(0编辑  收藏  举报