SpringBoot完美整合RabbitMq(三)一看就懂一写就会的示例代码

上面两篇文章已简单介绍了rabbitMq的安装和与其它消息中间件的区别。

详见:

1.SpringBoot完美整合RabbitMq(一)通过docker安装rabbitMq

2.SpringBoot完美整合RabbitMq(二)RabbitMq等消息中间件简介

现在简单介绍下rabbitMq在SpringBoot中的使用。

一、引入依赖

SpringBoot已经默认整合了rabbitMq自动配置,只需要引入简单的依赖即可

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

我的Springboot默认版本2.1.9.RELEASE

二、新增RabbitMqController测试类

  1. 注入RabbitTemplate类, RabbitTemplate是消息发送处理组件。
1
2
@Autowired
private RabbitTemplate rabbitTemplate;
  1. 新增单播模式的发送和接收方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 /**
* 发送rabbitMq消息--单播模式
*
* @Author ZHANGCHAO
* @Date 2020/3/4 17:05
* @param
* @retrun void
**/
@GetMapping("/send")
public void sendMqDirect() {
Map<String,Object> map = new HashMap<>();
map.put("msg","这是第一条消息");
map.put("data", Arrays.asList("ggg",123,999L,888d));
// rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",map);
GyUser user = new GyUser();
user.setUserName("卧草啊");
user.setPassWord("666666");
user.setAddress("山东省章丘市龙山街道办");
rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",user);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 接收rabbitMq消息--单播模式
*
* @Author ZHANGCHAO
* @Date 2020/3/4 17:05
* @param
* @retrun void
**/
@GetMapping("/receive")
public ResponseMessage receiveMqDirect() {
Object object = rabbitTemplate.receiveAndConvert("atguigu.news");
System.out.println(object.getClass());
return new ResponseMessage(true,object);
}
  1. 广播模式,和单播模式大同小异,不设置路由键
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 广播模式
*
* @Author ZHANGCHAO
* @Date 2020/3/5 8:18
* @param
* @retrun void
**/
@RequestMapping("/sendFanout")
public void sendMqFanout() {
GyUser user = new GyUser();
user.setUserName("李岩刘露啊啊啊");
user.setPassWord("666666");
user.setAddress("山东省章丘市");
rabbitTemplate.convertAndSend("exchange.fanout","",user);
}

二、rabbitMq的转换器序列化方式是用的jdk的,可读性差,换成Jackson。

新建MyRabbitMqConfig配置类,配置MessageConverter类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @program sweet-dream
* @description: rabbitMq配置类
* @author: zhangchao
* @date: 2020/03/04 20:36
* @since: 1.0.0
*/
@Configuration
public class MyRabbitMqConfig {

@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
}

三、application.properties配置rabbitMq的地址、用户名密码等信息

1
2
3
4
5
6
## rabbitmq配置 ##
spring.rabbitmq.host=115.188.188.188
spring.rabbitmq.username=root
spring.rabbitmq.password=999999
#端口号可以省略,默认就是5672
spring.rabbitmq.port=5672

四、开启基于注解的RabbitMQ模式

  1. 在启动类加上@EnableRabbit,开启基于注解的RabbitMQ模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* sweet-dream启动类
*
* @author ZHANGCHAO
*/
@Slf4j
@EnableRabbit
@EnableTransactionManagement
@SpringBootApplication
@MapperScan({"com.junya.dao","com.junya.mapper"})
public class DreamApplication {

public static void main(String[] args) {
SpringApplication.run(DreamApplication.class, args);
}

}
  1. 注入AmqpAdmin,rabbitMq的管理组件
1
2
@Autowired
private AmqpAdmin amqpAdmin;

AmqpAdmin有很多方法,比如创建和移除交换机、队列、绑定等,下面简单列举几个

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
/**
* 创建交换器
*
* @Author ZHANGCHAO
* @Date 2020/3/5 9:32
* @param
* @retrun void
**/
@GetMapping("/creatExchange")
public void creatExchange() {
amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
}
/**
* 创建队列
*
* @Author ZHANGCHAO
* @Date 2020/3/5 9:32
* @param
* @retrun void
**/
@GetMapping("/creatQueue")
public void creatQueue() {
amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));
}
/**
* 创建绑定关系
*
* @Author ZHANGCHAO
* @Date 2020/3/5 9:33
* @param
* @retrun void
**/
@GetMapping("/creatBinding")
public void creatBinding() {
amqpAdmin.declareBinding(new Binding("amqpadmin.queue",Binding.DestinationType.QUEUE,
"amqpadmin.exchange","amqp.哈哈",null));
}
  1. 新建RabbitMqService类,加几个监听方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* rabbitMq相关
*
* @author ZHANGCHAO
* @date 2020/3/5 8:34
* @since 1.0.0
*/
@Service
public class RabbitMqService {

@RabbitListener(queues = {"atguigu.news"})
public void getMsg(GyUser user){
System.out.println("接收到消息:"+user);
}

@RabbitListener(queues = {"atguigu.emps"})
public void getMsg1(Message message) {
System.out.println(message);
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
}

@RabbitListener(queues = {“atguigu.emps”}) 监听指定的队列,内部接收数组,可以监听多个队列。如果其中队列接收到消息会执行方法,打印消息。

总结

自动配置

  • 1、RabbitAutoConfiguration

  • 2、有自动配置了连接工厂ConnectionFactory;

  • 3、RabbitProperties 封装了 RabbitMQ的配置

  • 4、 RabbitTemplate :给RabbitMQ发送和接受消息;

  • 5、 AmqpAdmin : RabbitMQ系统管理功能组件;

    ​      AmqpAdmin:创建和删除 Queue,Exchange,Binding
  • 6、@EnableRabbit + @RabbitListener 监听消息队列的内容

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2020 yak33
  • Powered by Hexo Theme Ayer
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信