rabbitmq+topic+java_RabbitMq七种工作模式,java实战案例分享,别再说你不会!

一、Maven依赖添加

com.rabbitmqamqp-client3.0.4

复制代码

二、七种工作模式的java实例

1、简单模式

最简单的一个消费者和一个生产者模式,生产者生成消息,消费者监听消息,若是消费者监听到它所需要的消息,就会消费该消息,这种消息是次性的,被消费了就没有了。

eccacad8e89dfadf5fa971e43a0412af.png

1.1.1、EasyRecv.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.ConnectionFactory;

import com.rabbitmq.client.QueueingConsumer;

public class EasyRecv{

//队列名称

private final static String QUEUE_NAME = "hello world";

public static void main(String[] argv) throws java.io.IOException,java.lang.InterruptedException {

//打开连接和创建频道,与发送端一样

ConnectionFactory factory = new ConnectionFactory();

//设置RabbitMQ所在主机ip或者主机名

factory.setHost("127.0.0.1");

Connection connection = factory.newConnection();

Channel channel = connection.createChannel();

//声明队列,主要为了防止消息接收者先运行此程序,队列还不存在时创建队列。

/** * 队列名 * 是否持久化 * 是否排外 即只允许该channel访问该队列 一般等于true的话用于一个队列只能有一个消费者来消费的场景 * 是否自动删除 消费完删除 * 其他属性 * */

channel.queueDeclare(QUEUE_NAME, false, false, false, null);

System.out.println("Waiting for messages. To exit press CTRL+C");

//创建队列消费者

QueueingConsumer consumer = new QueueingConsumer(channel);

//指定消费队列

/** * 队列名 * 其他属性 路由 * 消息body */

channel.basicConsume(QUEUE_NAME, true, consumer);

while (true)

{

//nextDelivery是一个阻塞方法(内部实现其实是阻塞队列的take方法)

QueueingConsumer.Delivery delivery = consumer.nextDelivery();

String message = new String(delivery.getBody());

System.out.println("Received '" + message + "'");

}

}

}

复制代码

1.1.2、EasySend.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class EasySend{

//队列名称

private final static String QUEUE_NAME = "hello world";

public static void main(String[] argv) throws java.io.IOException

{

/** * 创建连接连接到MabbitMQ */

ConnectionFactory factory = new ConnectionFactory();

//设置MabbitMQ所在主机ip或者主机名

factory.setHost("127.0.0.1");

while (true){

//创建一个连接

Connection connection = factory.newConnection();

//创建一个频道

Channel channel = connection.createChannel();

//指定一个队列

channel.queueDeclare(QUEUE_NAME, false, false, false, null);

//发送的消息

Scanner scanner = new Scanner(System.in);

String ms = scanner.nextLine();

//String message = "hello world!";

//往队列中发出一条消息

channel.basicPublish("", QUEUE_NAME, null, ms.getBytes());

System.out.println("Sent '" + ms + "'");

//关闭频道和连接

channel.close();

connection.close();

}

}

复制代码

以上两个已经可以进行通信了,下面同样是简单的实例,但是我们可以看到在代码层面上,连接的代码都是一样的,所以我们可以创建一个连接的工具类。

1.2.1、RabbitmqConnectionUtil .java

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;

public class RabbitmqConnectionUtil{

public static Connection getConnection() throws IOException {

//连接工厂

ConnectionFactory factory = new ConnectionFactory();

factory.setHost("localhost");

//连接5672端口 注意15672为工具界面端口 25672为集群端口

factory.setPort(5672);

//factory.setVirtualHost("/xxxxx");

// factory.setUsername("xxxxxx");

// factory.setPassword("123456");

//获取连接

Connection connection = factory.newConnection();

return connection;

}

}

复制代码

1.2.2、UtilSend.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

public class UtilSend{

private final static String QUEUE_NAME = "UtilConn";

public static void main(String[] args) throws IOException {

Connection connection = RabbitmqConnectionUtil.getConnection();

//创建通道

Channel channel = connection.createChannel();

//声明队列

channel.queueDeclare(QUEUE_NAME, false, false, false, null);

//消息内容

String message = "这里是lbw广场";

channel.basicPublish("", QUEUE_NAME,null,message.getBytes());

System.out.println("[x]Sent '"+message + "'");

//最后关闭通关和连接

channel.close();

connection.close();

}

}

复制代码

1.2.3、UtilRecv.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.QueueingConsumer;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

public class UtilRecv{

private final static String QUEUE_NAME = "UtilConn";

public static void main(String[] args) throws IOException, InterruptedException {

Connection connection = null;

connection = RabbitmqConnectionUtil.getConnection();

//创建通道

Channel channel = connection.createChannel();

//声明队列

channel.queueDeclare(QUEUE_NAME, false, false, false, null);

QueueingConsumer queueingConsumer = new QueueingConsumer(channel);

channel.basicConsume(QUEUE_NAME,true,queueingConsumer);

while(true){

//该方法会阻塞

QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();

String message = new String(delivery.getBody());

System.out.println("[x] Received '"+message+"'");

}

}

}

复制代码

2、工作队列

工作队列也就是简单模式的强化版,一个队列是可以多个生产者,也可以有多个消费者来竞争消费消息,但是我们仍需保证队列的幂等性,队列存在就不能再创建同名队列,关注公众号:麒麟改bug,还可以获取Java核心知识点思维导图以及Java核心学习笔记。

8397769fb77e4a84b45a34191978fc17.png

下面的每个进程都控制其主线程休眠,让我们可以更好的看到结果。

2.1.1、Sender1.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

public class Sender1{

private final static String QUEUE_NAME = "queue_work";

public static void main(String[] args) throws IOException, InterruptedException {

Connection connection = RabbitmqConnectionUtil.getConnection();

Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false, false, false, null);

for(int i = 0; i < 100; i++){

String message = "lbw" + i;

channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

System.out.println("[x] Sent '"+message + "'");

Thread.sleep(i*10);

}

channel.close();

connection.close();

}

}

