温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springBoot整合rabbitmq测试常用模型有哪些

发布时间:2022-01-11 14:11:12 来源:亿速云 阅读:168 作者:柒染 栏目:开发技术

这篇文章跟大家分析一下“springBoot整合rabbitmq测试常用模型有哪些”。内容详细易懂,对“springBoot整合rabbitmq测试常用模型有哪些”感兴趣的朋友可以跟着小编的思路慢慢深入来阅读一下,希望阅读后能够对大家有所帮助。下面跟着小编一起深入学习“springBoot整合rabbitmq测试常用模型有哪些”的知识吧。

在企业开发中,我们更多的是使用spring框架来整合其它技术,springboot更是方便的提供了各种starter来快速添加依赖,完成整合,开箱即用。

1.添加依赖

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

2.编写配置

配置信息包括ip,端口,虚拟主机,用户名和密码,和原生java代码所需的配置信息一致。

spring:
  application:
    name: spirngboot-rabbitmq
  rabbitmq:
    host: 192.168.20.128
    port: 5672
    virtual-host: /vh
    username: wuwl
    password: 123456

3.编写并测试

下面主要针对前五种常用模型,在spirngboot框架的基础上整合rabbitmq并进行测试使用。

springBoot整合rabbitmq测试常用模型有哪些

(1) Hello World模型

这是一种简单的直连模型,生产者将消息直接发送至消息队列,消费者绑定消息队列后直接获取,一对一。
spring-boot-starter-amqp为我们提供了一个org.springframework.amqp.rabbit.core.RabbitTemplate类来方便我们使用rabbitmq,自动注入即可。

生产者测试类:

@SpringBootTest(classes = RabbitmqDemoApplication.class)
@RunWith(SpringRunner.class)
public class RabbitmqDemoApplicationTests {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
    public void testHelloQueues(){
            rabbitTemplate.convertAndSend("hello","hello world");
    }
}

生产者向名为hello的队列发送消息,但是,在没有消费者的情况下,生产者没有任何意义。另外,convertAndSend方法的第一个参数并不是消息队列的意思,而是routingKey,我们根据源码找到最初定义的接口可以看到以下内容:

/**
	 * Convert a Java object to an Amqp {@link Message} and send it to a default exchange
	 * with a specific routing key.
	 *
	 * @param routingKey the routing key
	 * @param message a message to send
	 * @throws AmqpException if there is a problem
	 */
	void convertAndSend(String routingKey, Object message) throws AmqpException;

第二个参数为Object类型,也就是说可以传递任意类型的对象,该方法将对象转换成一个Amqp消息并发送到一个默认的交换机,并且routingKey为第一个参数的内容,没有提到消息队列的信息,但我们可以分析到,这里的routingKeyqueues应该是同名的。

消费者类:

@Component
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloQueuesConsumer {
    @RabbitHandler
    public void consume(String msg){
        System.out.println("消费消息:" + msg + " " + System.currentTimeMillis());
    }
}

上面的代码等同于:

@Component
public class HelloQueuesConsumer {
    @RabbitListener(queuesToDeclare = @Queue("hello"))
    public void consume(String msg){
        System.out.println("消费消息:" + msg + " " + System.currentTimeMillis());
    }
}
@RabbitListener 可以标注在类上面,需配合 @RabbitHandler 注解一起使用
@RabbitListener 标注在类上面表示当有收到消息的时候,就交给 @RabbitHandler 的方法处理,具体使用哪个方法处理,根据 MessageConverter 转换后的参数类型

直接启动测试方法,也就是生产者,可以看到:

springBoot整合rabbitmq测试常用模型有哪些

消费者有接收到消息队列中的信息并打印。

(2) work queues模型

生产者测试方法,类与第一个模型一致

@Test
public void testWorkQueues(){
    for (int i = 0; i < 20; i++) {
        rabbitTemplate.convertAndSend("work","work index " + i);
    }
}

消费者类:

@Component
public class WorkQueuesConsumer {
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void consume1(String msg){
        System.out.println("consumer1消费消息:" + msg);
    }
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void consume2(String msg){
        System.out.println("consumer2消费消息:" + msg);
    }
}

启动生产者测试方法:

springBoot整合rabbitmq测试常用模型有哪些

消费者一与消费者二均匀分配了队列中的消息任务,即使两者执行效率不一致,也同样是均匀分配。

(3) Publish/Subscribe模型

生产者测试方法:

for (int i = 0; i < 20; i++) {
    rabbitTemplate.convertAndSend("amq.fanout","","fanout msg " + i);
}

消费者类:

@Component
public class FanoutQueuesConsumer {
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(
                            value = "amq.fanout",
                            type = "fanout"))})
    public void consume1(String msg) {
        System.out.println("consumer1消费消息:" + msg);
    }
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(
                            value = "amq.fanout",
                            type = "fanout"))})
    public void consume2(String msg) {
        System.out.println("consumer2消费消息:" + msg);
    }
}

注意此处的交换机信息

启动生产者测试方法:

springBoot整合rabbitmq测试常用模型有哪些

此处只粘贴了部分打印信息,两个消费者获得了相同的消息,生产者将消息发送至交换机,由交换机发送至已注册到交换机的所有临时消息队列,进而消费者获取队列中的消息。

(4) Routing模型

生产者测试方法:

@Test
public void testDirectQueues(){
    rabbitTemplate.convertAndSend("amq.direct","info","routingKey is info");
    rabbitTemplate.convertAndSend("amq.direct","warn","routingKey is warn");
    rabbitTemplate.convertAndSend("amq.direct","error","routingKey is error");
}

routing也成为fanout模型,对应的交换机类型为direct

消费者类:

@Component
public class DirectQueuesConsumer {
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(
                            value = "amq.direct",
                            type = "direct"),
                    key = {"info", "warn", "error"})})
    public void consume1(String msg) {
        System.out.println("consumer1消费消息:" + msg);
    }

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(
                            value = "amq.direct",
                            type = "direct"),
                    key = "error")})
    public void consume2(String msg) {
        System.out.println("consumer2消费消息:" + msg);
    }
}

启动生产者测试类:
springBoot整合rabbitmq测试常用模型有哪些消费者一配置了三种类型的routingKey,所以三种类型的消息都能够接收到,消费者二只能接受到error类型的消息。

(5) Topic模型

生产者测试方法:

@Test
public void testTopicQueues(){
    rabbitTemplate.convertAndSend("amq.topic","file.info","routingKey is info");
    rabbitTemplate.convertAndSend("amq.topic","file.warn","routingKey is warn");
    rabbitTemplate.convertAndSend("amq.topic","file.error","routingKey is error");
}

消费者类:

@Component
public class TopicQueuesConsumer {
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(
                            value = "amq.topic",
                            type = "topic"),
                    key = {"#"})})
    public void consume1(String msg) {
        System.out.println("consumer1消费消息:" + msg);
    }

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(
                            value = "amq.topic",
                            type = "topic"),
                    key = "*.error")})
    public void consume2(String msg) {
        System.out.println("consumer2消费消息:" + msg);
    }
}

启动生产者测试方法:

springBoot整合rabbitmq测试常用模型有哪些

消费者一配置的routingKey#,可以接受任意类型的消息,*好代表一个单词,消费者二可以接受任意单词加上.errorroutingKey的消息。

关于springBoot整合rabbitmq测试常用模型有哪些就分享到这里啦,希望上述内容能够让大家有所提升。如果想要学习更多知识,请大家多多留意小编的更新。谢谢大家关注一下亿速云网站!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI