1. 程式人生 > >Linux伺服器RabbitMq的安裝與使用

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就會看到如下介面

以上為自己安裝完之後總結的步驟,下面總結一下我安裝過程中遇見的坑:

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();
        }

 

好了,以上是我安裝和測試過程中遇見的所有坑,希望可以幫到大家,同時如果有寫錯的地方也希望大家可以幫忙指正,謝謝