当前位置:   article > 正文

SpringBoot 集成 RabbitMq_springboot 集成rabbitmq

springboot 集成rabbitmq

1.简介

1.1 什么是RabbitMq

RabbitMq是一个由erlang开发的AMQP(Advanced Message Queue 高级消息队列协议)的开源实现,RabbitMq是一个消息代理:它接收和转发消息。

1.2 RabbitMq的优点

1)异步消息处理 

2)业务解耦

3)错峰流控

4)处理分布式事务

1.3 RabbitMq中的重要角色

1)生产者:消息的创建者,负责创建和推送数据到消息服务器

2)消费者:消息的接收方,用于处理数据和确认消息

3)代理:就是RabbitMq本身,不生产消息,只做消息代理

1.4 RabbitMq中的重要组件

1)ConnectionFactory:连接管理器,应用程序与Rabbit之间简历连接的管理器

2)channel:信道,消息推送使用的通道

3)Exchange:交换器,用于接收,分配消息

4)Queue:队列,用于存储生产者的消息

5)RoutingKey:路由键,用户把生产者的数据分配到交换器上

6)BindingKey:绑定键,用于把交换器的消息绑定到队列上

1.5 RabbitMq中的广播类型

1)direct:默认方式,最基础最简单的模式,发送方把消息发送给订阅方,如果有多个订阅者,默认采取轮询的方式进行消息发送

2)headers:与direct类似,只是性能很差,此类型几乎用不到

3)fanout:分发模式,把消费分发给所有订阅者

4)topic:匹配订阅模式,使用正则匹配到消息队列,能匹配到的都能接收

1.6 SpringAMQP 主要对象

 

1.7 RabbitMq的运行流程

2.安装启动RabbitMq

2.1 docker 拉起镜像

docker pull rabbitmq:3-management

2.2  docker 安装RabbitMq

userName,password 请自行设置,--name 是名称,可以自定义

  1. docker run \
  2. -e RABBITMQ_DEFAULT_USER=userName \
  3. -e RABBITMQ_DEFAULT_PASS=password \
  4. --name mq \
  5. --hostname mq1 \
  6. -p 15672:15672 \
  7. -p 5672:5672 \
  8. -d \
  9. rabbitmq:3-management

2.3 docker 启动RabbitMq 

docker start mq

2.4 访问RabbitMq

网址为你的虚拟机ip加上15672,密码和用户名是自己设置,登录进去页面如下

 3.集成RabbitMq

3.1 项目结构图

publisher是生产者模块,consumer是消费者模块

 3.2 导入RabbitMq 相关依赖,版本有由SpringBoo管理

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-amqp</artifactId>
  4. </dependency>

3.3 在publisher创建一个test类,生产消息

  1. package cn.zb.publisher;
  2. import com.rabbitmq.client.Channel;
  3. import com.rabbitmq.client.Connection;
  4. import com.rabbitmq.client.ConnectionFactory;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import java.io.IOException;
  10. import java.util.concurrent.TimeoutException;
  11. @RunWith(SpringRunner.class)
  12. @SpringBootTest
  13. public class PublisherTest {
  14. @Test
  15. public void test() throws IOException, TimeoutException {
  16. ConnectionFactory factory = new ConnectionFactory();
  17. factory.setHost("192.168.182.136");
  18. factory.setPort(5672);
  19. factory.setVirtualHost("/");
  20. factory.setUsername("as");
  21. factory.setPassword("123456");
  22. //建立连接
  23. Connection connection = factory.newConnection();
  24. //创建信道Channel
  25. Channel channel = connection.createChannel();
  26. //创建交换机
  27. channel.exchangeDeclare("simple:exchange","direct",false,false,null);
  28. //创建队列
  29. channel.queueDeclare("simple:queue",false,false,false,null);
  30. //队列绑定交换机,"as" 为routingkey
  31. channel.queueBind("simple:queue","simple:exchange","as");
  32. //发送消息,需要传routingkey
  33. String message="hello,rabbitmq!";
  34. channel.basicPublish("simple:exchange","as",null,message.getBytes());
  35. //关闭通道和连接
  36. channel.close();
  37. connection.close();
  38. }
  39. }

3.4 运行,查看效果

 

 点击队列,进入到队列详情,可以看到队列中的消息详情

 

