准备工作
- 使用页面方式添加用户,虚拟主机,配置用户可以访问的虚拟主机
6种模型
HelloWorld模型–直连
public class Provider {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("hello",false, false,false,null);
channel.basicPublish("","hello",null,"hello".getBytes());
RbmqUtil.closeChannelAndConnection(channel,connection);
}
}
public class Customer {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("hello",false, false,false,null);
channel.basicConsume("hello",true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(LocalDateTime.now()+"--"+new String(body));
}
});
}
}
package com.wu.utils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class RbmqUtil {
private static ConnectionFactory connectionFactory;
static {
connectionFactory = new ConnectionFactory();
connectionFactory.setHost("ip");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/ems");
connectionFactory.setUsername("ems");
connectionFactory.setPassword("***");
}
public static Connection getConnection() {
Connection connection = null;
try {
connection = connectionFactory.newConnection();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
return connection;
}
public static void closeChannelAndConnection(Channel channel, Connection connection) {
try {
channel.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
Work queues–任务队列
public class Provider {
public static void main(String[] args) throws IOException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("work",true,false,false,null);
for (int i = 0; i <100; i++) {
channel.basicPublish("","work",null,(i+":发送了消息").getBytes());
}
RbmqUtil.closeChannelAndConnection(channel,connection);
}
}
public class Customer01 {
public static void main(String[] args) throws IOException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("work",true,false,false,null);
channel.basicConsume("work",true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(LocalDateTime.now()+"----消费者:01:"+new String(body));
}
});
}
}
public class Customer02 {
public static void main(String[] args) throws IOException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("work",true,false,false,null);
channel.basicConsume("work",true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(LocalDateTime.now()+"----消费者:02:"+new String(body));
}
});
}
}
public class Customer01 {
public static void main(String[] args) throws IOException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
channel.basicQos(1);
channel.queueDeclare("work",true,false,false,null);
channel.basicConsume("work",false,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(LocalDateTime.now()+"----消费者:01:"+new String(body));
channel.basicAck(envelope.getDeliveryTag(),false);
}
});
}
}
public class Customer02 {
public static void main(String[] args) throws IOException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
channel.basicQos(1);
channel.queueDeclare("work",true,false,false,null);
channel.basicConsume("work",false,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(LocalDateTime.now()+"----消费者:02:"+new String(body));
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
channel.basicAck(envelope.getDeliveryTag(),false);
}
});
}
}
Publish/Subscribe 发布订阅
public class Provider {
public static void main(String[] args) throws IOException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare("logs","fanout");
channel.basicPublish("logs","",null,"这是一条广播信息!!!".getBytes());
RbmqUtil.closeChannelAndConnection(channel,connection);
}
}
public class Customer01 {
public static void main(String[] args) throws IOException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare("logs","fanout");
String queue = channel.queueDeclare().getQueue();
channel.queueBind(queue,"logs","");
channel.basicConsume(queue,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消费者01---"+new String(body));
}
});
}
}
路由 Routing
public class Provider {
public static void main(String[] args) throws IOException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare("logs_direct","direct");
String routKey = "news";
channel.basicPublish("logs_direct",routKey,null,"这是一条日志信息".getBytes());
RbmqUtil.closeChannelAndConnection(channel,connection);
}
}
public class Customer01 {
public static void main(String[] args) throws IOException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
String exchange = "logs_direct";
String routKey = "log";
channel.exchangeDeclare(exchange,"direct");
String queue = channel.queueDeclare().getQueue();
channel.queueBind(queue,exchange,routKey);
channel.basicConsume(queue,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消费者--1--"+new String(body));
}
});
}
}
动态路由 Topics
public class Provider {
public static void main(String[] args) throws IOException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare("topics","topic");
channel.basicPublish("topics","user.save.2",null,"这是一条tops信息".getBytes());
RbmqUtil.closeChannelAndConnection(channel,connection);
}
}
public class Customer01 {
public static void main(String[] args) throws IOException {
Connection connection = RbmqUtil.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare("topics", "topic");
String queue = channel.queueDeclare().getQueue();
channel.queueBind(queue, "topics", "user.*");
channel.basicConsume(queue,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("-----" + new String(body));
}
});
}
}
Springboot整合
spring:
application:
name: rabbit_mq
rabbitmq:
host: ***.**
password: ...
port: 5672
username: ems
virtual-host: /ems
hello
@Test
void contextLoads() {
rabbitTemplate.convertAndSend("hello","hello world!!!!");
}
@Component
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloController {
@RabbitHandler
public void receivel(String body){
System.out.println("hello消费--"+body);
}
}
广播
@Test
void testFanout(){
rabbitTemplate.convertAndSend("logs","","测试广播!!!");
}
@Component
public class FanoutController {
@RabbitListener(bindings={
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "logs",type = "fanout")
)
})
public void hello(String msg){
System.out.println("hello----"+msg);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "logs",type = "fanout")
)
})
public void work(String msg){
System.out.println("work----"+msg);
}
}
路由
@Test
void testDirect(){
rabbitTemplate.convertAndSend("pros","log","路由!!");
}
@Component
public class RountController {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "pros",type = "direct"),
key = {"pro","log"}
)
})
public void msg01(String msg){
System.out.println("msg01------"+msg);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "pros",type = "direct"),
key = {"log"}
)
})
public void msg02(String msg){
System.out.println("msg02------"+msg);
}
}
动态路由
@Test
void testTopic(){
rabbitTemplate.convertAndSend("top","user.name.66","小米");
}
@Component
public class TopController {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "top",type = "topic"),
key = {"user.#"}
)
})
public void msg01(String msg){
System.out.println("msg01"+msg);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(name = "top",type = "topic"),
key = {"user.*"}
)
})
public void msg02(String msg){
System.out.println("msg02"+msg);
}
}
RabbitMQ