【RabbitMq学习day1】spring boot简单整合RabbitMq示例

一、RabbitMq介绍

RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件)。RabbitMQ服务器是用Erlang语言编写的,而群集和故障转移是构建在开放电信平台框架上的。所有主要的编程语言均有与代理接口通讯的客户端库。

相关推荐博客:
spring boot 整合RabbitMq详细篇
六种模式详细介绍
二、简单使用

  1. 下载并安装RabbitMq
    Mac :使用brew install rabbitmq 即可
    mac参考安装方法
    windows :下载并配置环境变量
    Windows安装参考

  2. 启动RabbitMq并打开访问
    rabbitmq-server -detached 后台启动
    输入 http://127.0.0.1:15672/进入登录界面,输入username:guest password:guest

  3. 进入界面,可手动进行添加Queues以及virtual-host

  4. 进入正题,开始整合
    关于RabbitMq的大致理解,生产者与消费者关系,而rabbitMq是一个中间件进行消息存储的器件,当生存者(对应一个生产者类)产生消息(消息队列),就将其存入其中,成为ready状态,待消费,而消费者检查到有消息则进行消费。

所以说将分为以下结构。
在这里插入图片描述
配置类

package com.example.demo.rabbitmq.direct;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {
 

    @Bean
    public org.springframework.amqp.core.Queue queue(){

        return new org.springframework.amqp.core.Queue("queue-2");
    }


}

生产类

package com.example.demo.rabbitmq.direct;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Random;

//@Component
@Controller
public class Provider {

    @Autowired
    AmqpTemplate rabbitmqTemplate;
    // testQueue消息产生处

    @RequestMapping("/send")
   public void send(String context){

       Random random = new Random();
       // user发送消息
       String info = "user"+random.nextInt(100)+context;

       System.out.println(info);

       this.rabbitmqTemplate.convertAndSend("queue-1",info);

   }

}

消费类

package com.example.demo.rabbitmq.direct;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "queue-1")
public class Consumer2 {
    @RabbitHandler
    public void process(String testQueue){
        System.out.println("receiver2:"+testQueue);
    }

}

中心配置类

spring.rabbitmq.addresses=127.0.0.1:5672
#spring.rabbitmq.port=
spring.rabbitmq.password=guest
spring.rabbitmq.username=guest

模拟测试

   @Test
    void testRabbitMq(){
        for(int i=0;i<10;i++)
        provider.send("我爱你啊..");
    }

启动测试过程:
(1).当未进行消费者的配置(注释掉component)时,20条消息ready,准备待消费在这里插入图片描述
(2)当进行消费者配置时,首先将消息消费完,再进行生产并竞争消费。
在这里插入图片描述

最后,此次实验是在默认的host情况下,不少博文是在virtual-host下,因此如需在virtual-host情况下,需要进行以下配置。

中心配置类,进行virtual-port配置,rabbitmq进行相应添加

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值