淮北建投网站,全球搜索网站排名,seo外包公司优化,什么是互联网企业说明#xff1a;本文章只是springboot和rabbitmq的直连整合#xff0c;只使用队列生产和消费消息#xff0c;最简单整合#xff01;
工程图#xff1a; A.总体pom.xml
?xml version1.0 encodingUTF-8?project xmlnshttp://…说明本文章只是springboot和rabbitmq的直连整合只使用队列生产和消费消息最简单整合
工程图 A.总体pom.xml
?xml version1.0 encodingUTF-8?project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentartifactIdspring-boot-starter-parent/artifactId !-- 被继承的父项目的构件标识符 --groupIdorg.springframework.boot/groupId !-- 被继承的父项目的全球唯一标识符 --version2.2.2.RELEASE/version !-- 被继承的父项目的版本 --relativePath/ !-- lookup parent from repository --/parentgroupIdRabbitMqSpringbootDemo/groupIdartifactIdRabbitMqSpringbootDemo/artifactIdversion1.0-SNAPSHOT/versionpackagingpom/packagingmodulesmoduleMqCustomer/modulemoduleMqProducer/module/modulesnameRabbitMqSpringbootDemo Maven Webapp/name!-- FIXME change it to the projects website --urlhttp://www.example.com/urlpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.target/propertiesdependencies/dependenciesbuildfinalNameRabbitMqSpringbootDemo/finalNamepluginManagement!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --pluginspluginartifactIdmaven-clean-plugin/artifactIdversion3.1.0/version/plugin!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --pluginartifactIdmaven-resources-plugin/artifactIdversion3.0.2/version/pluginpluginartifactIdmaven-compiler-plugin/artifactIdversion3.8.0/version/pluginpluginartifactIdmaven-surefire-plugin/artifactIdversion2.22.1/version/pluginpluginartifactIdmaven-war-plugin/artifactIdversion3.2.2/version/pluginpluginartifactIdmaven-install-plugin/artifactIdversion2.5.2/version/pluginpluginartifactIdmaven-deploy-plugin/artifactIdversion2.8.2/version/plugin/plugins/pluginManagement/build
/projectB.生产者MyProducer
1.pom.xml
?xml version1.0 encodingUTF-8?project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdRabbitMqSpringbootDemo/artifactIdgroupIdRabbitMqSpringbootDemo/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdMqProducer/artifactIdpackagingjar/packagingnameMqProducer Maven Webapp/name!-- FIXME change it to the projects website --urlhttp://www.example.com/urlpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.target/propertiesdependencies!--spring boot核心--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependency!--spring boot 测试--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!--springmvc web--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--开发环境调试--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdoptionaltrue/optional/dependency!--amqp 支持--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-amqp/artifactId/dependencydependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.78/version/dependency!-- commons-lang3 --dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-lang3/artifactIdversion3.4/version/dependency!--lombok--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.16.10/version/dependency/dependenciesbuildfinalNameMqProducer/finalNamepluginManagement!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --pluginspluginartifactIdmaven-clean-plugin/artifactIdversion3.1.0/version/plugin!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --pluginartifactIdmaven-resources-plugin/artifactIdversion3.0.2/version/pluginpluginartifactIdmaven-compiler-plugin/artifactIdversion3.8.0/version/pluginpluginartifactIdmaven-surefire-plugin/artifactIdversion2.22.1/version/pluginpluginartifactIdmaven-war-plugin/artifactIdversion3.2.2/version/pluginpluginartifactIdmaven-install-plugin/artifactIdversion2.5.2/version/pluginpluginartifactIdmaven-deploy-plugin/artifactIdversion2.8.2/version/plugin/plugins/pluginManagement/build
/project2.application.yml
server:port: 8080
spring: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*/
SpringBootApplication
public 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*/
Configuration
public 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
?xml version1.0 encodingUTF-8?project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdRabbitMqSpringbootDemo/artifactIdgroupIdRabbitMqSpringbootDemo/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdMqCustomer/artifactIdpackagingjar/packagingnameMqCustomer Maven Webapp/name!-- FIXME change it to the projects website --urlhttp://www.example.com/urlpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.target/propertiesdependencies!--spring boot核心--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependency!--spring boot 测试--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!--springmvc web--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--开发环境调试--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdoptionaltrue/optional/dependency!--amqp 支持--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-amqp/artifactId/dependency!-- commons-lang3 --dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-lang3/artifactIdversion3.4/version/dependency!--lombok--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.16.10/version/dependency/dependenciesbuildfinalNameMqCustomer/finalNamepluginManagement!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --pluginspluginartifactIdmaven-clean-plugin/artifactIdversion3.1.0/version/plugin!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --pluginartifactIdmaven-resources-plugin/artifactIdversion3.0.2/version/pluginpluginartifactIdmaven-compiler-plugin/artifactIdversion3.8.0/version/pluginpluginartifactIdmaven-surefire-plugin/artifactIdversion2.22.1/version/pluginpluginartifactIdmaven-war-plugin/artifactIdversion3.2.2/version/pluginpluginartifactIdmaven-install-plugin/artifactIdversion2.5.2/version/pluginpluginartifactIdmaven-deploy-plugin/artifactIdversion2.8.2/version/plugin/plugins/pluginManagement/build
/project2.application.yml
server:port: 8081spring:rabbitmq:port: 5672host: 192.168.18.145username: adminpassword: admin3.AppC
package cn.ct;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 类名称** author lqw* date 2024年02月27日 14:20*/
SpringBootApplication
public 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*/
Component
public class OnlyQueueRabbitListener {RabbitListener(queues onlyQueue)RabbitHandlerpublic void process(String hello) {System.out.println(Rabbitmq : hello);}}总结这个是mq消息的最简单的使用只使用队列