• 2022年10月30:rabbitmq学习、springboot整合rabbitmq


    目录

    springboot整合rabbitMQ

    1.producer:生产者

     2.consumer:消费者


    springboot整合rabbitMQ

    1.producer:生产者

    1.pom.xml

    
        
            org.springframework.boot
            spring-boot-starter-parent
            2.1.4.RELEASE
        
        
    
            
                org.springframework.boot
                spring-boot-starter-amqp
            
    
            
                org.springframework.boot
                spring-boot-starter-test
            
        
    

    2.启动类

    @SpringBootApplication
    public class ProducerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ProducerApplication.class);
        }
    }
    

    3.application.yml:ip、端口、username、password、虚拟主机

    #配置rabbitmq的基本信息,ip\端口\username\password\虚拟机
    spring:
      rabbitmq:
        host: 172.16.98.13
        port: 5672
        username: guest
        password: guest
        virtual-host: /

    4.配置类:RabbitMQConfig:交换机、队列、他们的关系(rount key)

    @Configuration
    public class RabbitMQConfig {
        public static final String EXCHAGE_NAME = "boot_topic_exchange";
        public static final String QUEUE_NAME = "boot_queue";
    //    1.交换机
        @Bean("bootExchange")
        public Exchange bootExchange(){
            return ExchangeBuilder.topicExchange(EXCHAGE_NAME).durable(true).build();
        }
    //    2.Queue队列
        @Bean("bootQueue")
        public Queue bootQueue(){
            return QueueBuilder.durable(QUEUE_NAME).build();
        }
    
    //    3.队列和交换机绑定关系Bingding
        /**
         * 1.知道哪个队列
         * 2.知道哪个交换机
         * 3.routting key
         */
        @Bean
        public Binding bingQueueExchange(@Qualifier("bootQueue")Queue queue,@Qualifier("bootExchange")Exchange exchange){
            return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
        }
    }

    5.发送消息

    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class ProducerTest {
    
    //    1.注入springTemplate
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        @Test
        public void testSend(){
            rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHAGE_NAME,"boot.haha","boot mq hello...");
        }

     2.consumer:消费者

    1.pom.xml

    
        
            org.springframework.boot
            spring-boot-starter-parent
            2.1.4.RELEASE
        
        
    
            
                org.springframework.boot
                spring-boot-starter-amqp
            
    
            
                org.springframework.boot
                spring-boot-starter-test
            
        
    

    2.启动类

    @SpringBootApplication
    public class ConsumerSpringbootApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerSpringbootApplication.class);
        }
    }

    3.application.yml:ip、端口、username、password、虚拟主机

    #配置rabbitmq的基本信息,ip\端口\username\password\虚拟机
    spring:
      rabbitmq:
        host: 172.16.98.13
        port: 5672
        username: guest
        password: guest
        virtual-host: /

    4.消息监听

    @Component
    public class RabbitMQListener {
        @RabbitListener(queues = "boot_queue")//监听队列的名称
        public void ListenerQueue(Message message){//将来如果有消息会封装在message中,不要导错包core核心包中
            System.out.println(message.getBody());//获取消息体,消息体才是有用内容
        }
    }

     3.小结

    • SpringBoot提供 了快速整合RabbitMQ的方式
    • 基本信息在ym|中配置,队列交互机以及绑定关系在配置类中使用Bean的方式配置
    • 生产端直接注 入RabbitTemplate完成消息发送(交换机和消息)
    • 消费端直接使用@RabbitListener完成消息接收 (监听队列)

    第二章

    一、rabbitmq高级特性

    1.消息可靠性

    confims模式

  • 相关阅读:
    用案例的方式解释 React 18 新特性——并发渲染、自动批处理等
    第二十六章·访问者模式
    机器学习基础:概率论基础
    xctf攻防世界 Web高手进阶区 command_execution
    K8S 污点和容忍度(Taint,Toleration)
    基于Java纯净水商城配送系统设计与实现 开题报告
    Linux网络编程:网络协议及网络传输的基本流程
    酱菜产业:传承美味,点亮生活
    动态顺序表的增删查改(C语言实现)
    时序分析 42 -- 时序数据转为空间数据 (一) 格拉姆角场
  • 原文地址:https://blog.csdn.net/m0_45209551/article/details/127607336