linux 安装
1. 安装依赖环境
yum install build-essential openssl openssl-devel unixODBC unixODBC-devel make gcc gcc-c++ kernel-devel m4 ncurses-devel tk tc xz
2. 安装Erlang
rpm -ivh erlang-18.3-1.el7.centos.x86_64.rpm
如果出现libc.so错误说明gblic 版本太低。我们可以查看当前机器的gblic 版本
strings /lib64/libc.so.6 | grep GLIBC
sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make -y
sudo rpm -Uvh *-2.17-55.el6.x86_64.rpm --force --nodeps
3. 安装RabbitMQ
rpm -ivh socat-1.7.3.2-5.el7.lux.x86_64.rpm
rpm -ivh rabbitmq-server-3.6.5-1.noarch.rpm
# 开启管理界面
rabbitmq-plugins enable rabbitmq_management
# 修改默认配置信息
vim /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.5/ebin/rabbit.app
# 比如修改密码、配置等等,例如:loopback_users 中的 <<"guest">>,只保留guest
4.启动服务
service rabbitmq-server start # 启动服务
service rabbitmq-server stop # 停止服务
service rabbitmq-server restart # 重启服务
5.移动配置文件
cd /usr/share/doc/rabbitmq-server-3.6.5/
cp rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
6.(先开端口或者关闭防火墙) 访问登录 ip:15672
user:guest
password:guest
安装完成
基本代码实现
依赖
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.6.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
生产者代码
public class Producer_HelloWord {
public static void main(String[] args) throws IOException, TimeoutException {
/*建立连接*/
ConnectionFactory connectionFactory = new ConnectionFactory();
/*参数*/
connectionFactory.setHost("");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("");
connectionFactory.setUsername("");
connectionFactory.setPassword("");
/*连接*/
Connection connection = connectionFactory.newConnection();
/*创建channel*/
/*String queue, 队列名称
boolean durable, 是否持久化,mq重启还存在
boolean exclusive, 是否独占 只能有一个消费者监听 当连接关闭时是否删除列队
boolean autoDelete, 是否自动删除 当没有消费者时自动删除
Map<String, Object> arguments 参数信息*/
Channel channel = connection.createChannel();
/*创建队列 如果没有这个名子的队列创建有则不创建*/
channel.queueDeclare("hello_world", true, false, false, null);
/*发送消息*/
/*String exchange, 交换机名称
String routingKey, 路由名称
boolean mandatory,
boolean immediate,
BasicProperties props, 配置信息
byte[] body / body.getBytes() 发送消息的数据*/
String body = "你好";
channel.basicPublish("", "hello_world", null, body.getBytes());
/*释放资源*/
channel.close();
connection.close();
}
}
消费者代码
public class CousmerWorld {
public static void main(String[] args) throws IOException, TimeoutException {
/*建立连接*/
ConnectionFactory connectionFactory = new ConnectionFactory();
/*参数*/
connectionFactory.setHost("");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("");
connectionFactory.setUsername("");
connectionFactory.setPassword("");
/*连接*/
Connection connection = connectionFactory.newConnection();
/*创建channel*/
/*String queue, 队列名称
boolean durable, 是否持久化,mq重启还存在
boolean exclusive, 是否独占 只能有一个消费者监听 当连接关闭时是否删除列队
boolean autoDelete, 是否自动删除 当没有消费者时自动删除
Map<String, Object> arguments 参数信息*/
Channel channel = connection.createChannel();
/*创建队列 如果没有这个名子的队列创建有则不创建*/
channel.queueDeclare("hello_world", true, false, false, null);
/*发送消息*/
/*String queue, 队列名称
DeliverCallback deliverCallback,回调对象
CancelCallback cancelCallback, 回调对象
ConsumerShutdownSignalCallback shutdownSignalCallback*/
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
/*回调方法当收到消息时执行此方法*/
/*String consumerTag, 标识
Envelope envelope,获取一些信息 交换机 路由KEY
AMQP.BasicProperties properties, 配置信息
byte[] body 收到的信息*/
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("consumerTag:"+consumerTag);
System.out.println("envelopeExchange:"+envelope.getExchange());
System.out.println("properties:"+properties);
System.out.println("body:"+new String(body));
}
};
channel.basicConsume("hello_world",true,defaultConsumer);
}
}