复制代码

2.1.2、Sender2.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

public class Sender2{

private final static String QUEUE_NAME = "queue_work";

public static void main(String[] args) throws IOException, InterruptedException {

Connection connection = RabbitmqConnectionUtil.getConnection();

Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false, false, false, null);

for(int i = 0; i < 100; i++){

String message = "nb" + i;

channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

System.out.println("[x] Sent '"+message + "'");

Thread.sleep(i*10);

}

channel.close();

connection.close();

}

}

复制代码

2.1.3、Receiver1.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.QueueingConsumer;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

/** * Created by san */

public class Receiver1{

private final static String QUEUE_NAME = "queue_work";

public static void main(String[] args) throws IOException, InterruptedException {

Connection connection = RabbitmqConnectionUtil.getConnection();

Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false,false, false,null);

//同一时刻服务器只会发送一条消息给消费者

channel.basicQos(1);

QueueingConsumer consumer = new QueueingConsumer(channel);

//关于手工确认 待之后有时间研究下

channel.basicConsume(QUEUE_NAME, false, consumer);

while(true){

QueueingConsumer.Delivery delivery = consumer.nextDelivery();

String message = new String(delivery.getBody());

System.out.println("[x] Received1 '"+message+"'");

Thread.sleep(10);

//返回确认状态

channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

}

}

}

复制代码

2.1.4、Receiver2.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.QueueingConsumer;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

/** * Created by san */

public class Receiver2{

private final static String QUEUE_NAME = "queue_work";

public static void main(String[] args) throws IOException, InterruptedException {

Connection connection = RabbitmqConnectionUtil.getConnection();

Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false,false, false,null);

//同一时刻服务器只会发送一条消息给消费者

channel.basicQos(1);

QueueingConsumer consumer = new QueueingConsumer(channel);

channel.basicConsume(QUEUE_NAME, false, consumer);

while(true){

QueueingConsumer.Delivery delivery = consumer.nextDelivery();

String message = new String(delivery.getBody());

System.out.println("[x] Received2 '"+message+"'");

Thread.sleep(1000);

//返回确认状态

channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

}

}

}

复制代码

2.1.5、结果

上面的四个程序都运行起来,结果可以看到如下,依据结果分析,可知,同一个消息队列,是可以有多个生产者和消费者的。

c1850b864a40fa2769bb540e40fe6dd0.png

620cbb9218cbf13613c297649b080f70.png

bfac9437f69093ca28585fbd9d7cf2cc.png

b51596515a7f9ec1cf3df3675a3f2be1.png

