win10下安装 RabbitMQ_柚几哥哥的博客-CSDN博客
Linux下载安装 RabbitMQ_柚几哥哥的博客-CSDN博客
org.springframework.boot spring-boot-starter-amqp org.springframework.amqp spring-rabbit-test test
spring:#RabbitMQrabbitmq:#服务器地址host: 192.168.10.100#用户名username: guest#密码password: guest#虚拟主机virtual-host: /#端口port: 5672listener:simple:#消费者最小数量concurrency: 10#消费者最大数量max-concurrency: 10#限制消费者每次只处理一条消息,处理完再继续下一条消息prefetch: 1#启动时是否默认启动容器,默认trueauto-startup: true#被拒绝时重新进入队列default-requeue-rejected: truetemplate:retry:#发布重试,默认falseenabled: true#重试时间 默认1000msinitial-interval: 1000#重试最大次数,默认3次max-attempts: 3#重试最大间隔时间,默认10000msmax-interval: 10000#重试间隔的乘数。比如配2.0 第一次等10s,第二次等20s,第三次等40smultiplier: 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 {@Beanpublic 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 {@Autowiredprivate 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";@Beanpublic Queue queue01(){return new Queue(QUEUE01);}@Beanpublic Queue queue02(){return new Queue(QUEUE02);}@Beanpublic FanoutExchange fanoutExchange(){return new FanoutExchange(EXCHANGE);}@Beanpublic Binding binding01(){return BindingBuilder.bind(queue01()).to(fanoutExchange());}@Beanpublic 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 {@Autowiredprivate 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";@Beanpublic Queue queue01(){return new Queue(QUEUE01);}@Beanpublic Queue queue02(){return new Queue(QUEUE02);}@Beanpublic DirectExchange directExchange(){return new DirectExchange(EXCHANGE);}@Beanpublic Binding binding01(){return
BindingBuilder.bind(queue01()).to(directExchange()).with(ROUTINGKEY01);}@Beanpublic 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 {@Autowiredprivate 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");
}
调用 mq/direct02 接口,消息经由交换机绑定的 queue.green RoutingKey 转发到 queue_direct02
队列
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.#";@Beanpublic Queue queue01(){return new Queue(QUEUE01);}@Beanpublic Queue queue02(){return new Queue(QUEUE02);}@Beanpublic TopicExchange topicExchange(){return new TopicExchange(EXCHANGE);}@Beanpublic Binding binding01(){return
BindingBuilder.bind(queue01()).to(topicExchange()).with(ROUTINGKEY01);}@Beanpublic 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 {@Autowiredprivate 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 队列
调用 mq/topic02 接口,消息经由交换机绑定的 *.queue.# 和 #.queue.# RoutingKey 转发到 queue_topic01 和 queue_topic02 队列
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";@Beanpublic Queue queue01(){return new Queue(QUEUE01);}@Beanpublic Queue queue02(){return new Queue(QUEUE02);}@Beanpublic HeadersExchange headersExchange(){return new HeadersExchange(EXCHANGE);}@Beanpublic Binding binding01(){Map map = new HashMap<>();map.put("color","red");map.put("speed","low");return
BindingBuilder.bind(queue01()).to(headersExchange()).whereAny(map).match();}@Beanpublic Binding binding02(){Map 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 {@Autowiredprivate 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");
}