springboot整合rabbitmq(一)

最近看到一篇文章,讲的是mqtt在物联网的使用,在测试使用时,选择的是rabbitmq的mqtt协议,因此测试使用完mqtt后,闲来无事再整合一下rabbitmq,mqtt可以看如下文章

springboot+rabbitmq搭建实现mqtt协议订阅发布_qianQueen的博客-CSDN博客

1.rabbitmq基本讲解

RabbitMQ简单来说就是一个消息队列中间件,用来保存消息和传递消息的一个容器。在此过程中充当一个中间人的作用。 是一种程序对程序的通信方法,其服务器也是以高性能、健壮以及可伸缩性出名的Erlang语言编写而成。

rabbitmq的搭建很简单,在此就不再做展示了。这是安装好的rabbitmq服务端界面 

 在网上找了个根据rabbitmq功能大概制成的图:

生产者发送消息,再由rabbitmq服务端的交换机以及路由和队列一系列操作,再由监听的消费者消费。 主要的交换机分为三种(还有其他交换机,这里只讲主要的三种):

1.Direct Exchange直连型交换机

大致流程,有一个队列绑定到一个直连交换机上,同时赋予一个路由键 routing key 。然后当一个消息携带着路由值为X,这个消息通过生产者发送给交换机时,交换机就会根据这个路由值X去寻找绑定值也是X的队列。

2.Fanout Exchange扇形交换机:

扇型交换机,这个交换机没有路由键概念,就算你绑了路由键也是无视的。 这个交换机在接收到消息后,会直接转发到绑定到它上面的所有队列。  

3.Topic Exchange主题交换机:

主题交换机,这个交换机其实跟直连交换机流程差不多,但是它的特点就是在它的路由键和绑定键之间是有规则的

*  (星号) 用来表示一个单词 (必须出现的)

#  (井号) 用来表示任意数量(零个或多个)单词

注:主题交换机可以实现直连型交换机和扇形交换机的功能。

2.接下来就展示代码,需要一个provider项目和consumer项目。

首先是项目:

pom文件所需依赖:

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

application.yml配置文件:

spring:
    application:
      rabbitmq-provider
    rabbitmq:
      host: 127.0.0.1
      port: 5672
      username: guest
      password: guest

2.1 Direct Exchange直连型交换机代码: 

   配置类文件代码:

/**
 * @Author : liuqian
 * @CreateTime : 2022/11/22
 * @Description :
 **/
@Data
@Configuration
public class DirectRabbitConfig {

    public static String topic = "TestDirectQueue";

    public static String exchange = "TestDirectExchange";

    public static String routingKey = "TestDirectRouting";

    //队列 起名:TestDirectQueue
    @Bean
    public Queue TestDirectQueue() {
        // durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
        // exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
        //   return new Queue("TestDirectQueue",true,true,false);
 
        //一般设置一下队列的持久化就好,其余两个就是默认false
        return new Queue(topic,true);
    }
 
    //Direct交换机 起名:TestDirectExchange
    @Bean
    DirectExchange TestDirectExchange() {
        return new DirectExchange(exchange,true,false);
    }
 
    //绑定  将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with(routingKey);
    }
 
    @Bean
    DirectExchange lonelyDirectExchange() {
        return new DirectExchange("lonelyDirectExchange");
    }
 
 
}

发送消息接口:

/**
 * @Author : liuqian
 * @CreateTime : 2022/11/22
 * @Description :
 **/
@RequestMapping("rabbitmq")
@RestController
public class RabbitmqSendController {
 
    @Resource
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法
 
    @GetMapping("/send-direct-message")
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "directMessage, hello!" + System.currentTimeMillis();
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime", DateUtils.getTime());
        //将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend(DirectRabbitConfig.exchange, DirectRabbitConfig.routingKey, map);
        return "ok";
    }


}

接下来启动provider项目,可在rabbitmq服务端查看发送过去的消息。

 接下来就是创建consumer项目,pom文件和application.yml文件和provider项目一致,这里不展示了,如果消费者只单纯消费消息,则只需要监听队列即可,如果需要作为生产者发送消息,则需要一个DirectRabbitConfig文件(其他交换机示例代码则不再展示消费者的该配置文件)

/**
 * @Author : liuqian
 * @CreateTime : 2022/11/22
 * @Description :
 **/
@Configuration
public class DirectRabbitConfig {
 
    //队列 起名:TestDirectQueue
    @Bean
    public Queue TestDirectQueue() {
        return new Queue("TestDirectQueue",true);
    }
 
    //Direct交换机 起名:TestDirectExchange
    @Bean
    DirectExchange TestDirectExchange() {
        return new DirectExchange("TestDirectExchange");
    }
 
    //绑定  将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
    }
    
}

接下来就是consumer消费者监听类:

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

import java.util.Map;

@Component
@RabbitListener(queues = "TestDirectQueue")//监听的队列名称 TestDirectQueue
public class DirectReceiver {
 
    @RabbitHandler
    public void process(Map testMessage) {
        System.out.println("DirectReceiver消费者1收到消息  : " + testMessage.toString());
    }
 
}