3、发布/订阅(fanout)

43b1a0f4f7e5ce63a4a9602d11fe1bcb.png

3.1.1、Sender.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.ConnectionFactory;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

public class Sender{

private final static String EXCHANGE_NAME = "test_exchange_fanout";

public static void main(String[] args)

{

try

{

//获取连接

Connection connection = RabbitmqConnectionUtil.getConnection();

//从连接中获取一个通道

Channel channel = connection.createChannel();

//声明交换机(分发:发布/订阅模式)

channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

//发送消息

for (int i = 0; i < 5; i++)

{

String message = "卢本伟广场" + i;

System.out.println("[send]:" + message);

//发送消息

channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("utf-8"));

Thread.sleep(5 * i);

}

channel.close();

connection.close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

复制代码

3.1.2、Receiver1.java

import com.rabbitmq.client.*;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

public class Receiver1{

//交换机名称

private final static String EXCHANGE_NAME = "test_exchange_fanout";

//队列名称

private static final String QUEUE_NAME = "test_queue_email";

public static void main(String[] args)

{

try

{

//获取连接

Connection connection = RabbitmqConnectionUtil.getConnection();

//从连接中获取一个通道

final Channel channel = connection.createChannel();

//声明交换机(分发:发布/订阅模式)

channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

//声明队列

channel.queueDeclare(QUEUE_NAME, false, false, false, null);

//将队列绑定到交换机

channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");

//保证一次只分发一个

int prefetchCount = 1;

channel.basicQos(prefetchCount);

//定义消费者

DefaultConsumer consumer = new DefaultConsumer(channel)

{

//当消息到达时执行回调方法

@Override

public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,

byte[] body) throws IOException

{

String message = new String(body, "utf-8");

System.out.println("[email] Receive message:" + message);

try

{

//消费者休息2s处理业务

Thread.sleep(1000);

}

catch (InterruptedException e)

{

e.printStackTrace();

}

finally

{

System.out.println("[1] done");

//手动应答

channel.basicAck(envelope.getDeliveryTag(), false);

}

}

};

//设置手动应答

boolean autoAck = false;

//监听队列

channel.basicConsume(QUEUE_NAME, autoAck, consumer);

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

复制代码

3.1.3、Receiver2.java

import com.rabbitmq.client.*;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

public class Receiver2{

//交换机名称

private final static String EXCHANGE_NAME = "test_exchange_fanout";

//队列名称

private static final String QUEUE_NAME = "test_queue_phone";

public static void main(String[] args)

{

try

{

//获取连接

Connection connection = RabbitmqConnectionUtil.getConnection();

//从连接中获取一个通道

final Channel channel = connection.createChannel();

//声明交换机(分发:发布/订阅模式)

channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

//声明队列

channel.queueDeclare(QUEUE_NAME, false, false, false, null);

//将队列绑定到交换机

channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");

//保证一次只分发一个

int prefetchCount = 1;

channel.basicQos(prefetchCount);

//定义消费者

DefaultConsumer consumer = new DefaultConsumer(channel)

{

//当消息到达时执行回调方法

@Override

public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,

byte[] body) throws IOException

{

String message = new String(body, "utf-8");

System.out.println("[phone] Receive message:" + message);

try

{

//消费者休息1s处理业务

Thread.sleep(1000);

}

catch (InterruptedException e)

{

e.printStackTrace();

}

finally

{

System.out.println("[2] done");

//手动应答

channel.basicAck(envelope.getDeliveryTag(), false);

}

}

};

//设置手动应答

boolean autoAck = false;

//监听队列

channel.basicConsume(QUEUE_NAME, autoAck, consumer);

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

复制代码

3.1.4、结果

从程序运行结果和RabbitMq的后台看出,这样的消息属于广播型,两个不同名的队列的都能收到该消息,只需它们都将自己绑定到同一个交换机,而且,该消息是持久的,只要交换机还在,消费者啥时候上线都能消费它所绑定的交换机,而且只会一个消费者只会消费一

28224b4573c48f064af88996041b9e43.png

42b200ca7a6c4c6cb4501734447489b9.png

eb144b71cca97efb66bc7a5310b92398.png

8c65804f92e9937fac86ffafef9a8bf2.png

4、路由(direct)

在前面的示例中,我们已经在创建绑定。您可能会想起类似的代码:

channel.queueBind(queueName,EXCHANGE_NAME,“”);

复制代码

绑定是交换和队列之间的关系。可以简单地理解为:队列对来自此交换的消息感兴趣。

绑定可以采用额外的routingKey参数。为了避免与basic_publish参数混淆,我们将其称为 绑定键。这是我们可以创建带有键的绑定的方法:

channel.queueBind(queueName,EXCHANGE_NAME,“ black”);

复制代码

直接绑定(密钥直接绑定到单个队列)

e16d2ef4f5e1f8fcf420ad6561847982.png

多重绑定(相同的绑定密钥绑定多个队列)

6e88dffbbb2889328224ca62549ea746.png

不同密钥绑定不同的队列,可以发挥出不同日志级别发送到不同的队列的效果。

ff8bf4f8e67c84c21ea37a591055adb6.png

4.1.1、Sender

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

public class Sender{

private final static String EXCHANGE_NAME = "exchange_direct";

private final static String EXCHANGE_TYPE = "direct";

public static void main(String[] args) throws IOException {

Connection connection = RabbitmqConnectionUtil.getConnection();

Channel channel = connection.createChannel();

channel.exchangeDeclare(EXCHANGE_NAME,EXCHANGE_TYPE);

String message = "那一定是蓝色";

channel.basicPublish(EXCHANGE_NAME,"key2", null, message.getBytes());

System.out.println("[x] Sent '"+message+"'");

channel.close();

connection.close();

}

}

复制代码

4.1.2、Receiver1.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.QueueingConsumer;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

/** * Created by san */

public class Receiver1{

private final static String QUEUE_NAME = "queue_routing";

private final static String EXCHANGE_NAME = "exchange_direct";

public static void main(String[] args) throws IOException, InterruptedException {

// 获取到连接以及mq通道

Connection connection = RabbitmqConnectionUtil.getConnection();

Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false,false,false,null);

channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"key");

channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"key2");

channel.basicQos(1);

QueueingConsumer consumer = new QueueingConsumer(channel);

channel.basicConsume(QUEUE_NAME, false, consumer);

while(true){

QueueingConsumer.Delivery delivery = consumer.nextDelivery();

String message = new String(delivery.getBody());

System.out.println("[x] Received1 "+message);

Thread.sleep(10);

channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

}

}

}

复制代码

4.1.3、Receiver2.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.QueueingConsumer;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

/** * Created by san */

public class Receiver2{

private final static String QUEUE_NAME = "queue_routing2";

private final static String EXCHANGE_NAME = "exchange_direct";

public static void main(String[] args) throws IOException, InterruptedException {

// 获取到连接以及mq通道

Connection connection = RabbitmqConnectionUtil.getConnection();

Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false,false,false,null);

channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"key2");

channel.basicQos(1);

QueueingConsumer consumer = new QueueingConsumer(channel);

channel.basicConsume(QUEUE_NAME, false, consumer);

while(true){

QueueingConsumer.Delivery delivery = consumer.nextDelivery();

String message = new String(delivery.getBody());

System.out.println("[x] Received2 "+message);

Thread.sleep(10);

channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

}

}

}

复制代码

4.1.4、结果-总结

有一点要注意是:在direct下,必须是Exchange(交换机)已经存在,消费端的队列才能绑定到Exchange,否则会报错。也就说上面的程序第一次运行时,需先启Sender,才能成功启动Reciver。

7409768d9e2d19bbe3ad502fde772c1c.png

58a8bce9d2f844acfef9ebf89d808399.png

d48df4bb3a778de26bf9de525d7b5881.png

9f4ba70d09ed941d27a3b2297d062a42.png

1a7b6c37d68ebecd52cad2391a437ca6.png

5、话题(topic)

话题也是一个持久的消息,只要交换机还在,每个上线的消费者都可以消费一次自己感兴趣的topic。

*(星号)可以代替一个单词。

#(哈希)可以替代零个或多个单词。

关注公众号:麒麟改bug,还可以获取Java核心知识点思维导图以及Java核心学习笔记。

d636f4c6b7b1a672794950852b65ca63.png

5.1.1、Sender.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

public class Sender{

private final static String EXCHANGE_NAME = "exchange_topic";

private final static String EXCHANGE_TYPE = "topic";

public static void main(String[] args) throws IOException {

Connection connection = RabbitmqConnectionUtil.getConnection();

Channel channel = connection.createChannel();

channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);

//消息内容

String message = "这里是卢本伟广场";

//第二个参数是topic匹配值

channel.basicPublish(EXCHANGE_NAME,"lbw.nb",null,message.getBytes());

System.out.println("[x] Sent '"+message+"'");

//关通道 关连接

channel.close();

connection.close();

}

}

