咱們在centos7上安裝RabbitMQ。java
1.安裝Erlangnode
1.1.下載安裝linux
//下載安裝包文件 wget http://www.erlang.org/download/otp_src_18.3.tar.gz //解壓縮 tar -zvxf otp_src_18.3.tar.gz //移動解壓縮後文件夾。/usr/local/src/otp_src_18.3 --> /usr/local/otp_src_18.3 mv -i otp_src_18.3 /usr/local/ //進入/usr/local/otp_src_18.3 cd /usr/local/otp_src_18.3/ //生產安裝配置 ./configure --prefix=/usr/local/erlang
此時報錯:configure: error: no acceptable C compiler found in $PATHc++
緣由是:缺乏gcc的編譯環境。spring
執行yum install -y gcc安裝gcc編譯環境。若是yum能夠先安裝yum,或者直接gcc的安裝包。安裝okjson
再次執行生產安裝配置的操做centos
./configure --prefix=/usr/local/erlang
再次報錯:安全
解決:yum install -y ncurses-devel。ok。服務器
再次執行生產安裝配置的操做:app
./configure --prefix=/usr/local/erlang
仍是報錯:
由於咱們用的gcc編譯,因此不須要java compiler,這個提示不用管。
---------------------------------不關注這個------------------------------------------------
須要關注odbc : ODBC library – link check failed 表示你未安裝unixODBC庫
下載unixODBC源碼包(http://www.unixodbc.org/unixODBC-2.2.1.tar.gz)放到某處好比/usr/local下,而後運行下述命令:
//在/usr/local下 mkdir unixODBC-2.2.1 cd unixODBC-2.2.1 ./configure --prefix=/usr/local/unixODBC-2.2.1 --includedir=/usr/include --libdir=/usr/lib -bindir=/usr/bin --sysconfdir=/etc --enable-gui=no
報錯:
須要安裝flex
yum -y install flex
成功後再次執行
./configure --prefix=/usr/local/unixODBC-2.2.1 --includedir=/usr/include --libdir=/usr/lib -bindir=/usr/bin --sysconfdir=/etc --enable-gui=no
成功,而後執行命令
make && make install
失敗,/bin/sh: yacc: command not found
解決:yum install -y byacc。ok。
再次執行make && make install,ok。
-------------------------------------------end---------------------------------------------------------------
而後安裝openSSL
//在/usr/local mkdir openssl yum list | grep ssl yum install openssl-devel
執行ok!
由於odbc : ODBC library - link check failed報錯,執行如下命令:
yum list | grep ODBC yum install unixODBC-devel
再次執行erlang的configure命令
./configure --prefix=/home/erlang --without-javac
執行如下命令:
yum install gcc-c++
ok!
再次執行./configure --prefix=/home/erlang --without-javac
崩潰!!!
哈哈,其實這個錯不用管了,咱們用的gcc編譯,直接安裝。
//編譯安裝 make && make install
測試erlang是否安裝成功。/home/erlang/bin/erl。
ok!!!erlang安裝ok了。
1.2.配置Erlang的環境變量
vi /etc/profile //在底部加入下面的內容 #set erlang environment ERL_HOME=/home/erlang PATH=$ERL_HOME/bin:$PATH export ERL_HOME PATH
讓配置生效:
source /etc/profile //生效
測試
至此,erlang安裝完成!!!
2.安裝RabbitMQ
2.1.下載安裝
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.1/rabbitmq-server-generic-unix-3.6.1.tar.xz xz -d rabbitmq-server-generic-unix-3.6.1.tar.xz tar -xvf rabbitmq-server-generic-unix-3.6.1.tar mv -i rabbitmq_server-3.6.1/ /usr/local/
配置rabbitmq環境變量:
vi /etc/profile //在底部加入如下內容 #set rabbitmq environment export PATH=$PATH:/usr/local/rabbitmq_server-3.6.1/sbin //讓配置生效,執行如下命令 source /etc/profile
啓動服務:
//啓動rabbitmq,-detached表明後臺守護進程方式啓動。 rabbitmq-server -detached
查看狀態:
rabbitmqctl status
若是能顯示rabbitmq的狀態信息,代表安裝成功!
Status of node rabbit@localhost ... [{pid,37234}, {running_applications,[{rabbit,"RabbitMQ","3.6.1"}, {mnesia,"MNESIA CXC 138 12","4.13.3"}, {rabbit_common,[],"3.6.1"}, {ranch,"Socket acceptor pool for TCP protocols.", "1.2.1"}, {os_mon,"CPO CXC 138 46","2.4"}, {xmerl,"XML parser","1.3.10"}, {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:2:2] [async-threads:64] [hipe] [kernel-poll:true]\n"}, {memory,[{total,42598496}, {connection_readers,0}, {connection_writers,0}, {connection_channels,0}, {connection_other,0}, {queue_procs,2808}, {queue_slave_procs,0}, {plugins,0}, {other_proc,18822224}, {mnesia,60888}, {mgmt_db,0}, {msg_index,39096}, {other_ets,900800}, {binary,27024}, {code,17392316}, {atom,662409}, {other_system,4690931}]}, {alarms,[]}, {listeners,[{clustering,25672,"::"},{amqp,5672,"::"}]}, {vm_memory_high_watermark,0.4}, {vm_memory_limit,764742860}, {disk_free_limit,50000000}, {disk_free,10780426240}, {file_descriptors,[{total_limit,924}, {total_used,2}, {sockets_limit,829}, {sockets_used,0}]}, {processes,[{limit,1048576},{used,138}]}, {run_queue,0}, {uptime,19}, {kernel,{net_ticktime,60}}]
3.配置管理界面插件
首先建立目錄,不然可能報錯:
mkdir /etc/rabbitmq
而後啓用插件:
mkdir /etc/rabbitmq rabbitmq-plugins enable rabbitmq_management
配置防火牆:
配置linux 端口 。網頁管理端口:15672 AMQP端口: 5672
firewall-cmd --permanent --add-port=15672/tcp firewall-cmd --permanent --add-port=5672/tcp systemctl restart firewalld.service
訪問RabbitMQ管理頁面:http://192.168.1.177:15672/
使用用戶名:guest,密碼:guest登錄,發現登錄不了,因爲帳號guest具備全部的操做權限,而且又是默認帳號,出於安全因素的考慮,guest用戶只能經過localhost登錄使用,並建議修改guest用戶的密碼以及新建其餘帳號管理使用rabbitmq(該功能是在3.3.0版本引入的)。
新建用戶並設置權限
rabbitmqctl add_user lyang lyang //添加用戶,後面兩個參數分別是用戶名和密碼,我這都用lyang了。 rabbitmqctl set_permissions -p / lyang ".*" ".*" ".*" //添加權限 rabbitmqctl set_user_tags lyang administrator //修改用戶角色
使用用戶名:lyang,密碼:lyang登錄,ok!
4.Java客戶端鏈接
這裏咱們用SpringBoot的方式鏈接。
pom.xml中加入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
application.yml中加入RabbitMQ的鏈接配置:
spring: rabbitmq: host: 192.168.1.177 port: 5672 username: guest password: guest
此時基本配置已完成,能夠開始碼代碼了:
生產者:RabbitProducer
import com.rabbitmq.client.*; import java.io.IOException; import java.util.concurrent.TimeUnit; public class RabbitProducer { private static final String EXCHANGE_NAME = "exchange_demo"; private static final String ROUTING_KEY = "routingkey-demo"; private static final String QUEUE_NAME = "queue_demo"; private static final String IP_ADDRESS = "192.168.1.177"; private static final int PORT = 5672; //rabbitmq默認端口號5672 private static final String USERNAME = "lyang"; private static final String PASSWORD = "lyang"; public static void main(String[] args) throws Exception { //建立rabbitmq的ConnectionFactory(鏈接工廠) ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost(IP_ADDRESS); connectionFactory.setPort(PORT); connectionFactory.setUsername(USERNAME); connectionFactory.setPassword(PASSWORD); //建立鏈接 Connection connection = connectionFactory.newConnection(); //建立通道 Channel channel = connection.createChannel(); //建立一個交換器 //type=direct、持久化的、非自動刪除的交換器 //第三個參數boolean durable設置是否持久化,持久化能夠將交換器存盤,服務器重啓時不會丟失相關信息 //第四個參數boolean autoDelete設置是否自動刪除, channel.exchangeDeclare(EXCHANGE_NAME,BuiltinExchangeType.DIRECT,true,false,null); //將交換器與隊列經過路由鍵綁定 channel.queueDeclare(QUEUE_NAME,true,false,false,null); //將交換器和隊列經過路由鍵綁定 channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,ROUTING_KEY); //發送一條持久化的消息 String msg = "hello world!"; //發佈消息 //第三個參數是boolean mandatory,表示當沒法根據路由鍵找到一個符合條件的隊列時, //rabbitmq會調用Basic.Return命令將消息返回給生產者 //第四個參數是boolean immediate,表示隊列沒有任何消費者時,這條消息會被返回給生產者 channel.basicPublish(EXCHANGE_NAME,ROUTING_KEY,true,false, MessageProperties.PERSISTENT_TEXT_PLAIN,msg.getBytes()); //添加ReturnListener來獲取沒有被正確路由到合適隊列的消息 channel.addReturnListener(new ReturnListener() { @Override public void handleReturn(int replyCode, String replyText, String exchange, String rountingKey,AMQP.BasicProperties basicProperties, byte[] bytes) throws IOException { String msg = new String(bytes); System.out.println("Basic.return返回的結果是:"+msg); } }); TimeUnit.SECONDS.sleep(200); channel.close(); connection.close(); } }
消費者:RabbitConsumer
import com.rabbitmq.client.*; import java.io.IOException; import java.util.concurrent.TimeUnit; public class RabbitConsumer { private static final String QUEUE_NAME = "queue_demo"; private static final String IP_ADDRESS = "192.168.1.177"; private static final int PORT = 5672; //rabbitmq默認端口號5672 private static final String USERNAME = "guest"; private static final String PASSWORD = "guest"; public static void main(String[] args) throws Exception{ //建立地址 Address[] addresses = new Address[]{new Address(IP_ADDRESS,PORT)}; //建立ConnectionFactory ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setUsername(USERNAME); connectionFactory.setPassword(PASSWORD); //建立鏈接 Connection connection = connectionFactory.newConnection(addresses); //建立通道 final Channel channel = connection.createChannel(); //設置客戶端最多接收未被ack的消息的個數 channel.basicQos(64); //建立消費端 Consumer consumer = new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException{ System.out.println("receive msg = "+new String(body)); try{ TimeUnit.SECONDS.sleep(1); }catch(InterruptedException e){ e.printStackTrace(); } //deliveryTag能夠看作成消息的編號,64位的長整型 //第二個參數requeue,表示是否將消息從新存入隊列,以即可以發送給下一個消費者 channel.basicAck(envelope.getDeliveryTag(),true); //顯示確認接收消息 //channel.basicReject(envelope.getDeliveryTag(),true); //顯示拒絕接受消息 } }; //第二個參數false表示取消自動確認消息接收機制、改成顯示手動確認,防止消息丟失 channel.basicConsume(QUEUE_NAME,false,consumer); //等待回調函數執行完畢以後,關閉資源 TimeUnit.SECONDS.sleep(200); channel.close(); connection.close(); } }
運行!
咱們看下RabbitMQ的管理頁面
上面的交換機和隊列就是咱們建立的。
至此,RabbitMQ的安裝、運行、java客戶端鏈接已所有ok!!!