首页 > 其他分享 >简单-SpringBoot整合RabbitMQ

简单-SpringBoot整合RabbitMQ

时间:2023-02-16 00:44:11浏览次数:48  
标签:SpringBoot springframework annotation RabbitMQ 整合 org msg import public

目录

1.windows下安装erlang环境和rabbitMq服务

https://blog.csdn.net/tirster/article/details/121938987

1.1客户端页面

http://127.0.0.1:15672/#/

注意: 控制台端口是15672 服务端口是5672别弄混了

登陆账户密码:guest guest

2.准备工作

2.1pom依赖

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

2.1启动类注解开启:@EnableRabbit

image

2.2application配置文件

spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest

image

3.队列的简单使用

3.1 配置交换器与队列

package com.demo.mq;

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;

/**
 * 大汉软件
 */
@Configuration
public class RabbitConfig {
    @Bean
    public Queue myQueue(){
        return new Queue("que001",true);
    }
}

3.2 消息发送者

package com.demo.mq;

import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

/**
 * 大汉软件
 */
@RequiredArgsConstructor
@Service
public class QueueProvider {
    private final Queue myQueue;
    private final RabbitTemplate rabbitTemplate;
    /**
     * 发送普通消息
     *
     * @param msg msg
     */
    public void sendMsg(String msg) {
        rabbitTemplate.convertAndSend(myQueue.getName(), msg);
    }

}

3.3 消息消费者

package com.demo.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

/**
 * 大汉软件
 */
@Slf4j
@Service
public class QueueConsumer {
    @RabbitListener(queues = {"que001"})
    public void receive(@Payload String fileBody) {
        log.info("que001队列:" + fileBody);
    }
}

3.4 接口调用:

package com.demo.controller;

import com.demo.mq.QueueProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 大汉软件
 */
@RestController
public class MqController {
    @Autowired
    private QueueProvider queueProvider;

    @GetMapping("/send")
    public String sendMsgToMq(@RequestParam String msg) {
        queueProvider.sendMsg(msg);
        return "ok";
    }
}

结果如下:

image

4.主题的高级使用(Topic Exchange)---主题交换器

主题交换器,生产者将消息丢给它后,它就给与自己绑定的那些个关心这个消息queue全部发送一份消息。 就简单理解成发布-订阅模式就好了。

  • Queue 队列
  • RoutingKey路由key:用来控制交换器如何将消息发送给绑定的队列。例如交换器说了:俺们只投递路由key包含“shusheng007”字样的消息,其他的俺们不处理。 然后来了条消息,这条消息的路由key是“王二狗”,然后交换器一看不对啊,就残忍的拒绝了投递消息到队列的工作。

4.1配置交换器与队列

package com.demo.mq;

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;

/**
 * 大汉软件
 */
@Configuration
public class RabbitConfig {
    @Bean
    public Queue myQueue(){
        return new Queue("que001",true);
    }

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

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

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

    @Bean
    public Binding topicBinding1(){
        return BindingBuilder.bind(topicQueue1()).to(topicExchange())
                .with("que001.id.*");
    }

    @Bean
    public Binding topicBinding2(){
        return BindingBuilder.bind(topicQueue2()).to(topicExchange())
                .with("que001.name.*");
    }
}

4.2消息发送者

package com.demo.mq;

import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

/**
 * 大汉软件
 */
@RequiredArgsConstructor
@Service
public class QueueProvider {
    private final Queue myQueue;
    private final RabbitTemplate rabbitTemplate;
    private final static String EXCHANGE_NAME = "topicExchange";

    /**
     * 发送普通消息
     *
     * @param msg msg
     */
    public void sendMsg(String msg) {
        rabbitTemplate.convertAndSend(myQueue.getName(), msg);
    }

    /**
     * 主题信息
     *
     * @param msg   msg
     * @param route route
     */
    public void sendTopicMsg(String msg, String route) {
        rabbitTemplate.convertAndSend(EXCHANGE_NAME, route, msg);
    }
}

4.3消息消费者

package com.demo.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

/**
 * 大汉软件
 */
@Slf4j
@Service
public class QueueConsumer {
    @RabbitListener(queues = {"que001"})
    public void receive(@Payload String fileBody) {
        log.info("que001队列:" + fileBody);
    }

    @RabbitListener(queues = {"topicQueue1"})
    public void receiveTopic1(@Payload String fileBody) {
        log.info("topic1队列:" + fileBody);
    }

    @RabbitListener(queues = {"topicQueue2"})
    public void receiveTopic2(@Payload String fileBody) {
        log.info("topic2队列:" + fileBody);
    }

}

4.4测试接口类

package com.demo.controller;

import com.demo.mq.QueueProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 大汉软件
 */
@RestController
public class MqController {
    @Autowired
    private QueueProvider queueProvider;

    @GetMapping("/send2")
    public String sendMsgToMq2(@RequestParam String msg, String routeKey) {
        queueProvider.sendTopicMsg(msg, routeKey);
        return "ok";
    }
}

5.参考

https://blog.csdn.net/ShuSheng0007/article/details/125921491

标签:SpringBoot,springframework,annotation,RabbitMQ,整合,org,msg,import,public
From: https://www.cnblogs.com/csh-java/p/17125243.html

相关文章

  • SpringBoot容器化的多环境配置
    SpringBoot容器化的多环境配置部署通常会有多个环境,如"dev"/"test"/"prod"等环境容器化部署通常使用环境变量,而非手动修改配置例子来自《SpringBoot实战派》中,并进行......
  • springboot--多环境启动
    法一:   法二:       ......
  • springboot starter 原理解析及实践
    什么是springbootstarterstarter是springBoot的一个重要部分。通过starter,我们能够快速的引入一个功能,而无需额外的配置。同时starter一般还会给我提供预留的自定配置选......
  • springboot切换web服务器
    <?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:s......
  • SpringBoot整合MyBatis
    SpringBoot整合MyBatis在pom文件中添加以下坐标,增加mybatis依赖,和数据库驱动依赖<!--导入Mybatis依赖坐标--><dependency><groupId>org.mybatis.spring.boot</gr......
  • RabbitMQ-消息中间键
    MQ,中文是消息队列(MessageQueue),字面来看就是存放消息的队列。也就是事件驱动架构中的Broker。快速入门1.publisher实现publicclassPublisherTest{@Testpubl......
  • SpringBoot注册Servlet、Filter和Listener的方式和原理
    1实战在SpringBoot项目中,如果使用内嵌Web服务器,可以很方便地注册Servlet、Filter和Listener等组件。总的来说,包括以下方式:创建实现ServletContextInitializer接口的b......
  • SpringBoot---起步依赖starter
    1、简化spring应用的初始搭建和开发过程。springboot程序优点:自动配置、起步依赖(简化依赖配置)、辅助功能(内置服务器...)<?xmlversion="1.0"encoding="UTF-8"?><pro......
  • SpringBoot-----quickstart
    一、最简SpringBoot程序所包含的基础文件:pom.xml和Application类。spring官网同样可以快速构建springboot工程。 二、SpringBoot项目快速启动1、在lifecycle中双击pac......
  • SpringBoot文件上传丢失Bug记录
    SpringBoot文件上传丢失Bug记录报错如下:java.io.FileNotFoundException:C:\Users\zbz12\AppData\Local\Temp\tomcat.708075972435741143.8080\work\Tomcat\localhost\c......