复制代码

5.1.2、Receiver1.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.QueueingConsumer;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

public class Receiver1{

private final static String QUEUE_NAME = "queue_topic";

private final static String EXCHANGE_NAME = "exchange_topic";

private final static String EXCHANGE_TYPE = "topic";

public static void main(String[] args) throws IOException, InterruptedException {

Connection connection = RabbitmqConnectionUtil.getConnection();

Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false, false,false, null);

//第二参数就是去匹配我兴趣的topic

channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "lbw.nb.*");

channel.basicQos(1);

QueueingConsumer consumer = new QueueingConsumer(channel);

channel.basicConsume(QUEUE_NAME, false, consumer);

while(true){

QueueingConsumer.Delivery delivery = consumer.nextDelivery();

String message = new String(delivery.getBody());

System.out.println("[x] Received1 '"+message + "'");

Thread.sleep(10);

channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

}

}

}

复制代码

5.1.3、Receiver2.java

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.QueueingConsumer;

import top.san.RabbitMq.util.RabbitmqConnectionUtil;

import java.io.IOException;

public class Receiver2{

private final static String QUEUE_NAME = "queue_topic2";

private final static String EXCHANGE_NAME = "exchange_topic";

private final static String EXCHANGE_TYPE = "topic";

public static void main(String[] args) throws IOException, InterruptedException {

Connection connection = RabbitmqConnectionUtil.getConnection();

Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false, false,false, null);

//第二参数就是去匹配我兴趣的topic

channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "lbw.#");

