• 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模式

  • 相关阅读:
    k8s/资源清单
    为了吃透25个技术栈,这份文档帮我省了大量时间,值得一看
    温故知新(十)——UART
    内容交付网络—CDN
    【C/C++】 const
    Linux应用调试之配置修改内核打印用户态段错误信息
    数据结构零基础入门篇(C语言实现)
    【动态规划】dp 路径问题(不同路径、路径最小和、地下城游戏...)
    1封开发信收获7个客户订单,怎么做到21%回复率的?(内涵英文邮件模板)
    大数据怎么学?对大数据开发领域及岗位的详细解读,完整理解大数据开发领域技术体系
  • 原文地址:https://blog.csdn.net/m0_45209551/article/details/127607336