Springboot整合RabbitMQ

1.引入依赖

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.配置RabbitMQ

spring:
  application:
    name: springboot_rabbitmq
  rabbitmq:
    host: 192.168.121.200
    port: 5672
    username: guest
    password: guest
    virtual-host: /zhbj
    listener:
      simple:
        acknowledge-mode: manual

3.消息生产者

package com.demo;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class RabbitmqSpringbootApplicationTests {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    void helloWorldModel() {
        rabbitTemplate.convertAndSend("hello","hello world");
    }

    @Test
    void workQueuesModel(){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work","work模型"+i);
        }
    }

    @Test
    void publishSubscribeModel(){
        rabbitTemplate.convertAndSend("logs","","Fanout的模型发送的消息");
    }

    @Test
    void routingModel(){
        rabbitTemplate.convertAndSend("directs","error","发送error的key的路由信息");
    }

    @Test
    void topicsModel(){
        rabbitTemplate.convertAndSend("topics","order","order 路由信息");
    }

}

4.消息消费者

//helloWorldModel
@Component
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public class HelloCustomer {

    @RabbitHandler
    public void receive(String message){
        System.out.println("message = " + message);
    }
}

//workQueuesModel
@Component
public class WorkCustomer {

    // 第一个消费者
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive1(Message message, Channel channel) throws IOException, InterruptedException {
        System.out.println("message1 = " + new String(message.getBody()));
        channel.basicQos(1);
        TimeUnit.SECONDS.sleep(3);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    // 第二个消费者
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive2(Message message, Channel channel) throws IOException {
        System.out.println("message2 = " + new String(message.getBody()));
        channel.basicQos(1);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}

//publishSubscribeModel
@Component
public class FanoutCustomer {

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,  //创建临时队列
                    exchange = @Exchange(value = "logs",type = "fanout")    //绑定交换机
            )
    })
    public void receive1(String message){
        System.out.println("message1 = "+ message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,  //创建临时队列
                    exchange = @Exchange(value = "logs",type = "fanout")    //绑定交换机
            )
    })
    public void receive2(String message){
        System.out.println("message2 = "+ message);
    }

}

//routingModel
@Component
public class RouteCustomer {

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,  //创建临时队列
                    exchange = @Exchange(value = "directs",type = "direct"), //指定交换机名称和类型
                    key = {"info","error","warn"}
            )
    })
    public void receive1(String message){
        System.out.println("message1 = "+message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,  //创建临时队列
                    exchange = @Exchange(value = "directs",type = "direct"), //指定交换机名称和类型
                    key = {"error"}
            )
    })
    public void receive2(String message){
        System.out.println("message2 = "+message);
    }
}

//topicsModel
@Component
public class topicCustomer {

    @RabbitListener(bindings = {
        @QueueBinding(
                value = @Queue,
                exchange = @Exchange(type = "topic",name = "topics"),
                key = {"user.save","user.*"}
        )
    })
    public void reveive1(String message){
        System.out.println("message1 = "+message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(type = "topic",name = "topics"),
                    key = {"order.#","produce.#","user.*"}
            )
    })
    public void reveive2(String message){
        System.out.println("message2 = "+message);
    }
}
posted @ 2020-11-21 22:36  xl4ng  阅读(174)  评论(0编辑  收藏  举报