channel.basicQos(1);

QueueingConsumer consumer = new QueueingConsumer(channel);

channel.basicConsume(QUEUE_NAME, false, consumer);

while(true){

QueueingConsumer.Delivery delivery = consumer.nextDelivery();

String message = new String(delivery.getBody());

System.out.println("[x] Received2 '"+message + "'");

Thread.sleep(10);

channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

}

}

}

复制代码

5.1.4、结果-分析

话题的特色就是队列可以获取自己感兴趣的话题消息,可以通过通配符*或#来表示匹配所有的感兴趣的字符串。

066b07b497677b1c1a2a7c08f799b88c.png

4cef4e7151a736d4c3b6f1cc1b48438d.png

88ba1e43b742617f38288c0287c42837.png

e80c4231b371a94b291d27814abb48c8.png

dcf23d2ed9e23ea2b53429740ed363d6.png

6、RPC(远程过程调用)

给张图自己体会吧(官网没给示例代码,我也就不写了),就是通过两个交换机实现一个可回调的过程吧。

7701216975c2d16a02cb6a19349ffc93.png

三、RabbitMq的交换机

RabbitMq是有一个交换机的概念的, 消息(Message)由Client发送,RabbitMQ接收到消息之后通过交换机转发到对应的队列上面。Worker会从队列中获取未被读取的数据处理。这样就可以实现消息的发送者无需知道消息使用者的存在,反之亦然,关注公众号:麒麟改bug,还可以获取Java核心知识点思维导图以及Java核心学习笔记。

Direct exchange:直连(路由)交换机,转发消息到routigKey指定的队列

Fanout exchange:扇形交换机,转发消息到所有绑定队列(相当于广播)

Topic exchange:主题交换机,按规则转发消息(很灵活)

Headers exchange:首部交换机

722f55b0f907338080274db49e77276a.png

前面的简单类型我们都是忽略了交换机的参数的,如该方法:channel.basicPublish("", QUEUE_NAME, null, message.getBytes());就是这个方法的第一个参数,置空说明使用了默认的交换机。 有几种交换类型可用:direct,topic,headers 和fanout。

小编分享的文章到这里就结束了,喜欢小编的分享可以点赞分享关注哦,感谢你们的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值