AMQP-RabbitMQ/1/概念/一对一简单模型

释放双眼,带上耳机,听听看~!

# JMS,AMQP,MQTT的区别与联系

  • JMS

Java消息传递服务(Java Messaging Service )

  • AMQP

高级消息队列协议(Advanced Message Queueing Protocol

  • MQTT

消息队列遥测传输(Message Queueing Telemetry Transport )

简单理解:
*JMS是专门为Java设计的一套消息服务API,像ActiveMQ就是对它的实现
*AMQP为了解决不同平台之间的通信问题,定义了一种名为amqp的通信协议,从而实现平台和语言无关性。
*MQTT也是一种通信协议。相比于AMQP的复杂性,它简单的多。所以amqp用于处理相对较重的任务,如两个系统平台之间的消息传输。而mqtt因为非常轻量,所以大量应用于物联网

# RabbitMQ常用命令

  • 修改密码 rabbitmqctl change_password Username Newpassword
  • 显示所有用户 rabbitmqctl list_users
  • 启动 rabbitmq-server start
  • 关闭 rabbitmqctl stop
  • homebrew安装配置文件地址 /usr/local/etc/rabbitmq/rabbitmq-env.conf
  • 管理平台地址 http://localhost:15672/

AMQP-RabbitMQ/1/概念/一对一简单模型

1. 简单模型

The simplest thing that does something

  • 模型图示

  • 生产者


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1package com.futao.springmvcdemo.mq.rabbit.simple;
2
3import com.futao.springmvcdemo.mq.rabbit.RabbitMqConnectionTools;
4import com.futao.springmvcdemo.mq.rabbit.RabbitMqQueueEnum;
5import com.rabbitmq.client.Channel;
6import com.rabbitmq.client.Connection;
7import lombok.Cleanup;
8import lombok.SneakyThrows;
9import lombok.extern.slf4j.Slf4j;
10
11/**
12 * 简单发送者
13 *
14 * @author futao
15 * Created on 2019-04-22.
16 */
17@Slf4j
18public class Send {
19    @SneakyThrows
20    public static void main(String[] args) {
21        @Cleanup
22        Connection connection = RabbitMqConnectionTools.getConnection();
23        @Cleanup
24        Channel channel = connection.createChannel();
25        //定义一个队列
26        channel.queueDeclare(RabbitMqQueueEnum.SIMPLE.getQueueName(), false, false, false, null);
27        String msg = "Hello RabbitMq!";
28        channel.basicPublish("", RabbitMqQueueEnum.SIMPLE.getQueueName(), null, msg.getBytes());
29        log.info("Send msg:[{}] success", msg);
30    }
31}
32
33
  • 消费者


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1package com.futao.springmvcdemo.mq.rabbit.simple;
2
3import com.futao.springmvcdemo.mq.rabbit.RabbitMqConnectionTools;
4import com.futao.springmvcdemo.mq.rabbit.RabbitMqQueueEnum;
5import com.rabbitmq.client.Channel;
6import com.rabbitmq.client.DeliverCallback;
7import lombok.SneakyThrows;
8import lombok.extern.slf4j.Slf4j;
9
10/**
11 * 简单消费者
12 *
13 * @author futao
14 * Created on 2019-04-22.
15 */
16@Slf4j
17public class Recv {
18    @SneakyThrows
19    public static void main(String[] args) {
20        Channel channel = RabbitMqConnectionTools.getChannel();
21        channel.queueDeclare(RabbitMqQueueEnum.SIMPLE.getQueueName(), false, false, false, null);
22        log.info("Waiting for message...");
23        DeliverCallback deliverCallback = ((consumerTag, message) -> {
24            log.info("收到消息:[{}],tag:[{}]", new java.lang.String(message.getBody()), consumerTag);
25        });
26        channel.basicConsume(RabbitMqQueueEnum.SIMPLE.getQueueName(), true, deliverCallback, consumerTag -> {
27        });
28    }
29}
30
31
  • 特点:一对一。一个生产者,一个消费者。

# 通用代码

  • 工具类 – 链接工厂


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
1package com.futao.springmvcdemo.mq.rabbit;
2
3import com.rabbitmq.client.Channel;
4import com.rabbitmq.client.Connection;
5import com.rabbitmq.client.ConnectionFactory;
6import lombok.SneakyThrows;
7
8import java.io.IOException;
9import java.util.concurrent.TimeoutException;
10
11/**
12 * rabbitMq配置类
13 *
14 * @author futao
15 * Created on 2019-04-19.
16 */
17public class RabbitMqConnectionTools {
18    /**
19     * 获取链接
20     *
21     * @return
22     * @throws IOException
23     * @throws TimeoutException
24     */
25    public static Connection getConnection() {
26        try {
27            return getConnectionFactory().newConnection();
28        } catch (IOException | TimeoutException e) {
29            e.printStackTrace();
30            throw new RuntimeException(e.getMessage(), e);
31        }
32    }
33
34    /**
35     * 连接工厂
36     *
37     * @return
38     */
39    private static ConnectionFactory getConnectionFactory() {
40        ConnectionFactory factory = new ConnectionFactory();
41        factory.setHost("localhost");
42        factory.setPort(5672);
43        factory.setUsername("futao");
44        factory.setPassword("123456");
45        factory.setVirtualHost("/springmvc");
46        return factory;
47    }
48
49    /**
50     * 创建并获取通道
51     *
52     * @return
53     */
54    @SneakyThrows
55    public static Channel getChannel() {
56        Connection connection = RabbitMqConnectionTools.getConnection();
57        return connection.createChannel();
58    }
59}
60
61
  • 枚举类 – RabbitMqQueueEnum队列名称


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
1package com.futao.springmvcdemo.mq.rabbit;
2
3import lombok.AllArgsConstructor;
4import lombok.Getter;
5
6/**
7 * queue名称枚举
8 *
9 * @author futao
10 * Created on 2019-04-19.
11 */
12@Getter
13@AllArgsConstructor
14public enum RabbitMqQueueEnum {
15
16    /**
17     * 简单queue
18     */
19    SIMPLE("simple-queue"),
20    /**
21     * WorkQueue 工作队列
22     */
23    WORK_QUEUE("work-queue"),
24    /**
25     * 发布订阅-fanout
26     */
27    EXCHANGE_QUEUE_FANOUT_ONE("exchange-queue-fanout-1"),
28
29    EXCHANGE_QUEUE_FANOUT_TWO("exchange-queue-fanout-2"),
30
31    EXCHANGE_QUEUE_DIRECT_ONE("exchange-queue-direct-1"),
32    EXCHANGE_QUEUE_DIRECT_TWO("exchange-queue-direct-2"),
33
34    EXCHANGE_QUEUE_TOPIC_ONE("exchange-queue-topic-1"),
35    EXCHANGE_QUEUE_TOPIC_TWO("exchange-queue-topic-2"),
36    EXCHANGE_QUEUE_TOPIC_THREE("exchange-queue-topic-3"),
37    EXCHANGE_QUEUE_TOPIC_FOUR("exchange-queue-topic-4"),
38
39    /**
40     * RPC队列
41     */
42    RPC_QUEUE("rpc-queue");
43
44
45    /**
46     * queue名称
47     */
48    private String queueName;
49}
50
51

给TA打赏
共{{data.count}}人
人已打赏
安全网络

CDN安全市场到2022年价值76.3亿美元

2018-2-1 18:02:50

安全技术

SSM-SpringMVC-09:SpringMVC中以继承MutiActionController类的方式实现处理器

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索