RabbitMQ入門(四、RabbitMQ+SpringBoot)

前言

Springboot對rabbitmq的支持度很高,所以Springboot繼承rabbitmq十分簡便。

首先maven引入amqp

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

在yml文件中添加配置

spring:
  rabbitmq:
    publisher-confirms: true
    username: hpsyche
    password: a7789858
    virtual-host: /vhost_test
    port: 5672

工作隊列

生產者

package com.hpsyche.rabbitmq_spring.pro;

import com.hpsyche.rabbitmq_spring.constant.RabbitMqConstant;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @author fuzihao
 * @date 2019/8/27 16:26
 */
@Component
public class RabbitProducer {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void sendSimpleOrder(String msg){
        //第一個參數:工作隊列名稱
        //第二個參數:發送的消息
        rabbitTemplate.convertAndSend("Spring_Queue",msg);
    }
}

消費者

package com.hpsyche.rabbitmq_spring.consumer;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author fuzihao
 * @date 2019/8/29 14:45
 */
@Component
public class RabbitConsumer {
    @RabbitListener(queues = "Spring_Queue")
    public void getMsg(String msg){
        System.out.println(msg);
    }
}

再寫一個controller,調用生產者

package com.hpsyche.rabbitmq_spring.controller;

import com.hpsyche.rabbitmq_spring.pro.RabbitProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author fuzihao
 * @date 2019/8/29 14:49
 */
@RestController
@RequestMapping("/msg")
public class MsgController {
    @Autowired
    private RabbitProducer rabbitProducer;
    @GetMapping("/{msg}")
    public String getMsg(@PathVariable String msg){
        rabbitProducer.sendSimpleOrder(msg);
        return "success";
    }
}

此時啓動發現報錯,原因爲工作隊列不存在,手動到官方創建名爲Spirng_Queue的隊列,再次啓動,啓動成功!

訪問http://localhost:8080/msg/hello,控制檯輸出hello,說明配置成功!

Topic模式

首先需要設置文件,如下

package com.hpsyche.rabbitmq_spring.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;

@Configuration
public class TopicRabbitConfig {

    //在消費者中來識別隊列
    final static String message = "goods.addGoods";
    final static String messages = "goods.messages";

    @Bean
    public Queue queueMessage() {
        return new Queue(TopicRabbitConfig.message);
    }

    @Bean
    public Queue queueMessages() {
        return new Queue(TopicRabbitConfig.messages);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("Spring_Exchange");
    }

    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange).with("goods.addGoods");
    }

    @Bean
    Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessages).to(exchange).with("goods.#");
    }
}

有生產者1

package com.hpsyche.rabbitmq_spring.pro;

import com.hpsyche.rabbitmq_spring.constant.RabbitMqConstant;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @author fuzihao
 * @date 2019/8/27 16:26
 */
@Component
public class RabbitProducer {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void sendOrder(String msg){
        rabbitTemplate.convertAndSend("Spring_Exchange","goods.addGoods","add:"+msg);
    }

}

有生產者2

package com.hpsyche.rabbitmq_spring.pro;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @author fuzihao
 * @date 2019/8/27 16:26
 */
@Component
public class RabbitProducerTwo {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void sendOrder(String msg){
        rabbitTemplate.convertAndSend("Spring_Exchange","goods.deleteGoods","delete:"+msg);
    }

}

有消費者1

package com.hpsyche.rabbitmq_spring.consumer;

import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author fuzihao
 * @date 2019/8/29 14:45
 */
@Component
@RabbitListener(queues="goods.messages")
public class RabbitConsumer {

    @RabbitHandler
    public void exchangeGetMsg(@Payload String msg, @Headers Map<String,Object> map) throws IOException {
        System.out.println(msg);
    }
}

消費者2

package com.hpsyche.rabbitmq_spring.consumer;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author fuzihao
 * @date 2019/8/29 14:45
 */
@Component
@RabbitListener(queues="goods.addGoods")
public class RabbitConsumerTwo {

    @RabbitHandler
    public void exchangeGetMsg(@Payload String msg, @Headers Map<String,Object> map){
        System.out.println(msg);
    }
}

同時有如下controller

package com.hpsyche.rabbitmq_spring.controller;

import com.hpsyche.rabbitmq_spring.pro.RabbitProducer;
import com.hpsyche.rabbitmq_spring.pro.RabbitProducerTwo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author fuzihao
 * @date 2019/8/29 14:49
 */
@RestController
@RequestMapping("/msg")
public class MsgController {
    @Autowired
    private RabbitProducer rabbitProducer;
    @Autowired
    private RabbitProducerTwo rabbitProducerTwo;

    @GetMapping("/exchangeAdd/{msg}")
    public String getExchangeAddMsg(@PathVariable String msg) throws InterruptedException {
        rabbitProducer.sendOrder(msg);
        return "success";
    }

    @GetMapping("/exchangeDelete/{msg}")
    public String getExchangeDeleteMsg(@PathVariable String msg) throws InterruptedException {
        rabbitProducerTwo.sendOrder(msg);
        return "success";
    }
}

當瀏覽器訪問http://localhost:8080/msg/exchangeAdd/hello時,控制檯輸出add:hello兩次,說明兩個隊列good.#和good.addGoods都實現了消費;而當瀏覽器訪問http://localhost:8080/msg/exchangeDelete/hello時,控制檯輸出delete:hello一次,說明只有good.#消費者消費了此信息,結果是準確的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章