「RabbitMQ入门三」SpringBoot2.1.5 集成RabbitMQ

「RabbitMQ入门三」SpringBoot2.1.5 集成RabbitMQ

    • 一、修改配置
    • 二、依赖添加
    • 三、配置添加
    • 四、rabbitmq配置类
    • 五、sender和receiver类
    • 六、springboot test测试

「RabbitMQ入门三」SpringBoot2.1.5 集成RabbitMQ_第1张图片

一、修改配置

本文springboot集成rabbitmq用的是它自带的guest账户,一般guest是不允许远程连接的,所以呢,我们先修改下配置

cd /usr/local/rabbitmq/etc/rabbitmq
touch rabbitmq.config
vim rabbitmq.config

添加

[{rabbit, [{loopback_users, []}]}].

然后我们重启下rabbitmq即可

下面我们来用springboot连接rabbitmq
「RabbitMQ入门三」SpringBoot2.1.5 集成RabbitMQ_第2张图片

二、依赖添加


 org.springframework.boot
 spring-boot-starter-amqp

三、配置添加

spring: 
	rabbitmq:
    host: 192.168.11.30
    port: 5672
    username: guest
    password: guest
    virtual-host: /
    listener:
      simple:
        concurrency: 10
        max-concurrency: 10
        prefetch: 1
        auto-startup: true
        default-requeue-rejected: true
    template:
      retry:
        enabled: true
        initial-interval: 1000
        max-attempts: 3
        multiplier: 1.0

四、rabbitmq配置类

MQConfig

@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 HEADER_QUEUE = "header.queue";
 public static final String TOPIC_EXCHANGE = "topicExchage";
 public static final String FANOUT_EXCHANGE = "fanoutxchage";
 public static final String HEADERS_EXCHANGE = "headersExchage";
 
 /**
 * Direct模式 交换机Exchange
 * */
 @Bean
 public Queue queue() {
 	return new Queue(QUEUE, true);
 }
 
 /**
 * Topic模式 交换机Exchange
 * */
 @Bean
 public Queue topicQueue1() {
	 return new Queue(TOPIC_QUEUE1, true);
 }

 @Bean
 public Queue topicQueue2() {
 	return new Queue(TOPIC_QUEUE2, true);
 }

 @Bean
 public TopicExchange topicExchage(){
	 return new TopicExchange(TOPIC_EXCHANGE);
 }

 @Bean
 public Binding topicBinding1() {
	 return BindingBuilder.bind(topicQueue1()).to(topicExchage()).with("topic.key1");
 }

 @Bean
 public Binding topicBinding2() {
 	return BindingBuilder.bind(topicQueue2()).to(topicExchage()).with("topic.#");
 }

 /**
 * Fanout模式 交换机Exchange
 * */
 @Bean
 public FanoutExchange fanoutExchage(){
	 return new FanoutExchange(FANOUT_EXCHANGE);
 }

 @Bean
 public Binding FanoutBinding1() {
	 return BindingBuilder.bind(topicQueue1()).to(fanoutExchage());
 }
 @Bean
 public Binding FanoutBinding2() {
 	 return BindingBuilder.bind(topicQueue2()).to(fanoutExchage());
 }

 /**
 * Header模式 交换机Exchange
 * */
 @Bean
 public HeadersExchange headersExchage(){
	 return new HeadersExchange(HEADERS_EXCHANGE);
 }

 @Bean
 public Queue headerQueue1() {
	 return new Queue(HEADER_QUEUE, true);
 }

 @Bean
 public Binding headerBinding() {
     Map map = new HashMap();
     map.put("header1", "value1");
     map.put("header2", "value2");
     return BindingBuilder.bind(headerQueue1()).to(headersExchage()).whereAll(map).match();
 }
 
}

五、sender和receiver类

MQSender

@Component
@Slf4j
public class MQSender {
 
 @Autowired
 AmqpTemplate amqpTemplate ;
 
 public void send(Object message) {
     String msg = JSONUtil.beanToString(message);
     log.info("send message:"+msg);
     amqpTemplate.convertAndSend(MQConfig.QUEUE, msg);
 }
 
 public void sendTopic(Object message) {
     String msg = JSONUtil.beanToString(message);
     log.info("send topic message:"+msg);
     amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE, "topic.key1", msg+"1");
     amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE, "topic.key2", msg+"2");
 }
 
 public void sendFanout(Object message) {
     String msg = JSONUtil.beanToString(message);
     log.info("send fanout message:"+msg);
     amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE, "", msg);
 }
 
 public void sendHeader(Object message) {
 	String msg = JSONUtil.beanToString(message);
     log.info("send fanout message:"+msg);
     MessageProperties properties = new MessageProperties();
     properties.setHeader("header1", "value1");
     properties.setHeader("header2", "value2");
     Message obj = new Message(msg.getBytes(), properties);
     amqpTemplate.convertAndSend(MQConfig.HEADERS_EXCHANGE, "", obj);
 }
 
}

MQReceiver

@Component 
@Slf4j
public class MQReceiver {
 
 @RabbitListener(queues = MQConfig.QUEUE) 
 public void receive(String message) {
	 log.info("receive message:" + message);
 }

 @RabbitListener(queues = MQConfig.TOPIC_QUEUE1) 
 public void receiveTopic1(String message) {
	 log.info(" topic queue1 message:" + message);
 }

 @RabbitListener(queues = MQConfig.TOPIC_QUEUE2) 
 public void receiveTopic2(String message) {
	 log.info(" topic queue2 message:" + message);
 }

 @RabbitListener(queues = MQConfig.HEADER_QUEUE) 
 public void receiveHeaderQueue(byte[] message) {
 	log.info(" header queue message:" + new String(message));
 }

}

六、springboot test测试

RabbitMQTest

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class RabbitMQTest {

 @Autowired
 private MQSender mqSender;

 @Test
 public void direct(){
	 mqSender.send("hello rabbitmq !");
 }

 @Test
 public void topic(){
 	mqSender.sendTopic("hello rabbitmq !");
 }
}

其他Exchange实现方式有兴趣的可以自己补充测试, llspace!

你可能感兴趣的:(RabbitMQ,spring,boot)