RabbitMQ消息队列

首先引入相关依赖:

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

配置applicaiton.yml:

server:
  port: 8082
spring:
  rabbitmq:
    host: 192.168.37.105
    port: 5672
    username: admin
    password: admin
    virtual-host: /

shop:
  exchange: shop.exchange.direct
  queue: shop.queue
  routingKey: shop.routingKey

编写配置类RabbitMqConfig:

@Configuration
public class RabbitmqConfig {

    @Value("${shop.exchange}")
    private String exchange;
    @Value("${shop.queue}")
    private String queue;
    @Value("${shop.routingKey}")
    private String routingKey;

    @Bean
    public Exchange getExchange(){
        return new DirectExchange(exchange);
    }

    @Bean
    public Queue getQueue(){
        return new Queue(queue);
    }

    @Bean
    public Binding binding(Exchange exchange,Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(routingKey).noargs();
    }

    /**
     * 消息转化器
     * 将对象转化为 Json 字符串
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

如果不配置消息转化器,默认只能处理字符串消息。不能处理对象!!! 

创建生产者:

@RestController
@RequiredArgsConstructor
@RequestMapping("producer02")
public class Producer02Controller {

    @Value("${shop.exchange}")
    private String exchange;

    @Value("${shop.routingKey}")
    private String routingKey;

    private final RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String sendMsg(){
        rabbitTemplate.convertAndSend(exchange,routingKey,new Student(2,"Alice","178",20));
        return "Producer02==>发送成功!";
    }
}

创建消费者(获取消息):

@Component
public class MegListener {

    @RabbitListener(queues = "${shop.queue}")
    public void readStu(String msg) {
        System.out.println("shop.queue:" + msg);
    }

    @RabbitListener(queues = "${shop.queue}")
    public void readStu(Student stu) {
        System.out.println("shop.queue:" + stu);
    }
}

创建实体类:

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Student implements Serializable {
    private Integer id;
    private String name;
    private String phone;
    private Integer age;
}

进行测试: 

 

相关推荐

  1. 消息队列RabbitMQ

    2024-05-13 01:36:05       28 阅读
  2. 消息队列-RabbitMQ

    2024-05-13 01:36:05       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-13 01:36:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-13 01:36:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-13 01:36:05       87 阅读
  4. Python语言-面向对象

    2024-05-13 01:36:05       96 阅读

热门阅读

  1. Kubernetes(k8s)的授权(Authorization)策略解析

    2024-05-13 01:36:05       25 阅读
  2. Leetcode 3147. Taking Maximum Energy From the Mystic Dungeon

    2024-05-13 01:36:05       30 阅读
  3. 前端工程化之---git hooks

    2024-05-13 01:36:05       28 阅读
  4. Git 剔除已经纳入版本管理的文件

    2024-05-13 01:36:05       31 阅读
  5. 代码随想录:二分查找相关题目推荐(35、34)

    2024-05-13 01:36:05       31 阅读