菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
40
0

SpringBoot整合RabbitMQ

原创
05/13 14:22
阅读数 794

SpringBoot整合RabbitMQ

SpringBoot框架已经提供了RabbitMQ的使用jar包,开发人员在使用RabbitMQ的时候只需要引用jar包简单的配置一下就可以使用RabbitMQ,这极大的简化了开发人员的开发成本,提升开发效率。

话不多说,直接上代码:

先在pom.xml文件添加依赖spring-boot-starter-amqp如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-boot-rabbitmq</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>springboot整合RabbitMQ的示例</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

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


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties文件中配置:

server.port=8085

spring.rabbitmq.host=host
spring.rabbitmq.port=5672
spring.rabbitmq.username=username
spring.rabbitmq.password=password
spring.rabbitmq.virtual-host=virtual-host

我们以topic模式为例,springboot提供了一种用bean的方式,在代码里配置绑定队列和交换机:

package com.example.topic;

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;

/**
 * create rabbitmq queue exchange and bind by routingKey
 */
@Configuration
public class TopicRabbitMQConfig {

      // 队列:queue.example.topic.new
    @Bean
    public Queue topicQueue() {
        return new Queue("queue.example.topic.new");
    }

      // 交换机:exchange.topic.example.new
    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange("exchange.topic.example.new");
    }

      // 绑定关系:routing.key.example.new
    @Bean
    Binding bindingTopicExchange() {
        return BindingBuilder
                .bind(topicQueue())
                .to(topicExchange())
                .with("routing.key.example.new");
    }

}

生产者:

package com.example.topic;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TopicProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessageByTopic() {
        String content = "This is a topic type of the RabbitMQ message example";
        this.rabbitTemplate.convertAndSend(
                "exchange.topic.example.new",
                "routing.key.example.new",
                content);
    }

}

消费者:


package com.example.topic;

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

@Component
@RabbitListener(queues = "queue.example.topic.new")
public class TopicConsumer {

    @RabbitHandler
    public void consumer(String message) {
        System.out.println(message);
    }

}

写一段测试代码测试一下,RabbitMQ的生产消费:

package com.example;

import com.example.direct.DirectProducer;
import com.example.fanout.FanoutProducer;
import com.example.simple.SimpleProducer;
import com.example.topic.TopicProducer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

    @Autowired
    private TopicProducer topicProducer;


    @Test
    public void topicProducerTest() {
        topicProducer.sendMessageByTopic();
    }

}

这样就能在SpringBoot中使用RabbitMQ了!另外三种模式:directfanouthead的代码我放在了github上,地址为:

Spring Boot 教程、技术栈、示例代码

联系我

关注后,回复“SpringBoot”,领取springboot、springcloud的大量资料。

扫描关注公众号:java之旅

发表评论

0/200
40 点赞
0 评论
收藏