环境:centos7
开工:
1、 初始化环境:yum install gcc glibc-devel make ncurses-devel openssl-devel xmlto
2、创建文件夹:cd usr/local/src/
3、wget http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64.rpm
4、wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.6.9/:rabbitmq-server-3.6.9-1.el7.noarch.rpm
5、rpm -ivh --nodeps erlang-19.0.4-1.el7.centos.x86_64.rpm
6、rpm -ivh --nodeps rabbitmq-server-3.6.9-1.el7.noarch.rpm
7、firewall-cmd --zone=public --add-port=15672/tcp --permanent
8、 firewall-cmd --zone=public --add-port=5672/tcp --permanent
9、firewall-cmd --reload
10、rabbitmq-plugins enable rabbitmq_management
11、rabbitmq-server -detached
安装已完成
12、rabbitmqctl add_user root root // 新增用户root/root.
13、rabbitmqctl set_user_tags root administrator // 付管理角色
14、rabbitmqctl set_permissions -p / root '.*' '.*' '.*' // 付权限.
使用:http://192.168.1.102:15672/进行访问(采用root/root登录)
单消息应用:
1、添加maven 依赖在项目中:
2、由于本人环境原因没有多机器模拟,只在一个项目中集成了消息生产和消费者
package com.example.demo;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
@SpringBootApplication
public class Demo11Application {
public final static String QUEUE_NAME = "Hello.rabbitMQ";
public static void main(String[] args) {
//SpringApplication.run(Demo11Application.class, args);
testSendMq();
testRMq();
}
/**
* 消息生产者
*/
public static void testSendMq() {
// 鏈接工程
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.1.102"); // 虚拟机地址
factory.setPort(5672);
factory.setUsername("root");
factory.setPassword("root");
try {
// 得到连接,创建通道
String message = "Hello RabbitMQ";
for(int i=1;i<101;i++) {
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//獲得一個消息隊列
channel.queueDeclare(QUEUE_NAME+i, false, false, false, null);
message +=i;
channel.basicPublish("", QUEUE_NAME+i, null, message.getBytes("UTF-8"));
channel.close();
connection.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 消息消费者
*/
public static void testRMq() {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.1.102"); // 虚拟机地址
factory.setPort(5672);
factory.setUsername("root");
factory.setPassword("root");
try {
for(int i=1;i<1000001;i++) {
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME+i, false, false, false, null);
Consumer consumer = new DefaultConsumer(channel) {
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println("Customer Received '" + message + "'");
}
};
channel.basicConsume(QUEUE_NAME+i, true, consumer);
channel.close();
connection.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
消息中心(注释掉消息接收者):
开启消息接受者:
IDE 控制台:
样例演示过程中的问题:
可能是环境原因总是不能执行成功这个命令
解决方案:找rpm 版本的安装即可。
2、注意版本匹配问题 erling 和rabbitMQ的版本匹配,以官方文档为准