Linux服务器RabbitMq的安装与使用

1.Erlang安装步骤:

下载连接:http://www.erlang.org/downloads本次选择版本为18.3

上传otp_src_18.3.tar.gz到/usr/local目录下并解压  tar -vxf otp_src_18.3.tar.gz

因为rabbitMq使用Erlang需要Erlang-ssl(也就是open-ssl)

下载链接http://erlang.org/documentation/doc-5.7.4/lib/ssl-3.10.7/doc/html/new_ssl.html

上传openssl-1.0.1f.tar.gz  到  /usr/local/openssl 并解压  tar -vxf openssl-1.0.1f.tar.gz

./config --prefix=/usr/local/ssl

 config之后,会生成Makefile,打开Makefile找到gcc,在CFLAG参数列表里加上-fPIC

    vim Makefile

CC= gcc  
CFLAG= -fPIC -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -Wa,--noexecstack -m64 -DL_ENDIAN -DTERMIO -O3 -Wall -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM 

编译并安装  make && make install

现在进入你的erlang源码安装目录,如果已经编译安装过erlang,为确保能重新编译,先执行:

    make clean

加上openssl安装路径重新configure   

./configure --with-ssl=/usr/local/ssl/ --prefix=/usr/local/erlang

如果出现  configure: error: No curses library functions found 提示 ,需要首先安装 ncurses-devel

下载链接http://ftp.gnu.org/gnu/ncurses/

上传ncurses-5.3.tar.gz到 /usr/local目录下 tar -vxf ncurses-5.3.tar.gz

./configure 之后 make install

安装完 ncurses-devel之后,编译并安装erlang  make && make install

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

export PATH=$PATH:/usr/local/erlang/bin

使得文件生效 source  /etc/profile

至此erlang安装完成

验证erlang是否安装成功:erl

表示安装成功

验证erlang-ssl是否安装成功:ssl:versions().

代表安装失败需要重新将OpenSSL和erlang都make clean以后重新安装

代表openssl安装成功

输入halt().指令退出

2.RabbitMq安装

下载链接:http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.2/rabbitmq-server-generic-unix-3.6.2.tar.xz

上传 rabbitmq-server-generic-unix-3.6.2.tar  到/usr/local/rabbitmq/ 并解压 tar -vxf rabbitmq-server-generic-unix-3.6.2.tar

添加环境变量:export PATH=$PATH:/usr/local/rabbitmq/sbin

环境变量生效:source  /etc/profile

进入sbin 启动服务:./rabbitmq-server

新增用户指令:rabbitmqctl add_user username password

分配角色:rabbitmqctl set_user_tags username administrator

新增虚拟主机:rabbitmqctl add_vhost  vhost_name

将新虚拟主机授权给新用户:rabbitmqctl set_permissions -p vhost_name username '.*' '.*' '.*'

角色说明

none  最小权限角色

management 管理员角色

policymaker   决策者

monitoring  监控

administrator  超级管理员 

 

打开浏览器输入ip:15762就会看到如下界面Linux服务器RabbitMq的安装与使用_第1张图片

以上为自己安装完之后总结的步骤,下面总结一下我安装过程中遇见的坑:

1.第一次安装的时候并没有去自己安装openssl和ncurses导致错误百出,所以这两个是必须的,特别是openssl,因为我们测试服务器已经安装过得,但是我指定之前安装的路径还是不行,后来自己重新安装才成功

2.安装完之后用guest用户 去登陆发现登录失败,因为guest用户只适用于localhost登录

3.新建用户admin之后登录成功,但是本地windows上编写的demo发布消息时候报错如下:这个主要原因是我们本地windows到测试服务器的5672端口没有访问权限

15:19:22.386 [AMQP Connection 10.142.142.154:5672] WARN com.rabbitmq.client.impl.ForgivingExceptionHandler - An unexpected connection driver error occured (Exception message: Connection reset)
java.io.IOException
	at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:126)
	at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:122)
	at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:373)
	at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:64)
	at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:156)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1095)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1054)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1012)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1170)
	at com.example.eurakeserver.rabbitMq.RabbitMqProducer.publish(RabbitMqProducer.java:34)
	at com.example.eurakeserver.rabbitMq.RabbitMqProducer.main(RabbitMqProducer.java:16)
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error
	at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66)
	at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
	at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:494)
	at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:315)
	... 8 more
Caused by: java.net.SocketException: Connection reset
	at java.net.SocketInputStream.read(SocketInputStream.java:210)
	at java.net.SocketInputStream.read(SocketInputStream.java:141)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
	at java.io.DataInputStream.readUnsignedByte(DataInputStream.java:288)
	at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:91)
	at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:164)
	at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:596)
	at java.lang.Thread.run(Thread.java:748)

测试demo:生产者

 //创建一个工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置ip
        factory.setHost("你的ip");
//        factory.setHost("localhost");
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setPort(5672);

        factory.setVirtualHost("/");

        //新建一个链接
        Connection connection = null;
        try {
            connection = factory.newConnection();
            //创建通道
            Channel channel = connection.createChannel();
            //申明一个队列
            channel.queueDeclare(QUEUE_NAME,false,false,false,null);
            //消息体
            for(int i = 0 ; i <= 10 ; i++){
                String message = "come from rabbitmq_test " + i;
                //发送消息到队列
//                Thread.sleep(3000);
                channel.basicPublish("",QUEUE_NAME,null,message.getBytes("utf-8"));
                System.out.println("Producer Send +'" + message + "'");
            }
            //关闭通道和连接
            channel.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

消费者:

 ConnectionFactory factory = new ConnectionFactory();
        //设置ip
        factory.setHost("10.142.142.154");
//        factory.setHost("localhost");
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setPort(5672);

        factory.setVirtualHost("/");
        Connection connection = null;
        try {
            connection = factory.newConnection();
            Channel channel = connection.createChannel();
            channel.basicQos(1);
            channel.queueDeclare(QUEUE_NAME,false,false,false,null);

            Consumer consumer = new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag,
                                           Envelope envelope,
                                           AMQP.BasicProperties properties,
                                           byte[] body)
                        throws IOException
                {
                    String message = new String(body,"utf-8");
                    System.out.println("Customer Received '" + message + "'");
                }
            };
            //自动回复队列应答 -- RabbitMQ中的消息确认机制
            channel.basicConsume(QUEUE_NAME, true, consumer);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

 

好了,以上是我安装和测试过程中遇见的所有坑,希望可以帮到大家,同时如果有写错的地方也希望大家可以帮忙指正,谢谢

你可能感兴趣的:(Java框架)