SpringBoot Rabbit 整合

SpringBoot整合RabbitMQ非常简单!因为SpringBoot太强大了,话不多说直接上demo,项目的整体框架如下所示:

SpringBoot Rabbit 整合_第1张图片
image.png

resources文件夹下的 application.properties的文件如下:

spring.rabbitmq.host=10.**.***.**
spring.rabbitmq.port=5672
spring.rabbitmq.username=******
spring.rabbitmq.password=******
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-returns=true
spring.rabbitmq.template.mandatory=true

项目的pom.xml文件如下所示:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.3.RELEASE
         
    
    SpringBoot.rabbitMq
    rabbtt
    0.0.1-SNAPSHOT
    rabbtt
    Demo project for Spring Boot

    
        1.8
    

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


SpringBoot启动类:RabbttApplication


package SpringBoot.rabbitMq.rabbtt;

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.amqp.core.TopicExchange;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class RabbttApplication {
    
    
    @Bean
    public Queue helloQueue() {
        return new Queue("hello");
    }
    
    @Bean
    public Queue userQueue() {
        return new Queue("entity");
    }
    
    
    //===============以下是验证topic Exchange的队列==========
    @Bean
    public Queue topicMessageA() {
        return new Queue("topic.A");
    }
    
    @Bean
    public Queue topicMessageB() {
        return new Queue("topic.B");
    }
    
    @Bean
    TopicExchange exchange() {
        return new TopicExchange("exchange");
    }
    
    @Bean
    Binding bindingtopicMessageA(Queue topicMessageA,TopicExchange exchange) {
        return BindingBuilder.bind(topicMessageA).to(exchange).with("topic.A");
    }
    
    @Bean
    Binding bindingtopicMessageB(Queue topicMessageB,TopicExchange exchange) {
        return BindingBuilder.bind(topicMessageB).to(exchange).with("topic.#");//*表示一个词,#表示零个或多个词
    }
    
    //===============以上是验证topic Exchange的队列==========
    
    
    
    
    //===============以下是验证Fanout Exchange的队列==========
    @Bean
    public Queue fanoutMessageA() {
        return new Queue("fanout.A");
    }

    @Bean
    public Queue fanoutMessageB() {
        return new Queue("fanout.B");
    }

    @Bean
    public Queue fanoutMessageC() {
        return new Queue("fanout.C");
    }
    
    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }
    
    
    @Bean
    Binding bindingExchangeA(Queue fanoutMessageA,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutMessageA).to(fanoutExchange);
    }
    
    @Bean
    Binding bindingExchangeB(Queue fanoutMessageB,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutMessageB).to(fanoutExchange);
    }
    
    @Bean
    Binding bindingExchangeC(Queue fanoutMessageC,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutMessageC).to(fanoutExchange);
    }

    public static void main(String[] args) {
        SpringApplication.run(RabbttApplication.class, args);
    }
    
    

}

二.SpringBoot整合RabbitMQ(Direct模式)

任何发送到Direct Exchange 上的消息都会转发到RouteKey指定的queue中。
1.一般使用RabbitMp自带的Exchange。
2.这种模式不需要对Exhange进行任何绑定(binding)操作。
3.消息传递时只需要一个简单的RouteKey,可理解为接收消息队列的名字,如RabbttApplication启动类中的“hello”,代码如下所示。
4.如果vhost中不存在RouteKey中指定的队列名,则该消息会被抛弃。

SpringBoot Rabbit 整合_第2张图片
image.png

直接在启动类RabbttApplication中添加queues消息队列如下所示,

@Bean
   public Queue helloQueue() {
       return new Queue("hello");
   }

在hello文件夹中创建SingletonHello类发送消息,在SpringBoot中直接使用AmqpTemplate去发送消息,代码如下:

package SpringBoot.rabbitMq.rabbtt.hello;

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

@Component
public class SingletonHello {
    
    @Autowired
    private AmqpTemplate amqpTemplate;
    
    public void send() {
        String sendMsg = "wan :" + "*****hello rabbit****";
        System.out.println("wan :" + sendMsg);
        amqpTemplate.convertAndSend("hello", sendMsg);
    }

}

