说明:本文章只是springboot和rabbitmq的直连整合,只使用队列生产和消费消息,最简单整合!

工程图:

A.总体pom.xml

4.0.0spring-boot-starter-parentorg.springframework.boot2.2.2.RELEASE RabbitMqSpringbootDemoRabbitMqSpringbootDemo1.0-SNAPSHOTpomMqCustomerMqProducerRabbitMqSpringbootDemo Maven Webapphttp://www.example.comUTF-81.81.8RabbitMqSpringbootDemomaven-clean-plugin3.1.0maven-resources-plugin3.0.2maven-compiler-plugin3.8.0maven-surefire-plugin2.22.1maven-war-plugin3.2.2maven-install-plugin2.5.2maven-deploy-plugin2.8.2

B.生产者:MyProducer

1.pom.xml

RabbitMqSpringbootDemoRabbitMqSpringbootDemo1.0-SNAPSHOT4.0.0MqProducerjarMqProducer Maven Webapphttp://www.example.comUTF-81.81.8org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-devtoolstrueorg.springframework.bootspring-boot-starter-amqpcom.alibabafastjson1.2.78org.apache.commonscommons-lang33.4org.projectlomboklombok1.16.10MqProducermaven-clean-plugin3.1.0maven-resources-plugin3.0.2maven-compiler-plugin3.8.0maven-surefire-plugin2.22.1maven-war-plugin3.2.2maven-install-plugin2.5.2maven-deploy-plugin2.8.2

2.application.yml

server:port: 8080spring:rabbitmq:port: 5672host: 192.168.18.145username: adminpassword: adminvirtual-host: /

3.AppP

package com.dev;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * 类名称: * * @author lqw * @date 2024年02月27日 14:20 */@SpringBootApplicationpublic class AppP {public static void main(String[] args) {SpringApplication.run(AppP.class);}}

4.OnlyQueueRabbitConfig

package com.dev.config;import org.springframework.amqp.core.Queue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * 类名称: * * @author lqw * @date 2024年02月28日 10:40 */@Configurationpublic class OnlyQueueRabbitConfig {/** * 队列 * @return onlyQueue队列名称 true 持久化 */@Beanpublic Queue directOnlyQueue(){return new Queue("onlyQueue",true);}}

5.OnlyQueueController

package com.dev.controller;import lombok.extern.slf4j.Slf4j;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * 类名称:直接交换机 消息生产者 * * @author lqw * @date 2024年02月27日 14:47 */@Slf4j@RestController@RequestMapping("onlyQueue")public class OnlyQueueController {@AutowiredRabbitTemplate rabbitTemplate;//使用RabbitTemplate,这提供了接收/发送等等方法/** * Direct测试 * @return */@GetMapping("/sendMessageByOnlyQueue")public String sendMessageByOnlyQueue() {rabbitTemplate.convertAndSend("", "onlyQueue", "你好,Queue!我是直连队列发送的消息!");return "ok";}}

C.消费者:MqCustomer

1.pom.xml

RabbitMqSpringbootDemoRabbitMqSpringbootDemo1.0-SNAPSHOT4.0.0MqCustomerjarMqCustomer Maven Webapphttp://www.example.comUTF-81.81.8org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-devtoolstrueorg.springframework.bootspring-boot-starter-amqporg.apache.commonscommons-lang33.4org.projectlomboklombok1.16.10MqCustomermaven-clean-plugin3.1.0maven-resources-plugin3.0.2maven-compiler-plugin3.8.0maven-surefire-plugin2.22.1maven-war-plugin3.2.2maven-install-plugin2.5.2maven-deploy-plugin2.8.2

2.application.yml

server:port: 8081spring:rabbitmq:port: 5672host: 192.168.18.145username: adminpassword: admin

3.AppC

package cn.ct;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * 类名称: * * @author lqw * @date 2024年02月27日 14:20 */@SpringBootApplicationpublic class AppC {public static void main(String[] args) {SpringApplication.run(AppC.class);}}

4.OnlyQueueRabbitListener

package cn.ct.listeners;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;/** * 类名称:消息消费者 * 直连 * @author lqw * @date 2024年02月27日 14:58 */@Componentpublic class OnlyQueueRabbitListener {@RabbitListener(queues = "onlyQueue")@RabbitHandlerpublic void process(String hello) {System.out.println("Rabbitmq: " + hello);}}

总结:这个是mq消息的最简单的使用,只使用队列!