赞
踩
win10下安装 RabbitMQ_柚几哥哥的博客-CSDN博客
Linux下载安装 RabbitMQ_柚几哥哥的博客-CSDN博客
- <!--RabbitMQ-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-amqp</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.amqp</groupId>
- <artifactId>spring-rabbit-test</artifactId>
- <scope>test</scope>
- </dependency>
- spring:
- #RabbitMQ
- rabbitmq:
- #服务器地址
- host: 192.168.10.100
- #用户名
- username: guest
- #密码
- password: guest
- #虚拟主机
- virtual-host: /
- #端口
- port: 5672
- listener:
- simple:
- #消费者最小数量
- concurrency: 10
- #消费者最大数量
- max-concurrency: 10
- #限制消费者每次只处理一条消息,处理完再继续下一条消息
- prefetch: 1
- #启动时是否默认启动容器,默认true
- auto-startup: true
- #被拒绝时重新进入队列
- default-requeue-rejected: true
- template:
- retry:
- #发布重试,默认false
- enabled: true
- #重试时间 默认1000ms
- initial-interval: 1000
- #重试最大次数,默认3次
- max-attempts: 3
- #重试最大间隔时间,默认10000ms
- max-interval: 10000
- #重试间隔的乘数。比如配2.0 第一次等10s,第二次等20s,第三次等40s
- multiplier: 1.0
- package com.xxxx.seckill.config;
- import org.springframework.amqp.core.Queue;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Configuration
- public class RabbitMQConfig {
- @Bean
- public Queue queue(){
- return new Queue("queue",true);
- }
- }
- package com.xxxx.seckill.rabbitmq;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Service
- @Slf4j
- public class MQSender {
- @Autowired
- private RabbitTemplate rabbitTemplate;
- public void send(Object msg) {
- log.info("发送消息:"+msg);
- rabbitTemplate.convertAndSend("queue", msg);
- }
- }
- /**
- * @author zyw
- * @since 1.0.0
- */
- @Service
- @Slf4j
- public class MQReceiver {
-
-
- @RabbitListener(queues = "queue")
- public void receive(Object msg) {
- log.info("接受消息:" + msg);
- }
- }
- /**
- * 测试发送RabbitMQ消息
- */
- @RequestMapping("/mq")
- @ResponseBody
- public void mq() {
- mqSender.send("Hello");
- }
- package com.xxxx.seckill.config;
- import org.springframework.amqp.core.Binding;
- import org.springframework.amqp.core.BindingBuilder;
- import org.springframework.amqp.core.FanoutExchange;
- import org.springframework.amqp.core.Queue;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Configuration
- public class RabbitMQConfig {
- private static final String QUEUE01 = "queue_fanout01";
- private static final String QUEUE02 = "queue_fanout02";
- private static final String EXCHANGE = "fanoutExchange";
- @Bean
- public Queue queue01(){
- return new Queue(QUEUE01);
- }
- @Bean
- public Queue queue02(){
- return new Queue(QUEUE02);
- }
- @Bean
- public FanoutExchange fanoutExchange(){
- return new FanoutExchange(EXCHANGE);
- }
- @Bean
- public Binding binding01(){
- return BindingBuilder.bind(queue01()).to(fanoutExchange());
- }
- @Bean
- public Binding binding02(){
- return BindingBuilder.bind(queue02()).to(fanoutExchange());
- }
- }
- package com.xxxx.seckill.rabbitmq;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Service
- @Slf4j
- public class MQSender {
- @Autowired
- private RabbitTemplate rabbitTemplate;
- public void send(Object msg) {
- log.info("发送消息:"+msg);
- rabbitTemplate.convertAndSend("fanoutExchange","",msg);
- }
- }
- package com.xxxx.seckill.rabbitmq;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Service;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Service
- @Slf4j
- public class MQReceiver {
- @RabbitListener(queues = "queue_fanout01")
- public void receive01(Object msg) {
- log.info("QUEUE01接受消息:" + msg);
- }
- @RabbitListener(queues = "queue_fanout02")
- public void receive02(Object msg) {
- log.info("QUEUE02接受消息:" + msg);
- }
- }
- /**
- * 测试发送RabbitMQ消息
- */
- @RequestMapping("/mq/fanout")
- @ResponseBody
- public void mq() {
- mqSender.send("Hello");
- }
调用 mq/direct01 接口,消息经由交换机转发到绑定该交换机的所有队列
- package com.xxxx.seckill.config;
- import org.springframework.amqp.core.Binding;
- import org.springframework.amqp.core.BindingBuilder;
- import org.springframework.amqp.core.DirectExchange;
- import org.springframework.amqp.core.Queue;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Configuration
- public class RabbitMQConfig {
- private static final String QUEUE01 = "queue_direct01";
- private static final String QUEUE02 = "queue_direct02";
- private static final String EXCHANGE = "directExchange";
- private static final String ROUTINGKEY01 = "queue.red";
- private static final String ROUTINGKEY02 = "queue.green";
- @Bean
- public Queue queue01(){
- return new Queue(QUEUE01);
- }
- @Bean
- public Queue queue02(){
- return new Queue(QUEUE02);
- }
- @Bean
- public DirectExchange directExchange(){
- return new DirectExchange(EXCHANGE);
- }
- @Bean
- public Binding binding01(){
- return
- BindingBuilder.bind(queue01()).to(directExchange()).with(ROUTINGKEY01);
- }
- @Bean
- public Binding binding02(){
- return
- BindingBuilder.bind(queue02()).to(directExchange()).with(ROUTINGKEY02);
- }
- }
- package com.xxxx.seckill.rabbitmq;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Service
- @Slf4j
- public class MQSender {
- @Autowired
- private RabbitTemplate rabbitTemplate;
- public void send01(Object msg) {
- log.info("发送red消息:"+msg);
- rabbitTemplate.convertAndSend("directExchange","queue.red",msg);
- }
- public void send02(Object msg) {
- log.info("发送green消息:"+msg);
- rabbitTemplate.convertAndSend("directExchange","queue.green",msg);
- }
- }
- package com.xxxx.seckill.rabbitmq;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Service;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Service
- @Slf4j
- public class MQReceiver {
- @RabbitListener(queues = "queue_direct01")
- public void receive01(Object msg) {
- log.info("QUEUE01接受消息:" + msg);
- }
- @RabbitListener(queues = "queue_direct02")
- public void receive02(Object msg) {
- log.info("QUEUE02接受消息:" + msg);
- }
- }
- /**
- * 测试发送RabbitMQ消息
- */
- @RequestMapping("/mq/direct01")
- @ResponseBody
- public void mq01() {
- mqSender.send01("Hello,Red");
- }
- /**
- * 测试发送RabbitMQ消息
- */
- @RequestMapping("/mq/direct02")
- @ResponseBody
- public void mq02() {
- mqSender.send02("Hello,Green");
- }
- package com.xxxx.seckill.config;
- 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 zhoubin
- * @since 1.0.0
- */
- @Configuration
- public class RabbitMQConfig {
- private static final String QUEUE01 = "queue_topic01";
- private static final String QUEUE02 = "queue_topic02";
- private static final String EXCHANGE = "topicExchange";
- private static final String ROUTINGKEY01 = "#.queue.#";
- private static final String ROUTINGKEY02 = "*.queue.#";
- @Bean
- public Queue queue01(){
- return new Queue(QUEUE01);
- }
- @Bean
- public Queue queue02(){
- return new Queue(QUEUE02);
- }
- @Bean
- public TopicExchange topicExchange(){
- return new TopicExchange(EXCHANGE);
- }
- @Bean
- public Binding binding01(){
- return
- BindingBuilder.bind(queue01()).to(topicExchange()).with(ROUTINGKEY01);
- }
- @Bean
- public Binding binding02(){
- return
- BindingBuilder.bind(queue02()).to(topicExchange()).with(ROUTINGKEY02);
- }
- }
- package com.xxxx.seckill.rabbitmq;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Service
- @Slf4j
- public class MQSender {
- @Autowired
- private RabbitTemplate rabbitTemplate;
- public void send01(Object msg) {
- log.info("发送消息(被01队列接受):"+msg);
- rabbitTemplate.convertAndSend("topicExchange","queue.red.message",msg);
- }
- public void send02(Object msg) {
- log.info("发送消息(被两个queue接受):"+msg);
-
- rabbitTemplate.convertAndSend("topicExchange","message.queue.green.abc",msg);
- }
- }
- package com.xxxx.seckill.rabbitmq;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Service;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Service
- @Slf4j
- public class MQReceiver {
- @RabbitListener(queues = "queue_topic01")
- public void receive01(Object msg) {
- log.info("QUEUE01接受消息:" + msg);
- }
- @RabbitListener(queues = "queue_topic02")
- public void receive02(Object msg) {
- log.info("QUEUE02接受消息:" + msg);
- }
- }
- /**
- * 测试发送RabbitMQ消息
- */
- @RequestMapping("/mq/topic01")
- @ResponseBody
- public void mq01() {
- mqSender.send01("Hello,Red");
- }
- /**
- * 测试发送RabbitMQ消息
- */
- @RequestMapping("/mq/topic02")
- @ResponseBody
- public void mq02() {
- mqSender.send02("Hello,Green");
- }
调用 mq/topic01 接口,消息经由交换机绑定的 #.queue.# RoutingKey 转发到 queue_topic01 队列
- package com.xxxx.seckill.config;
- import org.springframework.amqp.core.Binding;
- import org.springframework.amqp.core.BindingBuilder;
- import org.springframework.amqp.core.HeadersExchange;
- import org.springframework.amqp.core.Queue;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Configuration
- public class RabbitMQConfig {
- private static final String QUEUE01 = "queue_header01";
- private static final String QUEUE02 = "queue_header02";
- private static final String EXCHANGE = "headersExchange";
- @Bean
- public Queue queue01(){
- return new Queue(QUEUE01);
- }
- @Bean
- public Queue queue02(){
- return new Queue(QUEUE02);
- }
- @Bean
- public HeadersExchange headersExchange(){
- return new HeadersExchange(EXCHANGE);
- }
- @Bean
- public Binding binding01(){
- Map<String,Object> map = new HashMap<>();
- map.put("color","red");
- map.put("speed","low");
- return
- BindingBuilder.bind(queue01()).to(headersExchange()).whereAny(map).match();
- }
- @Bean
- public Binding binding02(){
- Map<String,Object> map = new HashMap<>();
- map.put("color","red");
- map.put("speed","fast");
- return
- BindingBuilder.bind(queue02()).to(headersExchange()).whereAll(map).match();
- }
- }
- package com.xxxx.seckill.rabbitmq;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.core.Message;
- import org.springframework.amqp.core.MessageProperties;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Service
- @Slf4j
- public class MQSender {
- @Autowired
- private RabbitTemplate rabbitTemplate;
- public void send01(String msg) {
- log.info("发送消息(被两个queue接受):" + msg);
- MessageProperties properties = new MessageProperties();
- properties.setHeader("color", "red");
- properties.setHeader("speed", "fast");
- Message message = new Message(msg.getBytes(), properties);
- rabbitTemplate.convertAndSend("headersExchange", "", message);
- }
- public void send02(String msg) {
- log.info("发送消息(被01队列接受):" + msg);
- MessageProperties properties = new MessageProperties();
- properties.setHeader("color", "red");
- properties.setHeader("speed", "normal");
- Message message = new Message(msg.getBytes(), properties);
- rabbitTemplate.convertAndSend("headersExchange", "", message);
- }
- }
- package com.xxxx.seckill.rabbitmq;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.core.Message;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Service;
- /**
- * @author zhoubin
- * @since 1.0.0
- */
- @Service
- @Slf4j
- public class MQReceiver {
- @RabbitListener(queues = "queue_header01")
- public void receive01(Message message) {
- log.info("QUEUE01接受Message对象:" + message);
- log.info("QUEUE01接受消息:" + new String(message.getBody()));
- }
- @RabbitListener(queues = "queue_header02")
- public void receive02(Message message) {
- log.info("QUEUE02接受Message对象:" + message);
- log.info("QUEUE02接受消息:" + new String(message.getBody()));
- }
- }
- /**
- * 测试发送RabbitMQ消息
- */
- @RequestMapping("/mq/header01")
- @ResponseBody
- public void mq01() {
- mqSender.send01("Hello,header01");
- }
- /**
- * 测试发送RabbitMQ消息
- */
- @RequestMapping("/mq/header02")
- @ResponseBody
- public void mq02() {
- mqSender.send02("Hello,header02");
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。