在hello文件夹中创建SingletonHelloReceiver类来接受消息,代码如下所示:

package SpringBoot.rabbitMq.rabbtt.hello;

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

@Component
@RabbitListener(queues =  "hello")
public class SingletonHelloReceiver {
    
    @RabbitHandler
    public void receiver(String hello) {
        System.out.println("*****SingletonHelloReceiver*****" + hello);
    }

}

验证测试代码,在controller文件夹中创建RabbitTestController类来测试,代码如下所示:

@RestController
@RequestMapping("/rabbit")
public class RabbitTestController {
    
    @Autowired
    private SingletonHello singletonHello;
     
        
    @GetMapping("/hello")
    public void hello () {
        singletonHello.send();
    }
}

在网页中输入:http://localhost:8080/rabbit/hello;控制台输出如下所示:

wan :wan :*****hello rabbit****
*****SingletonHelloReceiver*****wan :*****hello rabbit****

三.SpringBoot整合RabbitMQ(topic模式)

任何发送到Topic Exchange的消息都会被转发到RouteKey中匹配到的Queue上。
1.所有的队列(queue)都有一个标签,所有的消息也都带有一个标签(RouteKey),Exchange会将消息转发到与它相匹配的队列(queue)中,这里的匹配是模糊匹配。
3.这种模式需要RouteKey,且需要提前绑定Exchange和Queue。
4如果Exchange没有发现能够与RouteKey匹配的Queue,则会抛弃此消息。
5.“#”表示0个或若干个关键字,“*”表示一个关键字。


SpringBoot Rabbit 整合_第3张图片
image.png

在启动类里添加如下代码,首先会Exchange和队列(queue)匹配,如消息发送者amqpTemplate.convertAndSend("exchange","topic.A", topicA),topic.A可以匹配到两个queue,一个是bindingtopicMessageA,另一个是bindingtopicMessageB。因为topic.A可以匹配到topic.A和topic.#。

//===============以下是验证topic Exchange的队列==========
    @Bean
    public Queue topicMessageA() {
        return new Queue("topic.A");
    }
    
    @Bean
    public Queue topicMessageB() {
        return new Queue("topic.B");
    }
    
    @Bean
    TopicExchange exchange() {
        return new TopicExchange("exchange");
    }
    
    @Bean
    Binding bindingtopicMessageA(Queue topicMessageA,TopicExchange exchange) {
        return BindingBuilder.bind(topicMessageA).to(exchange).with("topic.A");
    }
    
    @Bean
    Binding bindingtopicMessageB(Queue topicMessageB,TopicExchange exchange) {
        return BindingBuilder.bind(topicMessageB).to(exchange).with("topic.#");//*表示一个词,#表示零个或多个词
    }

在topic文件夹下穿件topicsend类来发送消息,代码如下:

package SpringBoot.rabbitMq.rabbtt.topic;

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

@Component
public class TopicSend {
    private static final Logger LOGGER = LoggerFactory.getLogger(TopicSend.class);
    
    @Autowired
    private AmqpTemplate amqpTemplate;
    
    public void send() {
        String topicA = "*******yangnima******";
        LOGGER.info(topicA);
        amqpTemplate.convertAndSend("exchange","topic.A", topicA);
        
        String topicB = "******wanguniang*****";
        LOGGER.info(topicB);
        amqpTemplate.convertAndSend("exchange","topic.B", topicB);
    }

}

在topic文件下创建两个接收类TopicReceiverA 和TopicReceiverB去接收TopicSend的消息,TopicReceiverB代码如下:

package SpringBoot.rabbitMq.rabbtt.topic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "topic.B")
public class TopicReceiverB {
    private static final Logger LOGGER = LoggerFactory.getLogger(TopicReceiverB.class);
    
    @RabbitHandler
    public void receiver(String topicB) {
        LOGGER.info("*****TopicReceiverB*****"+topicB);
    }

}

TopicReceiverA代码如下:

package SpringBoot.rabbitMq.rabbtt.topic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "topic.A")
public class TopicReceiverA {
    private static final Logger LOGGER = LoggerFactory.getLogger(TopicReceiverA.class);
    
    @RabbitHandler
    public void receiver(String topicA) {
        LOGGER.info("*****TopicReceiverA*****" + topicA);
    }

}

