Spring Boot 集成RabbitMQ

RabbitMQ is an open source multi-protocol messaging broker.

前言

参照官方Messaging with RabbitMQ,记录在实战中的一些坑。

搭建RabbitMQ服务

本文使用Docker搭建MQ服务。Docker部署服务,快捷、方便。

安装镜像

参照docker 安装ubuntu安装镜像

这里写图片描述

这里写图片描述

启动镜像

docker run -d -p 15672:15672 -p 5672:5672 rabbitmq:3-management

这里写图片描述

这里要映射2个端口:15672是Web管理界面的端口;5672是MQ访问的端口。

Web管理界面

http://192.168.99.100:15672/

guest/guest

这里写图片描述

这里写图片描述

RabbitMQ服务部署好了。。。

集成

环境

IntelliJ IDEA 2016.3.4

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
        <java.version>1.8</java.version>

 
 
  • 1
  • 2

maven依赖

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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

代码实现

配置

这里写图片描述

AmqpInitConfig

@Configuration
@ConditionalOnProperty(prefix = "spring.rabbitmq",name = "enable", matchIfMissing = false)
public class AmqpInitConfig {

    final static String queueName="spring.boot";

    @Bean
    public Queue queue(){
        return new Queue(queueName,false);
    }

    @Bean
    public TopicExchange exchange(){
        return new TopicExchange("spring.boot.exchange");
    }

    @Bean
    public Binding binding(TopicExchange exchange,Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(queueName+".key");
    }

    @Bean
    public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter){
        SimpleMessageListenerContainer container=new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setMessageListener(listenerAdapter);
        container.addQueueNames(queueName);
        return container;
    }

    @Bean
    public MessageListenerAdapter listenerAdapter(Receiver receiver){
        return new MessageListenerAdapter(receiver,"receiveMessage");
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

接受消息

Receiver

@Component
public class Receiver {
    CountDownLatch latch = new CountDownLatch(1);

    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
        latch.countDown();
    }

    public CountDownLatch getLatch() {
        return latch;
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

发送消息

Runner

@Component
public class Runner implements CommandLineRunner {
    private final RabbitTemplate rabbitTemplate;
    private final Receiver receiver;
    private final ConfigurableApplicationContext context;

    public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,
                  ConfigurableApplicationContext context) {
        this.receiver = receiver;
        this.rabbitTemplate = rabbitTemplate;
        this.context = context;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        rabbitTemplate.convertAndSend("spring.boot.exchange","spring.boot.key", "Hello from RabbitMQ!");
        receiver.getLatch().await(  10000, TimeUnit.MILLISECONDS);
        context.close();
    }

}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

启动

AmqpApplication

@SpringBootApplication
@ComponentScan(basePackages = "com.wxs.amqp")
public class AmqpApplication {
    public static void main(String[] args) {
        SpringApplication.run(AmqpApplication.class,args);
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这里写图片描述

踩过的坑

坑一

发送消息需要制定exchange,如果不指定,不会发送消息。

这里写图片描述

参考

Messaging with RabbitMQ 
How to use this image Running the daemon

Spring Boot 集成 RabbitMQ 是一种常见的实践,用于在基于 Spring Boot 的应用程序中集成消息队列系统,RabbitMQ 是一个开源的消息中间件,常用于分布式系统的解耦和异步通信。 下面是简单的集成步骤: 1. 添加依赖:首先,你需要在你的 Maven 或 Gradle 项目中添加 RabbitMQ 客户端的依赖。对于 Maven,可以在 `pom.xml` 中添加: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 对于 Gradle,添加到 `build.gradle` 文件的 `dependencies` 节点: ```groovy implementation 'org.springframework.boot:spring-boot-starter-amqp' ``` 2. 配置 RabbitMQ:在 application.properties 或者 application.yml 文件中配置 RabbitMQ 的连接信息,如主机名、端口号、用户名和密码等: ```properties spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=admin spring.rabbitmq.password=password ``` 3. 创建消息生产者:使用 Spring 的 `RabbitTemplate` 类创建发送消息的方法,通常在服务中定义: ```java @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(String message) { rabbitTemplate.convertAndSend("queue-name", message); } ``` 4. 创建消息消费者:创建监听特定队列的消费者,可以是传统的 Java 接收器,也可以使用注解驱动(@RabbitListener)的方式: ```java @RabbitListener(queues = "queue-name") public void handleMessage(String message) { System.out.println("Received message: " + message); } ``` 5. 启动应用:运行 Spring Boot 应用,Spring Boot 自动会初始化 RabbitMQ 连接并注册你的消费者。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值