3.5 consumer编写test类,用于接收

  1. package cn.zb.consumer;
  2. import com.rabbitmq.client.*;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.test.context.junit4.SpringRunner;
  7. import java.io.IOException;
  8. import java.util.concurrent.TimeoutException;
  9. @RunWith(SpringRunner.class)
  10. @SpringBootTest
  11. public class ConsumerTest {
  12. @Test
  13. public void test() throws IOException, TimeoutException {
  14. ConnectionFactory factory = new ConnectionFactory();
  15. factory.setHost("192.168.182.136");
  16. factory.setPort(5672);
  17. factory.setVirtualHost("/");
  18. factory.setUsername("as");
  19. factory.setPassword("123456");
  20. //建立连接
  21. Connection connection = factory.newConnection();
  22. //创建信道Channel
  23. Channel channel = connection.createChannel();
  24. //接收消息,true表示,接收到传递过滤的消息后应答服务器
  25. channel.basicConsume("simple:queue",true,new DefaultConsumer(channel){
  26. @Override
  27. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  28. String message = new String(body);
  29. System.out.println("接收到消息:"+message);
  30. }
  31. });
  32. }
  33. }

 3.6 运行,存放到队列中的数据被消费,输出

 

4. SpringBoot 集成RabbitMq

4.1通过application.yml 配置文件中配置RabbitMq

端口号,用户名,密码自行填写,两个模块都是差不多的配置,顶多改一下服务的端口

  1. server:
  2. port: 8082
  3. spring:
  4. rabbitmq:
  5. host:
  6. port: 5672
  7. virtual-host: /
  8. username:
  9. password:

4.2 编写RabbitMqConfig 配置类,两个模块都需要有

  1. package cn.zb.publisher.config;
  2. import org.springframework.amqp.core.*;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. public class RabbitMqConfig {
  8. public static final String EXCHANGE_DIRECT="exchange_direct";
  9. public static final String QUEUE_MESSAGE="queue:message";
  10. public static final String ROUTINGKEY = "as";
  11. //声明交换机
  12. @Bean(EXCHANGE_DIRECT)
  13. public Exchange EXCHANGE_TOPIC(){
  14. //durable(true) 持久化,mq重启之后交换机还在
  15. //这里指定了交换机的广播方式为DIRECT默认模式
  16. return ExchangeBuilder.topicExchange(EXCHANGE_DIRECT).durable(false).build();
  17. }
  18. //声明队列
  19. @Bean(QUEUE_MESSAGE)
  20. public Queue QUEUE_MESSAGE(){
  21. return new Queue(QUEUE_MESSAGE);
  22. }
  23. //队列绑定交换机
  24. @Bean
  25. public Binding BINDING_QUEUE_MESSAGE(@Qualifier(QUEUE_MESSAGE) Queue queue,@Qualifier(EXCHANGE_DIRECT) Exchange exchange){
  26. //durable(true) 持久化,mq重启之后交换机还在
  27. return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY).noargs();
  28. }
  29. }

4.3 Publisher 创建controller编写生产消息,将消息发送到交换机的代码

  1. package cn.zb.publisher.controller;
  2. import cn.zb.publisher.config.RabbitMqConfig;
  3. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class PublisherController {
  9. @Autowired
  10. private RabbitTemplate rabbitTemplate;
  11. @GetMapping("/send")
  12. public void sendMessage() throws InterruptedException {
  13. for (int i = 0; i < 10; i++) {
  14. //指定消息发送到那个交换机,routingkey与配置类定义的一致
  15. rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_DIRECT,"as","这是生产者生产的第"+i+"条消息");
  16. Thread.sleep(1000);
  17. }
  18. }
  19. }

4.4consumer 创建RabbitMqListen 编写监听交换机绑定的队列,接收消息

  1. package cn.zb.consumer.listen;
  2. import cn.zb.consumer.config.RabbitMqConfig;
  3. import org.springframework.amqp.core.Message;
  4. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  5. import org.springframework.stereotype.Component;
  6. @Component
  7. public class RabbitMqListen {
  8. @RabbitListener(queues = {RabbitMqConfig.QUEUE_MESSAGE})
  9. public void getMessage(String msg, Message message){
  10. System.out.println(msg);
  11. }
  12. }

4.5 启动项目,发起send请求生成消息

 

本博客只做记录,参考了其他文献

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/728273
推荐阅读
相关标签
  

闽ICP备14008679号