Centos7.5
Erlang(5.10.4)
RabbitMQ(3.6.8)
注意Erlang与RabbitMQ版本需要匹配
#安装Erlang
yum install erlang
如果报以下错误,则是yum源没有配置,配置即可
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/2): extras/7/x86_64/primary_db | 187 kB 00:00:00
(2/2): updates/7/x86_64/primary_db | 5.2 MB 00:00:05
Loading mirror speeds from cached hostfile
● base:
● extras:
● updates:
No package erlang available.
Error: Nothing to do
配置yum源
在/etc/yum.repos.d/下创建文件epel.repo
并添加内容
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
检查是否安装成功,成功显示版本,然后Ctrl+z退出
[root@VM_0_17_centos etc]# erl
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.4 (abort with ^G)
1>
在https://github.com/rabbitmq/rabbitmq-server/releases/tag/rabbitmq_v3_6_8下载rabbitmq-server-3.6.8-1.el7.noarch.rpm文件。(官网https://www.rabbitmq.com/news.html)
并上传到linux上
# 以rpm方式安装
rpm -ivh rabbitmq-server-3.6.8-1.el7.noarch.rpm
如果出现以下错误
如果出现以下错误,是因为找不到socat包。
warning: rabbitmq-server-3.6.8-1.el7.noarch.rpm: Header V4 RSA/SHA512 Signature, key ID 6026dfca: NOKEY
error: Failed dependencies:
socat is needed by rabbitmq-server-3.6.8-1.el7.noarch
是因为没有安装socat导致,安装socat之后重新安装RabbitMQ即可
yum -y install socat
#启动
service rabbitmq-server start
#停止
service rabbitmq-server stop
#查看状态
service rabbitmq-server status
rabbitmqctl status
# 添加用户
[root@VM_0_17_centos etc]# rabbitmqctl add_user test 123456
Creating user "test" ...
# 给用户设置标签
rabbitmqctl set_user_tags test administrator
# 查看用户列表
rabbitmqctl list_users
# 用户授权
rabbitmqctl set_permissions -p "/" test".*" ".*" ".*"
#开启web管理端
rabbitmq-plugins enable rabbitmq_management
#l浏览器访问,ip:prot(端口号是15672)
192.234.99.xxx:15672
#创建访问用户
[root@VM_0_17_centos etc]# rabbitmqctl add_user test 123456
Creating user "test" ...
#给绑定用户绑定标签,不绑定没权限登录
[root@VM_0_17_centos etc]# rabbitmqctl set_user_tags test administrator
Setting tags for user "test" to [administrator] ...
创建maven项目,添加依赖
<dependency>
<groupId>com.rabbitmqgroupId>
<artifactId>amqp-clientartifactId>
<version>4.8.0version>
dependency>
生产者
package ai.zixing;
import com.rabbitmq.client.*;
public class RabbitmqProducter {
public static void main(String[] args) throws Exception {
// 连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("49.xxx.xxx.xxx");
factory.setPort(5672);
factory.setUsername("test");
factory.setPassword("123456");
// 创建连接
Connection connection = factory.newConnection();
// 创建信道
Channel channel = connection.createChannel();
// 创建交换器
channel.exchangeDeclare("demo.exchange", BuiltinExchangeType.DIRECT);
// 创建队列
channel.queueDeclare("demo.queue", false, false, false, null);
// 绑定交换器与队列关系
channel.queueBind("demo.queue", "demo.exchange", "ai.zixing");
// 所发送的消息
String message = "消息生产者111";
// 发送消息
channel.basicPublish("demo.exchange", "ai.zixing", MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes());
channel.close();
connection.close();
}
}
消费者
package ai.zixing;
import com.rabbitmq.client.*;
import java.io.IOException;
public class RabbitmqConsumer {
public static void main(String[] args) throws Exception{
// 连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("test");
factory.setPassword("123456");
Address[] addresses = {new Address("49.xx.xxx.xxx", 5672)};
Connection connection = factory.newConnection(addresses);
final Channel channel = connection.createChannel();
channel.basicConsume("demo.queue", new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("Cunsum :" + new String(body));
channel.basicAck(envelope.getDeliveryTag(), false); //告诉MQ 消费了消息 (消息确认机制)
}
});
Thread.sleep(1000);
channel.close();
connection.close();
}
}
报错一
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:120)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:142)
at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:407)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:63)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:177)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1150)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1109)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1067)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1224)
at ai.zixing.RabbitmqProducter.main(RabbitmqProducter.java:18)
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; protocol method: #method(reply-code=530, reply-text=NOT_ALLOWED - access to vhost '/' refused for user 'test', class-id=10, method-id=40)
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:443)
at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:263)
看日志主要是
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; protocol method: #method
这是因为用户权限不够,给test添加权限
[root@VM_0_17_centos etc]# rabbitmqctl set_permissions -p "/" test ".*" ".*" ".*"
Setting permissions for user "test" in vhost "/" ...
报错二
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
是因为端口号写错(6672),在客户端连接的时候端口号是5672