SpringBoot整合RabbitMQ

一、生产者

1.1 pom

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.baizhi</groupId>
    <artifactId>rabbitmq_springboot</artifactId>
    <packaging>pom</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <modules>
        <module>rabbitmq-producer</module>
        <module>rabbitmq-consumer</module>
    </modules>
    <name>rabbitmq_springboot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.1.14.RELEASE</spring-boot.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-web</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
<!--            引入与rabbitmq集成依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.2 yaml

server:
  port: 44000

spring:
  application:
    name: rabbitmq-producer

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

1.3 主启动

package com.best;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author Jiang Akang
 * employeeId: BG435424
 * @date 2020/11/20
 **/
@SpringBootApplication
public class RabbitMqProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(RabbitMqProducerApplication.class, args);
    }
}

1.4 配置类

package com.best.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Jiang Akang
 * employeeId: BG435424
 * @date 2020/11/20
 **/
@Configuration
public class RabbitmqConfig {
    public static final String QUEUE_INFORMA_EMAIL = "queue_inform_email";

    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";

    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";

    public static final String ROUTINGKEY_EMAIL = "inform.#.email.#";

    public static final String ROUTINGKEY_SMS = "inform.#.sms.#";

    //声明交换机
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange EXCHANGE_TOPICS_INFORM() {
        //durable(true)持久化,mq重启之后交换机还在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    //声明QUEUE_INFORMA_EMAIL队列
    @Bean(QUEUE_INFORMA_EMAIL)
    public Queue QUEUE_INFORMA_EMAIL() {
        return new Queue(QUEUE_INFORMA_EMAIL);
    }

    //声明QUEUE_INFORM_SMS队列
    @Bean(QUEUE_INFORM_SMS)
    public Queue QUEUE_INFORM_SMS() {
        return new Queue(QUEUE_INFORM_SMS);
    }

    //@Qualifier根据名称注入bean
    //ROUTINGKEY_EMAIL队列绑定交换机,指定routingKey
    //.noargs绑定的时候不需要指定参数
    @Bean
    public Binding BINGDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORMA_EMAIL) Queue queue,
                                               @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
    }

    //ROUTINGKEY_SMS队列绑定交换机,指定routingKey
    @Bean
    public Binding BIDING_ROUTINGKEY_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
                                         @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
    }
}

1.5 测试类

package com.best;

import com.best.config.RabbitmqConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author Jiang Akang
 * employeeId: BG435424
 * @date 2020/11/20
 **/
@SpringBootTest
@RunWith(SpringRunner.class)
public class Producer {

    @Autowired
    RabbitTemplate rabbitTemplate;

    //使用rabbitTemplate发送消息
    @Test
    public void testSendEmail() {
        //匹配ROUTINGKEY_EMAIL路由键,发送消息到ROUTINGKEY_EMAIL队列
        String message = "send email message to ROUTINGKEY_EMAIL";
         rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM, "inform.email", message);
    }

    //使用rabbitTemplate发送消息
    @Test
    public void testSendEmail1() {
        //匹配ROUTINGKEY_SMS路由键,发送消息到ROUTINGKEY_SMS队列
        String message = "send email message to ROUTINGKEY_SMS";
         rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM, "inform.sms", message);
    }


}

二、消费者

1.1 yaml

server:
  port: 44001

spring:
  application:
    name: rabbitmq-consumer

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

1.2 主启动

package com.best;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author Jiang Akang
 * employeeId: BG435424
 * @date 2020/11/20
 **/
@SpringBootApplication
public class RabbitMqConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(RabbitMqConsumerApplication.class, args);
    }
}

1.3 配置类

package com.best.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Jiang Akang
 * employeeId: BG435424
 * @date 2020/11/20
 **/
@Configuration
public class RabbitmqConfig {

    public static final String QUEUE_INFORMA_EMAIL = "queue_inform_email";

    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";

    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";

    public static final String ROUTINGKEY_EMAIL = "inform.#.email.#";

    public static final String ROUTINGKEY_SMS = "inform.#.sms.#";

    //声明交换机
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange EXCHANGE_TOPICS_INFORM() {
        //durable(true)持久化,mq重启之后交换机还在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    //声明QUEUE_INFORMA_EMAIL队列
    @Bean(QUEUE_INFORMA_EMAIL)
    public Queue QUEUE_INFORMA_EMAIL() {
        return new Queue(QUEUE_INFORMA_EMAIL);
    }

    //声明QUEUE_INFORM_SMS队列
    @Bean(QUEUE_INFORM_SMS)
    public Queue QUEUE_INFORM_SMS() {
        return new Queue(QUEUE_INFORM_SMS);
    }

    //@Qualifier根据名称注入bean
    //ROUTINGKEY_EMAIL队列绑定交换机,指定routingKey
    //.noargs绑定的时候不需要指定参数
    @Bean
    public Binding BINGDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORMA_EMAIL) Queue queue,
                                               @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
    }

    //ROUTINGKEY_SMS队列绑定交换机,指定routingKey
    @Bean
    public Binding BIDING_ROUTINGKEY_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
                                         @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
    }
}

1.4 业务类

package com.best.rabbitmq;

import com.best.config.RabbitmqConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author Jiang Akang
 * employeeId: BG435424
 * @date 2020/11/20
 **/
@Component
public class ReceiveHandler {

    @RabbitListener(queues = {RabbitmqConfig.QUEUE_INFORMA_EMAIL})
    public void send_email(String msg, Message message, Channel channel) {
        System.out.println("receive message is: " + msg);
    }

}

三、测试

运行生产者,往里面发送几条消息

打开rabbitMq: http://localhost:15672/#/queues

可以看到消息的内容

启动消费者,消费者监听着QUEUE_INFORMA_EMAIL队列消息,如果有消息,就会接受到

源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值