springboot整合rabbitMQ

1、首先要在服务器上安装rabbitmQ服务,安装的教程网上很多

   https://www.cnblogs.com/saryli/p/9149455.html这个安装教程不错,跟着做就行,一定要注意erlang的版本和rabbitmq的版本要对应,不然会安装出错,版本对应问题在rabbitmq官网有描述

 

2、在springboot项目的pmo.xml添加依赖

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

 

3、在spplication.properties文件添加有关rabbitmq的连接信息

spring.application.name=Spring-boot-rabbitmq
spring.rabbitmq.host=192.168.2.194
spring.rabbitmq.port=5672
spring.rabbitmq.username=swtm
spring.rabbitmq.password=swtm
spring.rabbitmq.virtual-host=/

 

4、创建MQConfig rabbitMQ的交换机和队列的配置

复制代码
package com.spring.first.rabbitmq;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQConfig {
    /*
    * Topic模式 一个生产者可以对应多个消费者,但是同一个元素只能被消费一次
    * 一条消息可以发送给多个queue
    * */

    @Bean
    public Queue topicQueue1(){
        return new Queue("topicqueue1",true);
    }
    @Bean
    public  Queue topicQueue2(){
        return new Queue("topicqueue2",true);
    }

    @Bean
    public  Queue directQueue3(){
        return new Queue("directqueue3",true);
    }
    @Bean
    public TopicExchange topicExchange(){
        return  new TopicExchange("topicExChange");
    }
    @Bean
    public Binding topicBinding1(){
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("topic.key1");
    }

    @Bean
    public Binding topicBinding2(){
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("topic.#");
    }


    /*
    * dirct模式 一个生产者对应一个消费者
    *
    * */

    @Bean
    DirectExchange directExchange(){
        return new DirectExchange("directExChange");
    }

    @Bean
    public Binding directBinding(){
        return BindingBuilder.bind(directQueue3()).to(directExchange()).with("direct.key1");
    }

}
复制代码

5、创建rabbitmq的生产者 MQSender

复制代码
package com.spring.first.rabbitmq;


import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MQSender {

    @Autowired
    AmqpTemplate amqpTemplate;



    public void sendTopic(String sendmessage){
      
        amqpTemplate.convertAndSend("topicExChange","topic.key1",sendmessage);
        System.out.println("sender1:"+sendmessage);
    }

    public  void  sendDirect(String sendmessage){ 
amqpTemplate.convertAndSend(
"directExChange","direct.key1",sendmessage);
System.out.println(
"sender2:"+sendmessage);
} }
复制代码

 

6、创建rabbitmq的消费者MQReceiver

复制代码
package com.spring.first.rabbitmq;


import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class MQReceiver {



    @RabbitListener(queues = "topicqueue1")
    public void receiveTopic1(String message){
        System.out.println("消费者2号消费topic1接收到消息:"+message);

    }

    @RabbitListener(queues = "topicqueue1")
    public void receiveTopic2(String message){
        System.out.println("消费者1号消费topicqueue1收到消息:"+message);

    }


    @RabbitListener(queues = "topicqueue2")
    public void receiveTopic3(String message){
        System.out.println("消费者4号消费topicqueue2收到消息:"+message);

    }


    @RabbitListener(queues = "directqueue3")
    public void receiveDirect1(String message){
        System.out.println("消费者接3收到消息:"+message);

    }


}
复制代码

7、通过controller去调用sender

复制代码
package com.spring.first.controller;


import com.spring.first.rabbitmq.MQSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestMQController {

    @Autowired
    private MQSender mqSender;

    @PostMapping("/testmq")
    public void testmq() throws InterruptedException {
        int x=0;
        while (x<10){
            mqSender.sendTopic("消息提醒"+x);
            x++;
        }

    }

    @PostMapping("/testmq2")
    public void testmq2(){
        int x=0;
        while (x<100){
     mqSender.sendDirect("这是第"+x+"条消息");
    x++; 
} } }
复制代码

访问testmq2的接口,结果如图

 

 注意:如果连接配置各方面没有问题但是还是连不上rabbitmq,有可能是用户没有设置can access virtual hosts,在可视化操作界面,重新设置一下即可

posted @   smellycats  阅读(159)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示