RabbitMQ學習之Java客戶端連線測試(二)
阿新 • • 發佈:2019-01-09
前面花了幾天糾結完RabbitMQ在Linux下的安裝之後,開始找簡單的例子來測試RabbitMQ和Java的連線。
和前面的安裝一樣,問題依舊。因為網上的帖子大多數都是很正常的步驟,並且沒有貼出來自己途中可能遇到的低階錯誤。
本文將沿用網上很經典的一個帖子來說出我過程中出現的低階問題。
如果您和我一樣是一個Linux和RabbitMQ的新手,首先不要一開始就想的太好太完美,首先從最簡單的做起。
不要去找那些看上去很高大上的帖子,也不要一開始就和maven以及spring去整合。就寫簡單的main方法,來測試是否能夠很順利的從Java連線成功RabbitMQ。
下面的例子是網上的,但是不知道原貼到底是誰的,我也稍微加工一下,彌補新手遺漏的一些資訊。
1.首先需要去官網下載Java需要的jar包,建議先不要用maven依賴,慢慢來。
依次進入下面的路徑,這個是我現在最新的jar包路徑,下載解壓之後裡面就會有jar包,然後匯入到專案中即可。
2.建立Java專案,並寫連線類,下面兩個類為網上經典的類。
------------------------------------------------------------------------------------------------------
看似簡單的例子,如果RabbitMQ安裝的時候沒有弄好的話,依舊還是可能出現一些問題的,以下就是我的問題。 3.出現的問題。 >>>第一個
解決方案: >1.是不是覺得自己的使用者完全沒有問題,web端也能正常的連線上,使用者名稱,埠換了很多次,依舊還是不解到底是什麼問題呢? >2.此問題就是連線失敗,認證失敗,如果覺得自己所有的東西都已經弄好了,很有可能就是RabbitMQ的使用者許可權出現了問題。 這個是我之前的使用者狀態,很明顯這個地方使用者許可權是有問題的,因為新手和無知,所以也找不出為什麼。
第二個問題和第一個基本上差不多,意思就是無法訪問佇列資訊,所以很有可能還是使用者許可權的問題。如果按照上面的設定已經解決好了,事實上不會出現
2.建立Java專案,並寫連線類,下面兩個類為網上經典的類。
import java.io.IOException; 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[] args) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("10.10.6.246"); factory.setUsername("admin"); factory.setPassword("admin"); factory.setPort(5672); 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()); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); } }
------------------------------------------------------------------------------------------------------
import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.QueueingConsumer; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; public class Reqv { private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setUsername("admin"); factory.setPassword("admin"); factory.setHost("10.10.6.246"); factory.setVirtualHost("/"); factory.setPort(5672); 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"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); while(true){ QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); } } }
看似簡單的例子,如果RabbitMQ安裝的時候沒有弄好的話,依舊還是可能出現一些問題的,以下就是我的問題。 3.出現的問題。 >>>第一個
Exception in thread "main" com.rabbitmq.client.PossibleAuthenticationFailureException: Possibly caused by authentication failure at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:355) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:516) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:533) at com.mq.test.Reqv.main(Reqv.java:30) Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; reason: java.net.SocketException: Connection reset at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67) at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33) at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343) at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216) at com.rabbitmq.client.impl.AMQChannel.rpc(AMQChannel.java:202) at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:347) ... 3 more Caused by: java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at java.io.BufferedInputStream.fill(Unknown Source) at java.io.BufferedInputStream.read(Unknown Source) at java.io.DataInputStream.readUnsignedByte(Unknown Source) at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:95) at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:131) at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:515)
解決方案: >1.是不是覺得自己的使用者完全沒有問題,web端也能正常的連線上,使用者名稱,埠換了很多次,依舊還是不解到底是什麼問題呢? >2.此問題就是連線失敗,認證失敗,如果覺得自己所有的東西都已經弄好了,很有可能就是RabbitMQ的使用者許可權出現了問題。 這個是我之前的使用者狀態,很明顯這個地方使用者許可權是有問題的,因為新手和無知,所以也找不出為什麼。
# rabbitmqctl set_permissions -p VHostPath【虛擬主機路徑 ‘/’】 User【使用者名稱字】 ".*" ".*" ".*"
# /etc/init.d/rabbitmq-server restart
重新設定使用者的許可權之後重啟RabbitMQ,再次執行是否成功了呢?如果執行結果出現了:
[x]Sent 'hello world!'
----------------------------------------
[*] Waiting for messages. To exitpress CTRL+C
[x] Received 'hello world!'
那麼恭喜你終於解決了連線的問題了。 >>>第二個
Exception in thread "main" java.io.IOException
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106)
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124)
at com.rabbitmq.client.impl.ChannelN.queueDeclare(ChannelN.java:768)
at com.rabbitmq.client.impl.ChannelN.queueDeclare(ChannelN.java:61)
at com.mq.test.Reqv.main(Reqv.java:32)
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; reason: {#method<channel.close>(reply-code=403, reply-text=ACCESS_REFUSED - access to queue 'hello' in vhost '/' refused for user 'admin', class-id=50, method-id=10), null, ""}
at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343)
at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118)
... 3 more
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; reason: {#method<channel.close>(reply-code=403, reply-text=ACCESS_REFUSED - access to queue 'hello' in vhost '/' refused for user 'admin', class-id=50, method-id=10), null, ""}
at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:474)
at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:315)
at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144)
at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:91)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:533)
第二個問題和第一個基本上差不多,意思就是無法訪問佇列資訊,所以很有可能還是使用者許可權的問題。如果按照上面的設定已經解決好了,事實上不會出現
這個問題了,所以解決方式和第一個是一樣的。
4.給出一篇RabbitMQ許可權設定的帖子,以供參考。