1.RabbitMq
1.1介绍
RabbitMQ是一个消息代理:它接受并转发消息。你可以把它当成一个邮局:当你想邮寄信件的时候,你会把信件放在投递箱中,并确信邮递员最终会将信件送到收件人的手里。在这个例子中,RabbitMQ就相当与投递箱、邮局和邮递员。

AMQP协议中的核心思想就是生产者和消费者隔离,生产者从不直接将消息发送给队列。生产者通常不知道是否一个消息会被发送到队列中,只是将消息发送到一个交换机。先由Exchange来接收,然后Exchange按照特定的策略转发到Queue进行存储。同理,消费者也是如此。Exchange 就类似于一个交换机,转发各个消息分发到相应的队列中。

1.2六种工作模式
官网介绍:https://www.rabbitmq.com/getstarted.html

这里简单介绍下六种工作模式的主要特点:

简单模式:一个生产者,一个消费者

work模式:一个生产者,多个消费者,每个消费者获取到的消息唯一。

订阅模式:一个生产者发送的消息会被多个消费者获取。

路由模式:发送消息到交换机并且要指定路由key ,消费者将队列绑定到交换机时需要指定路由key

topic模式:将路由键和某模式进行匹配,此时队列需要绑定在一个模式上,“#”匹配一个词或多个词,“*”只匹配一个词。

1.2.0
准备:

安装rabbitmq,并启动

引入pom文件:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>

<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
</dependencies>
编写连接工具类:

package com.sakura.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;

/**
* Created by apple on 2018/9/4.
*/
public class ConnectionUtil {
public static Connection getConnection() throws IOException {
//连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
//连接5672端口 注意15672为工具界面端口 25672为集群端口
factory.setPort(5672);
factory.setVirtualHost("/jxd");
factory.setUsername("jxd");
factory.setPassword("123");
//获取连接
Connection connection = factory.newConnection();

return connection;

}
}
这里说一下配置虚拟主机和用户名密码

为什么会有虚拟主机:

当我们在创建用户时,会指定用户能访问一个虚拟机,并且该用户只能访问该虚拟机下的队列和交换机,如果没有指定,默认的是”/”;一个rabbitmq服务器上可以运行多个vhost,以便于适用不同的业务需要,这样做既可以满足权限配置的要求,也可以避免不同业务之间队列、交换机的命名冲突问题,因为不同vhost之间是隔离的。

设置权限:

1.2.1简单模式
生产者:

package com.sakura.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

/**
* Created by apple on 2018/9/4.
*/
public class Sender {
private final static String QUEUE_NAME = "simple_queue";

public static void main(String[] args) throws IOException {
//创建连接
Connection connection = ConnectionUtil.getConnection();
//创建通道
Channel channel = connection.createChannel();
//声明队列
/**
* 队列名
* 是否持久化
* 是否排外 即只允许该channel访问该队列 一般等于true的话用于一个队列只能有一个消费者来消费的场景
* 是否自动删除 消费完删除
* 其他属性
*
*/
channel.queueDeclare(QUEUE_NAME, false, false, false, null);

//消息内容
/**
* 交换机
* 队列名
* 其他属性 路由
* 消息body
*/
String message = "错的不是我,是这个世界~";
channel.basicPublish("", QUEUE_NAME,null,message.getBytes());
System.out.println("[x]Sent '"+message + "'");

//最后关闭通关和连接
channel.close();
connection.close();

}
}

消费者:

package com.sakura.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

/**
* Created by apple on 2018/9/4.
*/
public class Receiver {
private final static String QUEUE_NAME = "simple_queue";

public static void main(String[] args) throws IOException, InterruptedException {
//获取连接
Connection connection = ConnectionUtil.getConnection();
//获取通道
Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false, false, false, null);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, consumer);

while(true){
//该方法会阻塞
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println("[x] Received '"+message+"'");

}
}
}

1.2.1work模式
生产者:

package com.sakura.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

