linux安装因为系统不同而有所不同,步骤相对复杂。
windows安装,比较简单,注意版本匹配
配置文件application 这个文件中的配置,由于springboot的版本不同可能有一点差别。注意看文档。
#rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
#消费者数量
spring.rabbitmq.listener.simple.concurrency= 10
spring.rabbitmq.listener.simple.max-concurrency= 10
#消费者每次从队列获取的消息数量
spring.rabbitmq.listener.simple.prefetch= 1
#消费者自动启动
spring.rabbitmq.listener.simple.auto-startup=true
#消费失败,自动重新入队
spring.rabbitmq.listener.simple.default-requeue-rejected= true
#启用发送重试
spring.rabbitmq.template.retry.enabled=true
spring.rabbitmq.template.retry.initial-interval=1000s
spring.rabbitmq.template.retry.max-attempts=3
spring.rabbitmq.template.retry.max-interval=10000
spring.rabbitmq.template.retry.multiplier=1.0
controller
package com.home.list.controller;
import com.home.list.entity.Result;
import com.home.list.rabbitmq.MQsender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MQCon {
@Autowired
MQsender mQsender;
@RequestMapping("/send")
public Result send(){
mQsender.send("我来了");
return Result.success("成功");
}
@RequestMapping("/topic")
public Result topic(){
mQsender.topic("我zou了");
return Result.success("成功");
}
@RequestMapping("/fanout")
public Result fanout(){
mQsender.fanout("我fei了");
return Result.success("成功");
}
@RequestMapping("/headerex")
public Result headerex(){
mQsender.headerex("我kun了");
return Result.success("成功");
}
}
config 队列名,以及exchange的绑定。
package com.home.list.rabbitmq;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
@Configuration
public class MQconfig {
public static final String QUEUE = "queue";
public static final String TOPIC_QUEUE1 = "topic.queue1";
public static final String TOPIC_QUEUE2 = "topic.queue2";
public static final String TOPIC_EXCHANGE = "topicExchange";
public static final String FANOUT_EXCHANGE = "fanoutExchange";
public static final String HEADER_EXCHANGE = "headerExchange";
public static final String HEADER_QUEUE = "headerQueue";
@Bean
public Queue queue() {
return new Queue(QUEUE, true);
}
// @Bean
public Queue topic1() {
return new Queue(TOPIC_QUEUE1, true);
}
// @Bean
public Queue topic2() {
return new Queue(TOPIC_QUEUE2, true);
}
@Bean
public TopicExchange topicExchange() {
return new TopicExchange(TOPIC_EXCHANGE);
}
@Bean
public Binding topicBind() {
return BindingBuilder.bind(topic1()).to(topicExchange()).with("topic.key1");
}
@Bean
public Binding topicBind2() {
return BindingBuilder.bind(topic2()).to(topicExchange()).with("topic.#");
}
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange(FANOUT_EXCHANGE);
}
@Bean
public Binding fanBind1() {
return BindingBuilder.bind(topic1()).to(fanoutExchange());
}
@Bean
public Binding fanBind2() {
return BindingBuilder.bind(topic2()).to(fanoutExchange());
}
@Bean
public HeadersExchange headersExchange() {
return new HeadersExchange(HEADER_EXCHANGE);
}
@Bean
public Queue headerqueue() {
return new Queue(HEADER_QUEUE, true);
}
@Bean
public Binding headBind() {
HashMap<String, Object> map = new HashMap<>();
map.put("head1", "val1");
map.put("head2", "val2");
return BindingBuilder.bind(headerqueue()).to(headersExchange()).whereAll(map).match();
}
}
接收者
package com.home.list.rabbitmq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class MQreceiver {
//这种是Diret模式。其实还有三种模式的。
@RabbitListener(queues = MQconfig.QUEUE)
public void receive(String msg){
log.info("receive:"+msg);
}
@RabbitListener(queues = MQconfig.TOPIC_QUEUE1)
public void receive1(String msg){
log.info("topic1:"+msg);
}
@RabbitListener(queues = MQconfig.TOPIC_QUEUE2)
public void receive2(String msg){
log.info("topic2:"+msg);
}
@RabbitListener(queues = MQconfig.HEADER_QUEUE)
public void reheader(byte[] msg){
log.info("header:"+new String(msg));
}
}
发送者
package com.home.list.rabbitmq;
import com.home.list.redis.RedisSer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class MQsender {
@Autowired
AmqpTemplate amqpTemplate;
public void send(Object msg){
String ms = RedisSer.beanToString(msg);
log.info("send:"+ms);
amqpTemplate.convertAndSend(MQconfig.QUEUE,ms);
}
public void topic(Object msg){
String ms = RedisSer.beanToString(msg);
// log.info("topic:"+ms);
amqpTemplate.convertAndSend(MQconfig.TOPIC_EXCHANGE,"topic.key1",ms+1);
amqpTemplate.convertAndSend(MQconfig.TOPIC_EXCHANGE,"topic.key2",ms+2);
}
public void fanout(Object msg){
String ms = RedisSer.beanToString(msg);
log.info("fanout:"+ms);
amqpTemplate.convertAndSend(MQconfig.FANOUT_EXCHANGE,"",ms+3);
// amqpTemplate.convertAndSend(MQconfig.FANOUT_EXCHANGE,"",ms+2);
}
public void headerex(Object msg) {
String ms = RedisSer.beanToString(msg);
log.info("header:"+ms);
MessageProperties pr = new MessageProperties();
pr.setHeader("head1","val1");
pr.setHeader("head2","val2");
Message message = new Message(ms.getBytes(), pr);
amqpTemplate.convertAndSend(MQconfig.HEADER_EXCHANGE,"",message);
}
}