ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

RabbitMQ SpringAMQP

2021-02-17 10:03:21  阅读:124  来源: 互联网

标签:SpringAMQP amqp 队列 springframework RabbitMQ import org public


SpringAMQP是SpringBoot操作 RabbitMQ的包

1.创建一个空项目并创建一个生产者的Module

2.导入依赖

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
    <version>2.4.2</version>
</dependency>

 3.创建一个application.yml文件:

spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
    virtual-host: /
server:
  port: 8081

4.创建一个Rabbit的配置类

package com.ckfuture.amqpsend.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;

/**
 * @author CKFuture
 * @since 1.0.0
 */
@Configuration
public class RabbitMQConfig {

    //声明队列
    @Bean
    public Queue queue(){
        return new Queue("amqp_queue");
    }
    //声明交换机
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("amqp_exchange");
    }
    //绑定交换机和队列
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(queue()).to(topicExchange()).with("*.amqp.#");
    }

}

5.编写生产者代码:

package com.ckfuture.amqpsend;

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 AmqpSendApplicationTests {

    @Test
    void contextLoads() {
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Test
    public void testSend(){
        String message="Hello";
        System.out.println("发送消息:"+message);
        //发送消息 (交互机,路由,消息)
        rabbitTemplate.convertAndSend("amqp_exchange","test.amqp",message);

    }

}

6.创建一个消费者RecvInit.java编写发送代码:

package com.ckfuture.amqprecv;

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

@Component
//监听队列
@RabbitListener(queues="amqp_queue")
public class RecvInit {

    //监听之后获取消息的处理方法
    @RabbitHandler
    public void testRecv(String message){
        System.out.println("接收到消息"+message);
    }
}

7.运行生产者

 

 在管理控制台查看:

 

 8.运行消费者程序

 

 在管理控制台查看:

 

 

 

 

总结:

① 创建配置类,声明 队列、交互机、绑定交互机和队列,在绑定的时候要使用通配符绑定路由key

② 正常发送消息是通过RabbitTemplate.convertAndSed()方法发送消息。

③ 消费者(接收者)只需要通过@RabbitListener(queues="队列名")监听队列和@RabbitHandler监听之后获取消息的处理方法

整个项目结构:

RabbitMQ 参考:https://www.bilibili.com/video/BV1Ky4y1D7Yv?p=2&spm_id_from=pageDriver

标签:SpringAMQP,amqp,队列,springframework,RabbitMQ,import,org,public
来源: https://www.cnblogs.com/ckfuture/p/14407777.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有