SpringBoot整合RabbitMq

SpringBoot整合RabbitMq

RabbitMQ是基于 erlang 语言开发的, 为了使用 rabbitMQ 需要安装 erlang环境。

安装erlang 环境

  1. 下载地址 https://www.erlang.org/downloads
    在这里插入图片描述选择自己适合的操作系统下载
  2. 配置环境变量 在这里插入图片描述
  3. 测试是否安装成功
    win+r打开cmd,输入erl,出现如图所示内容表示安装成功
    ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190628103023745.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NjgyNTQ2,size_16,color_FFFFFF,t_70

安装RabbitMQ

1.下载地址https://www.rabbitmq.com/download.html
在这里插入图片描述 选择自己的盘符默认下一步安装就行
2.配置插件
win+r打开cmd输入以下命令
“C:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.5\sbin\rabbitmq-plugins.bat” enable rabbitmq_management
在这里插入图片描述
出现如图所示表示插件配置成功。
3.重启 RabbitMQ
以管理员身份运行以下命令:
net stop RabbitMQ && net start RabbitMQ
在这里插入图片描述
4.访问管理界面
http://127.0.0.1:15672
输入账号: guest,密码: guest
在这里插入图片描述登陆成功页面如下

在这里插入图片描述至此环境准备好,下面进行springboot和rabbitmq的整合

整合SprinBoot和RabbitMQ

1.创建springboot项目,pom.xml引入下面依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
	<version>1.5.2.RELEASE</version>
</dependency>

2.创建application.properties配置文件

#配置rabbitMQ的支持
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

3.创建RabbitmqConfig配置类

/**
   *创建fanout模式队列
   */
    @Bean
    public Queue queueA(){
        return new Queue("queueA");
    }

    @Bean
    public Queue queueB(){
        return new Queue("queueB");
    }

    /**
     *创建topic模式队列
     */
    @Bean
    public Queue queueMessage() {
        return new Queue("topic.message");
    }

    @Bean
    public Queue queueMessages() {
        return new Queue("topic.messages");
    }

    /**
     *创建交换器
     */
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topicExchange");
    }

    /**
     * 交换器和队列绑定 fanout模式
     * */
    @Bean
    public Binding bindingExchangeQueueFanoutA(Queue queueA,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(queueA).to(fanoutExchange);
    }
    @Bean
    public Binding bindingExchangeQueueFanoutB(Queue queueB,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(queueB).to(fanoutExchange);
    }

    /**
     * 交换器和队列绑定 topic模式
     * */
    @Bean
    public Binding bindingExchangeMessage(Queue queueMessage,TopicExchange topicExchange){
        return BindingBuilder.bind(queueMessage).to(topicExchange).with("topic.message");
    }

    @Bean
    Binding bindingExchangeMessages(Queue queueMessages, TopicExchange topicExchange) {
        return BindingBuilder.bind(queueMessages).to(topicExchange).with("topic.#");
    }

4.创建生产者

@Component
public class Producer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * fanout模式生产消息
     */
    public void fanoutProduction() {
        for (int i = 0; i < 10; i++) {
            String message = "消息" + i;
            rabbitTemplate.convertAndSend("fanoutExchange","", message);
        }
    }

    /**
     * topic模式生产消息
     */
    public void topicProduction() {
        String message = "topic.message";
        rabbitTemplate.convertAndSend("topicExchange", "topic.message", message);

        String messages = "topic.messages";
        rabbitTemplate.convertAndSend("topicExchange", "topic.messages", messages);
    }
}

5.创建消费者
fanout的模式这里只实现了单个生产者对多个消费者的情况
两个消费者如下:

@Component
@RabbitListener(queues = "queueA")
public class FanoutConsumeA {

    @RabbitHandler
    public void  consume(String msg){
        System.out.println("queueA:" + msg) ;
    }
}
@Component
@RabbitListener(queues = "queueB")
public class FanoutConsumeB {
    @RabbitHandler
    public void  consume(String msg){
        System.out.println("queueB:" + msg);
    }
}

topic模式消费者如下

@Component
@RabbitListener(queues = "topic.message")
public class TopicConsumeMessage {

    @RabbitHandler
    public void consume(String msg){
        System.out.println("topic.message:" + msg) ;
    }

}
@Component
@RabbitListener(queues = "topic.messages")
public class TopicConsumeMessages {

    @RabbitHandler
    public void consume(String msg){
        System.out.println("topic.messages:" + msg) ;
    }
}

6.创建启动类访问

@RestController
@RequestMapping("/rabbit")
public class RabbitController {
    @Autowired
    private Producer producer;

    @GetMapping("/fanout")
    public void fanout(){
       producer.fanoutProduction();
    }

    @GetMapping("/topic")
    public void topic(){
        producer.topicProduction();
    }
}

启动项目
访问 http://127.0.0.1:8080/rabbit/fanout
在这里插入图片描述
访问 http://127.0.0.1:8080/rabbit/topic
在这里插入图片描述
完整代码
https://gitee.com/zhching/springboot-rabbitmq

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot框架可以很容易地与RabbitMQ进行集成。为了实现这个目标,你需要在项目的依赖项中添加两个关键的依赖项。首先,你需要添加spring-boot-starter-amqp依赖项,它提供了与RabbitMQ进行通信的必要类和方法。其次,你还需要添加spring-boot-starter-web依赖项,以便在项目中使用Web功能。 在你的项目中创建两个Spring Boot应用程序,一个是RabbitMQ的生产者,另一个是消费者。通过这两个应用程序,你可以实现消息的发送和接收。生产者应用程序负责将消息发送到RabbitMQ的消息队列,而消费者应用程序则负责从队列中接收并处理消息。这样,你就可以实现基于RabbitMQ的消息传递系统。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot整合RabbitMQ](https://blog.csdn.net/K_kzj_K/article/details/106642250)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Springboot 整合RabbitMq ,用心看完这一篇就够了](https://blog.csdn.net/qq_35387940/article/details/100514134)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [undefined](undefined)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值