接下来启动consumer端,将收到provider发送的消息。

 直连型交换机是一对一,多个消费者监听了同一个队列,那么会轮询消费,且不会重复消费。

 2.2 Fanout Exchange扇形交换机代码:   

 provider配置类文件代码:

/**
 * @Author : liuqian
 * @CreateTime : 2022/11/22
 * @Description :
 **/
 
@Configuration
public class FanoutRabbitConfig {
 
    /**
     *  创建三个队列 :fanout.test1   fanout.test2
     *  将三个队列都绑定在交换机 fanoutExchange 上
     *  因为是扇型交换机, 路由键无需配置,配置也不起作用
     */
 
 
    @Bean
    public Queue queueA() {
        return new Queue("fanout.test1");
    }
 
    @Bean
    public Queue queueB() {
        return new Queue("fanout.test2");
    }
 
    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }
 
    @Bean
    Binding bindingExchangeA() {
        return BindingBuilder.bind(queueA()).to(fanoutExchange());
    }
 
    @Bean
    Binding bindingExchangeB() {
        return BindingBuilder.bind(queueB()).to(fanoutExchange());
    }

}

provider发送消息接口


    @GetMapping("/send-fanout-message")
    public String sendFanoutMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: testFanoutMessage ";
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        return "ok";
    }

接下来是consumer消费者监听类:

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
 * @Author : liuqian
 * @CreateTime : 2022/11/22
 * @Description :
 **/
@Component
@RabbitListener(queues = "fanout.test1")
public class FanoutReceiverTest1 {
 
    @RabbitHandler
    public void process(Map testMessage) {
        System.out.println("FanoutReceiverTest1消费者收到消息  : " +testMessage.toString());
    }
 
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
 * @Author : liuqian
 * @CreateTime : 2022/11/22
 * @Description :
 **/
@Component
@RabbitListener(queues = "fanout.test2")
public class FanoutReceiverTest2 {
 
    @RabbitHandler
    public void process(Map testMessage) {
        System.out.println("FanoutReceiverTest2消费者收到消息  : " +testMessage.toString());
    }
 
}

先启动provider项目,再启动consumer项目,发送消息:

 

 2.3 Topic Exchange主题交换机代码:

provider配置类:

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * @Author : liuqian
 * @CreateTime : 2022/11/22
 * @Description :
 **/
 
@Configuration
public class TopicRabbitConfig {
    //绑定键
    public final static String student = "topic.school.student";
    public final static String teacher = "topic.school.teacher";

    public static String exchange = "topicExchange";
 
    @Bean
    public Queue firstQueue() {
        return new Queue(TopicRabbitConfig.student);
    }
 
    @Bean
    public Queue secondQueue() {
        return new Queue(TopicRabbitConfig.teacher);
    }
 
    @Bean
    TopicExchange exchange() {
        return new TopicExchange(exchange);
    }
 
 
    //将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
    //这样只要是消息携带的路由键是topic.man,才会分发到该队列
    @Bean
    Binding bindingExchangeMessage() {
        return BindingBuilder.bind(firstQueue()).to(exchange()).with(student);
    }
 
    //将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
    // 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
    @Bean
    Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.#");
    }
 
}

provider新增测试接口:


    @GetMapping("/send-topic-student")
    public String sendTopicMessage1(@RequestParam String message) {
        String messageId = String.valueOf(UUID.randomUUID());
        Map<String, Object> manMap = new HashMap<>();
        manMap.put("messageId", messageId);
        manMap.put("messageData", message+": student ");
        manMap.put("createTime", DateUtils.getTime());
        rabbitTemplate.convertAndSend("topicExchange", "topic.school.student", manMap);
        return "ok";
    }

    @GetMapping("/send-topic-teacher")
    public String sendTopicMessage2(@RequestParam String message) {
        String messageId = String.valueOf(UUID.randomUUID());
        Map<String, Object> womanMap = new HashMap<>();
        womanMap.put("messageId", messageId);
        womanMap.put("messageData", message+": teacher ");
        womanMap.put("createTime", DateUtils.getTime());
        rabbitTemplate.convertAndSend("topicExchange", "topic.school.teacher", womanMap);
        return "ok";
    }

consumer端新增消费者监听:

/**
 * @Author : liuqian
 * @CreateTime : 2022/11/22
 * @Description :
 **/
@Component
@RabbitListener(queues = "topic.school.student")
public class TopicStudentReceiver {
 
    @RabbitHandler
    public void process(Map testMessage) {
        System.out.println("TopicStudentReceiver消费者收到消息  : " + testMessage.toString());
    }
}
/**
 * @Author : liuqian
 * @CreateTime : 2022/11/22
 * @Description :
 **/
 
@Component
@RabbitListener(queues = "topic.school.teacher")
public class TopicTeacherReceiver {
 
    @RabbitHandler
    public void process(Map testMessage) {
        System.out.println("TopicTeacherReceiver消费者收到消息  : " + testMessage.toString());
    }
}

consumer端控制台展示:

TopicTeacherReceiver绑定的teacher队列绑定的routingKey为topic.#,不管是student生产者还是teacher生产者发送的消息,TopicTeacherReceiver都收到了消息。

以上就是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、付费专栏及课程。

余额充值