在RabbitTestController类中添加如下代码,


@Autowired
private TopicSend topicSend;

@GetMapping("/topic")
    public void testTopic() {
        topicSend.send();
    }

验证,访问http://localhost:8080/rabbit/topic控制台输出:

*******yangnima******
******wanguniang*****
*****TopicReceiverB************yangnima******
*****TopicReceiverA************yangnima******
*****TopicReceiverB***********wanguniang*****

三.SpringBoot整合RabbitMQ(Fanout模式)

任何发送到Fanout Exchange上的消息都会转发到与Exchange绑定(binding)的queue上。
1.可以理解为路由表的模式
2.这种模式不需要RouteKey
3.这种模式需要提前将Exchange与Queue进行绑定,一个Exchange可以绑定多个Queue,一个Queue可以同多个Exchange进行绑定。
4.如果接受到消息的Exchange没有与任何Queue绑定,则消息会被抛弃。


SpringBoot Rabbit 整合_第4张图片
image.png

在启动类RabbttApplication中添加如下代码:

//===============以下是验证Fanout Exchange的队列==========
    @Bean
    public Queue fanoutMessageA() {
        return new Queue("fanout.A");
    }

    @Bean
    public Queue fanoutMessageB() {
        return new Queue("fanout.B");
    }

    @Bean
    public Queue fanoutMessageC() {
        return new Queue("fanout.C");
    }
    
    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }
    
 
    @Bean
    Binding bindingExchangeA(Queue fanoutMessageA,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutMessageA).to(fanoutExchange);
    }
    
    @Bean
    Binding bindingExchangeB(Queue fanoutMessageB,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutMessageB).to(fanoutExchange);
    }
    
    @Bean
    Binding bindingExchangeC(Queue fanoutMessageC,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutMessageC).to(fanoutExchange);
    }

在fanout文件夹下创建FanoutSender类去发送消息,代码如下:

package SpringBoot.rabbitMq.rabbtt.fanout;

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

@Component
public class FanoutSender {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(FanoutSender.class);
    
    @Autowired
    private AmqpTemplate amqpTemplate;
    
    public void send() {
        String msg = "*****hello yangnima!! *******";
        LOGGER.info("*****FanoutSender******" + msg);
        amqpTemplate.convertAndSend("fanoutExchange","", msg);
    }

}

在fanout下面创建三个消息接收类去接受消息,FanoutReceiverA、FanoutReceiverB、FanoutReceiverC,FanoutReceiverA类的代码如下:

package SpringBoot.rabbitMq.rabbtt.fanout;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "fanout.A")
public class FanoutReceiverA {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(FanoutReceiverA.class);
    
    @RabbitHandler
    public void receiver(String msg) {
        LOGGER.info("*******FanoutReceiverA*******" + msg);
    }

}

FanoutReceiverB类的代码如下:

package SpringBoot.rabbitMq.rabbtt.fanout;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "fanout.B")
public class FanoutReceiverB {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(FanoutReceiverB.class);
    
    @RabbitHandler
    public void receiver(String msg) {
        LOGGER.info("*******FanoutReceiverB*******" + msg);
    }

}

FanoutReceiverC类的代码如下:

package SpringBoot.rabbitMq.rabbtt.fanout;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "fanout.C")
public class FanoutReceiverC {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(FanoutReceiverC.class);
    
    @RabbitHandler
    public void receiver(String msg) {
        LOGGER.info("*******FanoutReceiverC*******" + msg);
    }

}

在RabbitTestController类中添加如下代码,


@Autowired
private FanoutSender fanoutSender;

@GetMapping("/fanout")
    public void testFanout() {
        fanoutSender.send();
    }

验证,访问http://localhost:8080/rabbit/fanout,控制台输出如下:

*****FanoutSender***********hello yangnima!! *******
*******FanoutReceiverA************hello yangnima!! *******
*******FanoutReceiverC************hello yangnima!! *******
*****TopicReceiverB**********hello yangnima!! *******
*******FanoutReceiverB************hello yangnima!! *******
*****TopicReceiverA**********hello yangnima!! *******

你可能感兴趣的:(SpringBoot Rabbit 整合)