当前位置:   article > 正文

SpringBoot整合RabbitMQ_springboot整合mq

springboot整合mq

SpringBoot整合RabbitMQ

1.引入依赖

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

 2. 配置RabbitMQ连接

 在application.propertiesapplication.yml中配置RabbitMQ服务器的连接参数

3. DirectExchange

3.1消费者

  1. package com.jyx.consumer;
  2. import org.springframework.amqp.core.*;
  3. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.stereotype.Component;
  7. @Configuration
  8. public class DirectConsumer {
  9. // 声明队列
  10. @Bean
  11. public Queue queue(){
  12. return QueueBuilder.durable("Direct_01").build();
  13. }
  14. // 声明交换机
  15. @Bean
  16. public DirectExchange exchange(){
  17. return ExchangeBuilder.directExchange("Exchange_01").build();
  18. }
  19. // 绑定交换机和队列
  20. @Bean
  21. public Binding binding(Queue queue,DirectExchange exchange){
  22. return BindingBuilder.bind(queue).to(exchange).with("RK01");
  23. }
  24. // 声明消费者
  25. @RabbitListener(queues = "Direct_01")
  26. public void receive(String msg){
  27. System.out.println("Direct_01消费者收到消息:"+msg);
  28. }
  29. }

 3.2 生产者

  1. package com.jyx.producer;
  2. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. @Service
  6. public class DirectProducer {
  7. @Autowired
  8. private RabbitTemplate rabbitTemplate;
  9. // 生产者 发送消息
  10. public void send(Object message) {
  11. rabbitTemplate.convertAndSend("Exchange_01", "RK01", message);
  12. System.out.println("发送消息:" + message);
  13. }
  14. }

4. 启动类

  1. package com.jyx;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class App {
  6. public static void main(String[] args) {
  7. SpringApplication.run(App.class, args);
  8. }
  9. }

  测试类

  1. package com.jyx;
  2. import com.jyx.model.OrderKO;
  3. import com.jyx.producer.DirectProducer;
  4. import org.junit.jupiter.api.Test;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import java.io.IOException;
  8. @SpringBootTest
  9. class AppTest {
  10. @Autowired
  11. private DirectProducer directProducer;
  12. @Test
  13. void contextLoads() throws IOException {
  14. for (int i = 0; i < 10; i++) {
  15. directProducer.send("hello world");
  16. }
  17. System.in.read();
  18. }
  19. }

传对象

创建一个对象

  1. package com.jyx.model;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Builder;
  4. import lombok.Data;
  5. import lombok.NoArgsConstructor;
  6. import java.io.Serializable;
  7. @Data
  8. @AllArgsConstructor
  9. @NoArgsConstructor
  10. @Builder
  11. public class OrderKO implements Serializable {
  12. private int id;
  13. private String name;
  14. }

消费者

把String换成对象

  1. // 声明消费者
  2. @RabbitListener(queues = "Direct_01")
  3. public void receive(OrderKO msg){
  4. System.out.println("Direct_01消费者收到消息:"+msg);
  5. }

测试类

  1. @Test
  2. void contextLoads2() throws IOException {
  3. for (int i = 0; i < 10; i++) {
  4. directProducer.send(OrderKO.builder().id(i).name("张"+i).build());
  5. }
  6. System.in.read();
  7. }

正常情况下

加工具类

  1. package com.jyx.config;
  2. import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
  3. import org.springframework.amqp.support.converter.MessageConverter;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. public class RabbitConfig {
  8. @Bean
  9. public MessageConverter messageConverter() {
  10. return new Jackson2JsonMessageConverter();
  11. }
  12. }

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

闽ICP备14008679号