spring Boot 2.x | 集成 rabbitmq

栏目: Java · 发布时间: 4年前

内容简介:介绍:

springboot 项目中增加入 rabbitmqmq 是系统架构设计中的重要一环, mq 具有系统间解耦,异步通信,流量削峰等优点,但是引入 mq 也意味着要增加系统架构的复杂度,需要考虑到 mq 服务的高可用等问题

rabbitmq

介绍: rabbitMQ 是实现 AMQP (高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗

AMQP ,即 Advanced Message Queuing Protocol ,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计。消息中间件主要用于组件之间的解耦,消息的发送者无需知道消息使用者的存在,反之亦然。 AMQP 的主要特征是面向消息、队列、路由(包括点对点和发布/订阅)、可靠性、安全

集成 rabbitmq

  1. pom文件引入 rabbitmq 依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
  2. application.yml 配置文件中增加 rabbitmq 的相关配置

    spring:
      rabbitmq:
        host: 127.0.0.1
        port: 5672
        username: guest
        password: guest
  3. 创建队列

    /**
     * @author: chenmingyu
     * @date: 2019/5/23 17:29
     * @description: 队列配置类
     */
    @Configuration
    public class RabbitQueueConfig {
    
        /**
         * 测试队列名称
         */
        public static final String TEST_QUEUE = "test";
    
        @Bean
        public Queue Queue() {
            return new Queue(TEST_QUEUE);
        }
    }
  4. 生产者实现

    /**
     * @author: chenmingyu
     * @date: 2019/5/23 17:32
     * @description: 生产者
     */
    @Component
    public class TestProduce {
    
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        /**
         * 发送消息
         */
        public void send(){
            System.out.println("发送消息");
            rabbitTemplate.convertAndSend(RabbitQueueConfig.TEST_QUEUE,"this is test");
        }
    }
  5. 消费者实现

    /**
     * @author: chenmingyu
     * @date: 2019/5/23 17:31
     * @description: 消费者
     */
    @Component
    @RabbitListener(queues = RabbitQueueConfig.TEST_QUEUE)
    public class TestConsumer {
    
        @RabbitHandler
        public void process(String msg) {
            System.out.println("消费消息  : " + msg);
        }
    }
  6. 测试

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringbootRabbitmqApplicationTests {
    
        @Autowired
        private TestProduce testProduce;
    
        @Test
        public void contextLoads() {
            testProduce.send();
        }
    
    }

    输出

    spring Boot 2.x | 集成 rabbitmq


以上所述就是小编给大家介绍的《spring Boot 2.x | 集成 rabbitmq》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Design for ROI

Web Design for ROI

Lance Loveday、Sandra Niehaus / New Riders Press / 2007-10-27 / USD 39.99

Your web site is a business--design it like one. Billions of dollars in spending decisions are influenced by web sites. So why aren't businesses laser-focused on designing their sites to maximize thei......一起来看看 《Web Design for ROI》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具