springboot使用rabbitmq记录

716 阅读1分钟

1. 加入maven依赖

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

2. 配置rabbitmq

spring:
  rabbitmq:
    host: localhost
    port: 5672
    virtual-host: /users
    username: users
    password: 1234

3. 生产者测试类

注:单独调用生产者测试类,如果没有消费者监听,并不会生产消息

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class RabbitMqProviderTest {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    //点对点和工作队列模式,发送普通消息
    public void sendMsg() {
        for (int i=0; i<10; i++) {
            rabbitTemplate.convertAndSend("hello", i+" hello world");
        }
    }

    @Test
    //交换机模式/广播模式发送消息,引入交换机,路由为空
    public void sendExchangeMsg() {
        rabbitTemplate.convertAndSend("logs", "", "this is a log");
    }

    @Test
    //路由模式发送消息,相比交换机模式,发送路由key
    public void sendRoutingMsg() {
        rabbitTemplate.convertAndSend("directs", "info", "this is a info log");
        rabbitTemplate.convertAndSend("directs", "error", "this is a error log");
    }

    @Test
    //动态路由模式发送消息,相比路由模式,路由key可以是多个单词,通过分隔符.分割
    public void sendTopicMsg() {
        rabbitTemplate.convertAndSend("topics", "user.save", "topic test");
        rabbitTemplate.convertAndSend("topics", "user.dept.save", "topic test");
    }

}

4. 点对点和工作队列模式 消费者

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

@Component
public class HelloConsumer {

    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue("hello"))
    public void receive1(String msg) {
        System.out.println("msg1=" + msg);
    }

    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue("hello"))
    public void receive2(String msg) {
        System.out.println("msg2=" + msg);
    }
}

5. 交换机模式/广播模式 消费者

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

@Component
public class ExchangeConsumer {

    @RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(value = "logs", type = "fanout"))
    })
    public void exchangeMsg1(String msg) {
        System.out.println("exchangeMsg1=" + msg);
    }

    @RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(value = "logs", type = "fanout"))
    })
    public void exchangeMsg2(String msg) {
        System.out.println("exchangeMsg2=" + msg);
    }
}

6. 路由模式 消费者

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

@Component
public class RoutingConsumer {

    @RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(value = "directs"),
            key = {"info", "error"})
    })
    public void exchangeMsg1(String msg) {
        System.out.println("directMsg1=" + msg);
    }

    @RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(value = "directs"),
                            key = {"error"})
    })
    public void exchangeMsg2(String msg) {
        System.out.println("directMsg2=" + msg);
    }
}

7. 动态路由模式 消费者

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

@Component
public class TopicConsumer {

    @RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(value = "topics", type = "topic"),
            key = {"user.*"})
    })
    public void exchangeMsg1(String msg) {
        System.out.println("topics1=" + msg);
    }

    @RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(value = "topics", type = "topic"),
                            key = {"user.#"})
    })
    public void exchangeMsg2(String msg) {
        System.out.println("topics2=" + msg);
    }
}