消息:指在应用间传送的数据。
消息队列中间件:指利用高效可靠的消息传递机制进行与平台无关的数据交流,并基于数据通信来进行分布式系统的集成。通过提供消息传递和消息排队模型,可以在分布式环境下扩展进程间的通信。
消息队列中间件传递模式:
消息中间件系统:发送者将消息发送给消息服务器,消息服务器将消息存放在若干队列中,在合适时候将消息转发给接受者。
消息中间件作用:
起源:Eralng语言实现的AMQP(高级消息队列协议)的中间件。
特点:
先安装erlang并配置环境变量,再安装rabbitmq客户端。
双击exe程序
选择安装目录:E:\erl\erl9.2
配置环境变量:
可能出现的问题:
Error: unable to perform an operation on node ‘rabbit@doudou-dell’. Please see diagnostics informati。。。
解决方法
① 将以下两个目录下的.erlang.cookie 统一,(使用C:\Users\DELL的)
② 更换文件后,重启rabbitmq
重启方法,计算机 服务处右键重启
③重启后再执行 rabbitmqctl status 成功
登陆客户端:http://127.0.0.1:15672/ 用户名和密码:guest
最上侧的导航依次是:概览、连接、信道、交换器、队列、用户管理
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class RabbitProducer {
private static final String EXCHANGE_NAME = "exchange_demo";
private static final String ROUTING_KEY = "routingkey_demo";
private static final String QUEUE_NAME = "queue_demo";
private static final String IP_ADDRESS = "192.168.0.2";
private static final int PORT = 5672;
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(IP_ADDRESS);
factory.setPort(PORT);
factory.setUsername("guest");
factory.setPassword("guest");
Connection connection = factory.newConnection();//创建连接
Channel channel = connection.createChannel();//创建信道
//创建一个type ="direct" 持久化的,非自动删除的交换器
channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);
//创建一个持久化,非排他的,非自动删除的队列
channel.queueDeclare(QUEUE_NAME, true, false, false,null);
//将交换器与队列通过路由键绑定
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
String message = "Hello World";
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN,
message.getBytes());
channel.close();
connection.close();
}
}
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class RabbitConsumer {
private static final String QUEUE_NAME = "queue_demo";
private static final String IP_ADDRESS = "192.168.0.2";
private static final int PORT = 5672;
public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
Address[] addresses = new Address[]{new Address(IP_ADDRESS, PORT)};
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("guest");
factory.setPassword("guest");
Connection connection = factory.newConnection(addresses);//创建连接
final Channel channel = connection.createChannel();//创建信道
channel.basicQos(64);//设置客户端最多接收未被ack的消息的个数
Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties,
byte[] body) throws IOException{
System.out.println("recv message: " + new String(body));
try{
TimeUnit.SECONDS.sleep(1);
}catch (InterruptedException e){
e.printStackTrace();
}
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(QUEUE_NAME, consumer);
//等待回调函数执行完毕之后,关闭资源
TimeUnit.SECONDS.sleep(5);
channel.close();
connection.close();
}
}