欢迎访问优讯网!
您当前的位置:首页 > 爱编程

SpringBoot整合RabbitMQ

时间:2020-01-07 08:06:31  来源:优讯网  作者:小卡司  浏览次数:

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 教程、技术栈、示例代码

来顶一下
返回首页
返回首页

原文链接:https://my.oschina.net/u/4070155/blog/3154055


推荐资讯
如何下载旧版centos iso镜像 如何下载迷你mini版的centos镜像
如何下载旧版centos i
计算机的正确使用姿势 电脑痴如何正确的使用电脑
计算机的正确使用姿势
好用的后台管理的前端框架模版H-ui H-ui框架模版分享
好用的后台管理的前端
微信电脑多开方法 无需辅助电脑版微信双开方法分享
微信电脑多开方法 无
相关文章
栏目更新
栏目热门