/**
* Created by apple on 2018/9/4.
*/
public class Sender {
private final static String QUEUE_NAME = "queue_work";

public static void main(String[] args) throws IOException, InterruptedException {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false, false, false, null);
for(int i = 0; i < 100; i++){
String message = "冬马小三" + i;
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println("[x] Sent '"+message + "'");
Thread.sleep(i*10);
}

channel.close();
connection.close();
}
}

消费者:

package com.sakura.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

/**
* Created by apple on 2018/9/4.
*/
public class Receiver1 {
private final static String QUEUE_NAME = "queue_work";

public static void main(String[] args) throws IOException, InterruptedException {
Connection connection = ConnectionUtil.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);
}

}
}
package com.sakura.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

/**
* Created by apple on 2018/9/4.
*/
public class Receiver2 {
private final static String QUEUE_NAME = "queue_work";

public static void main(String[] args) throws IOException, InterruptedException {
Connection connection = ConnectionUtil.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);
}

}
}

channel.basicQos启
The following basic (Java) example will receive a maximum of 10 unacknowledged messages at once:

依据官网看。是指通道channel每次能够接收的消费者最大值  https://www.rabbitmq.com/consumer-prefetch.html

若将该行代码注释,则channel无限制,消息将很快发送完毕,只不过消息阻塞在队列中

1.2.3订阅模式
1.2.4路由模式
生产者:

package com.sakura.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

/**
* Created by apple on 2018/9/4.
*/
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 = ConnectionUtil.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();
}
}

消费者:

package com.sakura.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

/**
* Created by apple on 2018/9/4.
*/
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 = ConnectionUtil.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);
}

}
}

package com.sakura.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

/**
* Created by apple on 2018/9/4.
*/
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 = ConnectionUtil.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);
}

}
}
路由模式下,一个队列可以绑定多个路由

1.2.5topic模式
生产者:

package com.sakura.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

/**
* Created by apple on 2018/9/4.
*/
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 = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();

channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);

//消息内容
String message = "如果真爱有颜色";
channel.basicPublish(EXCHANGE_NAME,"key.1",null,message.getBytes());
System.out.println("[x] Sent '"+message+"'");

//关通道 关连接
channel.close();
connection.close();
}
}

消费者:

package com.sakura.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

/**
* Created by apple on 2018/9/4.
*/
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 = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false, false,false, null);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "key.*");

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);
}
}
}

package com.sakura.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

/**
* Created by apple on 2018/9/4.
*/
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 = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false, false,false, null);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "*.*");

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);
}
}
}

项目地址:https://gitee.com/ecoSakuraSou/spring-rabbitmq
---------------------
作者:爱真白真是太好了
来源:CSDN
原文:https://blog.csdn.net/qq_33040219/article/details/82383127
版权声明:本文为博主原创文章,转载请附上博文链接!

