• mq具体使用方式


    代码方式
    第一步方式导入依赖的方式
     
            
                org.springframework.boot
                spring-boot-starter-web
            
            
            
                org.springframework.boot
                spring-boot-starter-amqp
            
            
                junit
                junit
                test
            
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    原理,mq需要消费者(Publisher)发送消息,服务方式(Consumer)接受消息方式
    yml 配置方式
    spring:
      rabbitmq:
        host: 192.168.36.128 #主机端口
        port: 5672
        virtual-host: /  #虚拟主机
        username: itcast
        password: 123321
    server:
      port: 1020
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    Publisher 调用api 方式 RabbitTemplate
      @Autowired
        private RabbitTemplate rabbitTemplate;
    
        @GetMapping("/testSimpleQueue")
        public void testSimpleQueue() {
            // 队列名称
            String queueName = "simple.queue";
            // 消息
            for (int i = 0; i < 50; i++) {
                // 发送消息
                String message = "hello, spring amqp!"+i;
                rabbitTemplate.convertAndSend(queueName, message);
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    说明 rabbitTemplate.convertAndSend(queueName, message);
    queueName 队列名称,如何找到队列名称,这个队列名称,mq自动创建,只需要在发送消息,在消息接受方有对应的队列就可以
    message 发送消息
    Consumer 服务的接受方式
    yml 配置方式
    spring:
      rabbitmq:
        listener:
          simple:
            prefetch: 1
        host: 192.168.36.128
        port: 5672
        virtual-host: /
        username: itcast
        password: 123321
    server:
      port: 1010
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    consumer 接收者代码
    @Component
    public class SpringRabbitListener {
    
        @RabbitListener(queues = "simple.queue")
        public void listenSimpleQueueMessage(String msg) throws InterruptedException {
            System.out.println("spring 1----------:【" + msg + "】"+ LocalTime.now());
            Thread.sleep(20);
        }
    
        @RabbitListener(queues = "simple.queue")
        public void listenSimpleQueueMessageTwo(String msg) throws InterruptedException {
            System.out.println("spring 2------:【" + msg + "】"+ LocalTime.now());
            Thread.sleep(1000);
        }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    解释 RabbitListener 中的队列参数名称queues
    queues队列的参数名称
    目录结构

    在这里插入图片描述

  • 相关阅读:
    Kotlin:协程基础
    腐蚀和膨胀
    尚好房 05_二手房管理
    Oracle的优化控制器optimizer_mode参数说明以及设置
    “ApproachingTheTarget“ app Tech Support(URL)“ app Tech Support(URL)
    QT中常用文本控件区别(LineEdit、TextEdit 、PlainTextEdit )
    android可见即可说实现方案
    Java中的集合内容总结——Collection接口
    spring项目中starter包的原理,以及自定义starter包的使用
    ASO优化之通过页面的优化来提升排名
  • 原文地址:https://blog.csdn.net/weixin_51885039/article/details/134417201