一起做网站女装夏季,界面设计师培训,枣庄网站建设电话,接做网站私活1.Spring 整合RabbitMQ
生产者消费者 创建生产者工程添加依赖配置整合编写代码发送消息 创建消费者工程添加依赖配置整合编写消息监听器 2.创建工程RabbitMQ Producers
spring-rabbitmq-producers
?xml version1.0 encodingUTF-8?
pr…1.Spring 整合RabbitMQ
生产者消费者 创建生产者工程添加依赖配置整合编写代码发送消息 创建消费者工程添加依赖配置整合编写消息监听器 2.创建工程RabbitMQ Producers
spring-rabbitmq-producers
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns: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/modelVersionparentgroupIdorg.example/groupIdartifactIdspring_rabbit_mq/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdspring-rabbitmq-producers/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.1.7.RELEASE/version/dependencydependencygroupIdorg.springframework.amqp/groupIdartifactIdspring-rabbit/artifactIdversion2.1.8.RELEASE/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion5.1.7.RELEASE/version/dependency/dependencies/project
RabbitMQ配置信息rabbitmq.properties
rabbitmq.host127.0.0.1
rabbitmq.port5672
rabbitmq.usernameguest
rabbitmq.passwordguest
rabbitmq.virtual-host/ RabbitMQ的配置信息spring-rabbitmq-producer.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:rabbithttp://www.springframework.org/schema/rabbitxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/rabbithttp://www.springframework.org/schema/rabbit/spring-rabbit.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 加载属性文件 此种方式加载属性文件是给spring的配置文件使用的 --context:property-placeholderlocationclasspath:rabbitmq.properties/!--定义rabbitmq connectionFactory--rabbit:connection-factory idconnectionFactory host${rabbitmq.host}port${rabbitmq.port}username${rabbitmq.username}password${rabbitmq.password}virtual-host${rabbitmq.virtual-host} /!--定义交换机、队列--rabbit:admin connection-factoryconnectionFactory/!--定义持久化队列不存在则自动创建不绑定到默认交换机默认交换机类型为direct,名字”“路由键为队列名称--rabbit:queue idspring_queue namespring_queue auto-declaretrue/!--~~~~~~~~~~~广播模式所有队列都能收到消息~~~~~~~~~--!--定义广播交换机中的持久化队列不存在则自动创建--rabbit:queue idspring_fanout_queue_1 namespring_fanout_queue_1 auto-declaretrue/rabbit:queue idspring_fanout_queue_2 namespring_fanout_queue_2 auto-declaretrue/!--定义广播类型交换机绑定上述两个队列--rabbit:fanout-exchange idspring_fanout_exchange namespring_fanout_exchange auto-declarerabbit:bindingsrabbit:binding queuespring_fanout_queue_1/rabbit:binding queuespring_fanout_queue_2//rabbit:bindings/rabbit:fanout-exchange!--~~~~~~~~~~~通配符模式*匹配一个#匹配多个~~~~~~~~~--!--定义通配符模式--rabbit:queue idspring_topic_queue_star namespring_topic_queue_star auto-declaretrue/rabbit:queue idspring_topic_queue_well namespring_topic_queue_well auto-declaretrue/rabbit:queue idspring_topic_queue_well2 namespring_topic_queue_well2 auto-declaretrue/!--定义通配符交换机绑定上述两个队列--rabbit:topic-exchange idspring_topic_exchange namespring_topic_exchange auto-declarerabbit:bindingsrabbit:binding patternheima.* queuespring_topic_queue_star/rabbit:binding patternhema.# queuespring_topic_queue_well/rabbit:binding patternhema.haha.# queuespring_topic_queue_well2//rabbit:bindings/rabbit:topic-exchange!--定义rabbitTemplate对象操作可以在代码中方便发送消息--rabbit:template idrabbitTemplate connection-factoryconnectionFactory/
/beans
rabbitMQ发送消息的代码ProducerTest.java
package org.example;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(locations classpath:spring-rabbitmq-producer.xml)
public class ProducerTest {//1.注入rabbitTemplateAutowiredprivate RabbitTemplate rabbitTemplate;/*** 测试rabbitMQ的简单工作模式*/Testpublic void testHelloWorld(){//2.发送消息rabbitTemplate.convertAndSend(spring_queue,hello world spring ....);}Testpublic void testFanout(){//2.发送消息rabbitTemplate.convertAndSend(spring_fanout_exchange,,spring fanout ....);}Testpublic void testTopic(){//2.发送消息rabbitTemplate.convertAndSend(spring_topic_exchange,heima.hehe.haha,spring topic ....);}
}3.创建工程RabbitMQ Consumers
RabbitMQ consumer的pom文件和spring-rabbitmq-consumers项目的pom文件一致
RabbitMQ Consumer的配置文件rabbitmq.properties和spring-rabbitmq-consumers项目的rabbitmq.properties文件一致
RabbitMQ Consumer的配置文件spring-rabbitmq-consumer.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:rabbithttp://www.springframework.org/schema/rabbitxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/rabbithttp://www.springframework.org/schema/rabbit/spring-rabbit.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 加载属性文件 此种方式加载属性文件是给spring的配置文件使用的 --context:property-placeholderlocationclasspath:rabbitmq.properties/!--定义rabbitmq connectionFactory--rabbit:connection-factory idconnectionFactory host${rabbitmq.host}port${rabbitmq.port}username${rabbitmq.username}password${rabbitmq.password}virtual-host${rabbitmq.virtual-host} /!--定义监听器--bean idspringQueueListener classorg.example.listener.SpringQueueListener/bean idfanoutListListener1 classorg.example.listener.FanoutListListener1/bean idfanoutListListener2 classorg.example.listener.FanoutListListener2/bean idtopicListListenerStar classorg.example.listener.TopicListListenerStar/bean idtopicListListenerWell classorg.example.listener.TopicListListenerWell/bean idtopicListListenerWell2 classorg.example.listener.TopicListListenerWell2/!--注册监听器容器--rabbit:listener-container connection-factoryconnectionFactory auto-declaretruerabbit:listener refspringQueueListener queue-namesspring_queue/rabbit:listener reffanoutListListener1 queue-namesspring_fanout_queue_1/rabbit:listener reffanoutListListener2 queue-namesspring_fanout_queue_2/rabbit:listener reftopicListListenerStar queue-namesspring_topic_queue_star/rabbit:listener reftopicListListenerWell queue-namesspring_topic_queue_well/rabbit:listener reftopicListListenerWell2 queue-namesspring_topic_queue_well2//rabbit:listener-container!--定义rabbitTemplate对象操作可以在代码中方便发送消息--rabbit:template idrabbitTemplate connection-factoryconnectionFactory/
/beans
编写Listener代码
package org.example.listener;import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;public class SpringQueueListener implements MessageListener {Overridepublic void onMessage(Message message) {//打印消息System.out.println(new String(message.getBody()));}
}监听器在配置文件绑定了对应的队列当消费者启动起来的时候监听到消息时自动消费并打印。
ConsumerTest.java
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(locations classpath:spring-rabbitmq-consumer.xml)
public class ConsumerTest {Testpublic void test1() throws InterruptedException {Thread.sleep(5000);}
}
4.SpringBoot整合RabbitMQ 生产者 1.创建生产者SpringBoot工程 2.引入依赖 3.编写yml配置基本信息配置 4.定义交换机队列及绑定关系的配置类 5.注入RabbitTemplate调用方法完成消息发送 消费者 1.创建消费者SpringBoot工程 2.引入依赖 3.编写yml配置基本信息配置 4.定义监听类使用RabbitListener注解完成队列监听 总结 springboot提供了快速整合RabbitMQ的方式基本信息在yml中配置队列、交换机及绑定关系在配置类中使用Bean的方式配置生产端直接注入RabbitTemplate完成消息发送消费端直接使用RabbitListener完成消息接收
5.创建SpringBoot RabbitMQ Producers
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 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.9/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.example/groupIdartifactIdspringboot-rabbitmq-producers/artifactIdversion0.0.1-SNAPSHOT/versionnamespringboot-rabbitmq-producers/namedescriptionDemo project for Spring Boot Producers/descriptionpropertiesjava.version8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-amqp/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.amqp/groupIdartifactIdspring-rabbit-test/artifactIdscopetest/scope/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdscopetest/scope/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project配置文件application.yml
spring:rabbitmq:host: 127.0.0.1port: 5672username: guestpassword: guestvirtual-host: /
配置类RabbitMQConfig.java
package com.example.config;import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class RabbitMQConfig {public static final String EXCHANGE_NAME boot_topic_exchange;public static final String QUEUE_NAME boot_queue;//1.交换机Bean(bootExchange)public Exchange bootExchange() {return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();}//2.Queue队列Bean(bootQueue)public Queue bootQueue() {return QueueBuilder.durable(QUEUE_NAME).build();}//3.绑定队列和交换机 Binding/*** 绑定* param queue 知道绑定哪个队列* param exchange 知道哪个交换机* routingKey 路由键* return*/Beanpublic Binding bindQueueExchange(Qualifier(bootQueue) Queue queue,Qualifier(bootExchange) Exchange exchange) {return BindingBuilder.bind(queue).to(exchange).with(boot.#).noargs();}
}测试方法
package com.example.test;import com.example.config.RabbitMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;SpringBootTest
RunWith(SpringRunner.class)
public class ProducerTest {//1.注入RabbitTemplageAutowiredprivate RabbitTemplate rabbitTemplate;Testpublic void testSend(){rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,boot.haha,boot mq hello ~~~);}}6.创建SpringBoot RabbitMQ Consumers
配置文件pom.xml和application.yml 和RabbitMQ Producer一样
Consumer的Listener
package com.example.listener;import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;Component
public class RabbitMQListener {RabbitListener(queues boot_queue)public void ListenerQueue(Message message){System.out.println(new String(message.getBody()));}
}测试方法
package com.example.test;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;SpringBootTest
RunWith(SpringRunner.class)
public class ConsumerTest {Testpublic void testReceive(){System.out.println(接收成功);}
}