rabbitmq官方的六种工作模式的更多相关文章

  1. RabbitMQ从零到集群高可用(.NetCore5.0) - RabbitMQ简介和六种工作模式详解

    一.RabbitMQ简介 是一个开源的消息代理和队列服务器,用来通过普通协议在完全不同的应用之间共享数据,RabbitMQ是使用Erlang(高并发语言)语言来编写的,并且RabbitMQ是基于AMQ ...

  2. SpringBoot整合RabbitMQ实现六种工作模式

    RabbitMQ主要有六种种工作模式,本文整合SpringBoot分别介绍工作模式的实现. 前提概念 生产者 消息生产者或者发送者,使用P表示: 队列 消息从生产端发送到消费端,一定要通过队列转发,使 ...

  3. RabbitMQ的六种工作模式

    一.基于erlang语言: 是一种支持高并发的语言 RabbitMQ的六种工作模式: 1.1 simple简单模式 消息产生着§将消息放入队列 消息的消费者(consumer) 监听(while) 消 ...

  4. RabbitMQ的六种工作模式总结

    最近学习RabbitMQ的使用方式,记录下来,方便以后使用,也方便和大家共享,相互交流. RabbitMQ的六种工作模式: 1.Work queues2.Publish/subscribe3.Rout ...

  5. RabbitMQ六种工作模式有哪些?怎样用SpringBoot整合RabbitMQ

    目录 一.RabbitMQ入门程序 二.Work queues 工作模式 三.Publish / Subscribe 发布/订阅模式 四.Routing 路由模式 五.Topics 六.Header ...

  6. rabbitmq的五种工作模式

    abbitmq的五种工作模式      

  7. 消息队列rabbitmq的五种工作模式(go语言版本)

    前言:如果你对rabbitmq基本概念都不懂,可以移步此篇博文查阅消息队列RabbitMQ 一.单发单收 二.工作队列Work Queue 三.发布/订阅 Publish/Subscribe 四.路由 ...

  8. RabbitMQ的几种工作模式

    maven: <dependencies> <!-- RabbitMQ的客户端 --> <dependency> <groupId>com.rabbit ...

  9. RabbitMQ工作模式

    ------------恢复内容开始------------ RabbitMQ基本概念: Producer:生产者(消息的提供者) Consumer:消费者(消息的使用者) Message:消息(程序 ...

随机推荐

  1. 文件权限解释rwx

    linux文件权限 r 读w 写x (execute)执行 rwxrw-r-- 解释:rwx文件所有者对该文件有读写执行的权限:rw- 组内用户对该文件有读写的权限:r-- 其他非本组用户对该文件具有 ...

  2. css:Media Queries: How to target desktop, tablet and mobile?

    <!doctype html> <html> <head> <meta name="viewport" content="wid ...

  3. cf1132E. Knapsack(搜索)

    题意 题目链接 Sol 看了status里面最短的代码..感觉自己真是菜的一批..直接爆搜居然可以过?..但是现在还没终测所以可能会fst.. #include<bits/stdc++.h> ...

  4. SAP MM 销售订单库存与普通库存之间相互转换过账后对于EBEWH以及MBEWH表的更新

    SAP MM 销售订单库存与普通库存之间相互转换过账后对于EBEWH以及MBEWH表的更新 1,DEMO数据 物料号:1300009995 工厂:2160 销售订单号/item号:0010097627 ...

  5. 29.Odoo产品分析 (四) – 工具板块(2) – 搜索和仪表盘(1)

    查看Odoo产品分析系列--目录 "项目管理"是一个用于管理你的项目,且将它们与其他应用关联起来的非常灵活的模块,他允许您的公司管理项目阶段,分配团队,甚至跟踪与项目相关的时间和工 ...

  6. iOS开发GCD(3)-数据安全

    /* 多个线程可能访问同一块资源,造成数据错乱和数据安全问题 为代码添加同步锁(互斥锁) */ -(void)synchronized{ @synchronized(self){ //需要锁住的代码, ...

  7. 企业建立成功 DevOps 模式所需应对的5个挑战

    [编者按]本文作者为 Kevin Goldberg,主要介绍要想成功部署 DevOps 模式,企业所需应对的5大挑战与问题.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 要给 DevOps ...

  8. Linux中对swap分区的配置

    swap分区的安装与正常分区的安装大致相同,我这里就只说一下不同 大家可先看我上一篇的安装:https://www.cnblogs.com/feiquan/p/9219447.html 1.查看swa ...

  9. SQL Server等待事件—RESOURCE_SEMAPHORE_QUERY_COMPILE

    等待事件介绍 关于等待事件RESOURCE_SEMAPHORE_QUERY_COMPILE,官方的介绍如下: Occurs when the number of concurrent query co ...

  10. 如何实现javascript js 类命名空间的写法

    转载 猫猫小屋http://www.maomao365.com/?p=823 在C#中有namespace概念,java中有package的概念,有了这些概念之后,在系统的运行时,每一个方法就会拥有唯 ...