作者:手机用户2502856895 | 来源:互联网 | 2023-05-19 10:36
Ubuntu下安装RabbitMQ1.安装#AddDebianWheezybackportsrepositorytoobtaininit-system-helpersgpg--ke
Ubuntu 下安装RabbitMQ
1.安装
# Add Debian Wheezy backports repository to obtain init-system-helpers
gpg --keyserver pgpkeys.mit.edu --recv-key 7638D0442B90D010
gpg -a --export 7638D0442B90D010 | sudo apt-key add -
echo 'deb http://ftp.debian.org/debian wheezy-backports main' | sudo tee
/etc/apt/sources.list.d/wheezy_backports.list
# Add Erlang Solutions repository to obtain esl-erlang
wget -O- https://packages.erlang-solutions.com/debian/erlang_solutions.asc | sudo apt-key add -
echo 'deb https://packages.erlang-solutions.com/debian wheezy contrib' | sudo tee /etc/apt/sources.list.d/esl.list
sudo apt-get update
sudo apt-get install init-system-helpers socat esl-erlang
# continue with RabbitMQ installation as explained above
wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -
echo 'deb http://www.rabbitmq.com/debian/ testing main' | sudo tee /etc/apt/sources.list.d/rabbitmq.list
sudo apt-get update
sudo apt-get install rabbitmq-server
注:若有出现配置密钥的可通过下面命令添加
sudo -E apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 6B73A36E6026DFCA
可参照官网 http://www.rabbitmq.com/install-debian.html
2.启动
sudo service rabbitmq-server start
注:启动时有可能报错 “ERROR: node with name "rabbit" already running on "ubuntu"”,
解决方法:
ps aux | grep epmd
ps aux | grep erl
找出rabbit运行的进程,并 kill, 再次执行启动
启动命令:sudo service rabbitmq-server start
关闭命令: sudo service rabbitmq-server stop
重启命令:sudo service rabbitmq-server restart
查看状态: sudo service rabbitmq-server status
3.配置网页插件
sudo rabbitmq-plugins enable rabbitmq_management
配置完成之后可通过浏览器访问后台管理端: http://localhost:15672/ (默认端口是15672,默认用户guest 密码guest 默认用户仅能在本地访问)
4. 远程访问配置
默认网页是不允许访问的,需要增加一个用户修改一下权限,命令如下:
添加用户: sudo rabbitmqctl add_user root 123456
添加权限: sudo rabbitmqctl set_permissions -p "/" root ".*" ".*" ".*"
修改用户角色: sudo rabbitmqctl set_user_tags root administrator
然后就可以远程访问了,然后可直接配置用户权限等信息。
5. 简单实例
下载 rabbitmq-client.jar
或使用Maven依赖包
com.rabbitmq
amqp-client
4.0.2
消息发送端:
import java.util.concurrent.TimeoutException;
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 java.io.IOException, TimeoutException{
//创建连接工程
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("10.100.53.75");
factory.setPort(5672);
factory.setUsername("root");
factory.setPassword("123456");
//创建连接
Connection cOnnection= factory.newConnection();
//创建消息通道
Channel channel = connection.createChannel();
//生成一个消息队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World RabbitMQ";
//发布消息,第一个参数表示路由(Exchange名称),未""则表示使用默认消息路由
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '"+message+"'");
//关闭消息通道和连接
channel.close();
connection.close();
}
}
消息接收端:
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
public class Recv {
//消息队列名称
private final static String QUEUE_NAME="hello";
public static void main(String[] args) throws java.io.IOException,java.lang.InterruptedException, TimeoutException{
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("10.100.53.75");
factory.setPort(5672);
factory.setUsername("root");
factory.setPassword("123456");
//创建连接
Connection cOnnection= factory.newConnection();
//创建消息信道
Channel channel = connection.createChannel();
//消息队列
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
System.out.println("[*] Waiting for message. To exist 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+"'");
}
}
}