欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

RabbitMQ 整合 Spring

程序员文章站 2022-03-03 09:17:53
博文目录文章目录Spring BaseproducerconsumerSpring Basepom.xml

博文目录


pom.xml

<?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>
    <groupId>org.mrathena.middle.ware</groupId>
    <artifactId>rabbit.mq.spring</artifactId>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>2.3.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<Pattern>[%date{yyyyMMdd.HHmmss.SSS}][%-5level][%thread][%class{1}.%method:%line] %message%n</Pattern>
		</encoder>
	</appender>
	<root level="info">
		<appender-ref ref="console" />
	</root>
</configuration>

producer

rabbitmq.properties

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

spring-rabbitmq-producer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <rabbit:connection-factory id="connectionFactory"
                               host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"/>
    <!--定义管理交换机、队列-->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--定义持久化队列,不存在则自动创建;不绑定到交换机则绑定到默认交换机. 默认交换机类型为direct,名字为:"",路由键为队列的名称-->
    <!--
        id:bean的名称
        name:queue的名称
        auto-declare:自动创建
        auto-delete:自动删除。 最后一个消费者和该队列断开连接后,自动删除队列
        durable:是否持久化
    -->

    <!-- 简单模式 -->
    <!-- ignore-declaration-exceptions: 忽略队列已存在且声明时属性不一致的报错 -->
    <rabbit:queue id="queue-hello-world" name="queue-hello-world" auto-declare="true" durable="false" ignore-declaration-exceptions="true"/>

    <!--工作队列模式-->
    <rabbit:queue id="queue-work-queue" name="queue-work-queue" auto-declare="true" durable="false"/>

    <!-- 发布订阅模式 -->
    <!-- fanout: 广播, 所有绑定到交换机的队列都能收到消息 -->
    <rabbit:queue id="queue-fanout-1" name="queue-fanout-1" auto-declare="true" durable="false"/>
    <rabbit:queue id="queue-fanout-2" name="queue-fanout-2" auto-declare="true" durable="false"/>
    <rabbit:fanout-exchange id="exchange-fanout" name="exchange-fanout" auto-declare="true" durable="false">
        <rabbit:bindings>
            <rabbit:binding queue="queue-fanout-1"/>
            <rabbit:binding queue="queue-fanout-2"/>
        </rabbit:bindings>
    </rabbit:fanout-exchange>

    <!-- 路由模式 -->
    <!-- direct: 定向, 绑定到交换机且指定的队列才能收到消息 -->
    <rabbit:queue id="queue-direct" name="queue-direct" auto-declare="true" durable="false"/>
    <rabbit:direct-exchange id="exchange-direct" name="exchange-direct">
        <rabbit:bindings>
            <!--key: 路由键-->
            <rabbit:binding queue="queue-direct" key="direct"/>
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <!-- 主题模式 -->
    <!-- topic: 主题, 绑定到交换机且满足条件的队列才能收到消息, 路由键:*匹配一个单词,#匹配多个单词 -->
    <rabbit:queue id="queue-topic-1" name="queue-topic-1" auto-declare="true" durable="false"/>
    <rabbit:queue id="queue-topic-2" name="queue-topic-2" auto-declare="true" durable="false"/>
    <rabbit:queue id="queue-topic-3" name="queue-topic-3" auto-declare="true" durable="false"/>
    <rabbit:topic-exchange id="exchange-topic" name="exchange-topic" auto-declare="true">
        <rabbit:bindings>
            <rabbit:binding queue="queue-topic-1" pattern="topic.*"/>
            <rabbit:binding queue="queue-topic-2" pattern="topic.#"/>
            <rabbit:binding queue="queue-topic-3" pattern="topic.three.*"/>
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!-- rabbitTemplate-->
    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>

com.mrathena.base.ProducerTest

package com.mrathena;

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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class ProducerBaseTest {

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Test
	public void testHelloWorld() {
		rabbitTemplate.convertAndSend("queue-hello-world", "hello world");
	}

	@Test
	public void testWorkQueues() {
		rabbitTemplate.convertAndSend("queue-work-queue", "work queue");
	}

	@Test
	public void testFanout() {
		rabbitTemplate.convertAndSend("exchange-fanout", "", "fanout");
	}

	@Test
	public void testDirect() {
		rabbitTemplate.convertAndSend("exchange-direct", "direct", "direct");
	}

	@Test
	public void testTopics() {
		rabbitTemplate.convertAndSend("exchange-topic", "topic.a", "topic topic.a");
		rabbitTemplate.convertAndSend("exchange-topic", "topic.a.b", "topic topic.a.b");
		rabbitTemplate.convertAndSend("exchange-topic", "topic.three.a.b", "topic topic.three.a.b");
	}
}

consumer

rabbitmq.properties

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

spring-rabbitmq-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <rabbit:connection-factory id="connectionFactory"
                               host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"/>

    <!--基础部分-->
    <bean id="queueHelloWorldListener" class="com.mrathena.rabbit.mq.listener.base.QueueHelloWorldListener"/>
    <bean id="queueWorkQueueOneListener" class="com.mrathena.rabbit.mq.listener.base.QueueWorkQueueOneListener"/>
    <bean id="queueWorkQueueTwoListener" class="com.mrathena.rabbit.mq.listener.base.QueueWorkQueueTwoListener"/>
    <bean id="queueFanoutListener" class="com.mrathena.rabbit.mq.listener.base.QueueFanoutListener"/>
    <bean id="queueDirectListener" class="com.mrathena.rabbit.mq.listener.base.QueueDirectListener"/>
    <bean id="queueTopicOneListener" class="com.mrathena.rabbit.mq.listener.base.QueueTopicOneListener"/>
    <bean id="queueTopicTwoListener" class="com.mrathena.rabbit.mq.listener.base.QueueTopicTwoListener"/>
    <bean id="queueTopicThreeListener" class="com.mrathena.rabbit.mq.listener.base.QueueTopicThreeListener"/>
    <rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
        <rabbit:listener ref="queueHelloWorldListener" queue-names="queue-hello-world"/>
        <rabbit:listener ref="queueWorkQueueOneListener" queue-names="queue-work-queue"/>
        <rabbit:listener ref="queueWorkQueueTwoListener" queue-names="queue-work-queue"/>
        <rabbit:listener ref="queueFanoutListener" queue-names="queue-fanout-1,queue-fanout-2"/>
        <rabbit:listener ref="queueDirectListener" queue-names="queue-direct"/>
        <rabbit:listener ref="queueTopicOneListener" queue-names="queue-topic-1"/>
        <rabbit:listener ref="queueTopicTwoListener" queue-names="queue-topic-2"/>
        <rabbit:listener ref="queueTopicThreeListener" queue-names="queue-topic-3"/>
    </rabbit:listener-container>
</beans>

com.mrathena.rabbit.mq.listener.base.QueueHelloWorldListener

package com.mrathena.rabbit.mq.listener.base;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

@Slf4j
public class QueueHelloWorldListener implements MessageListener {
	@Override
	public void onMessage(Message message) {
		log.info("{}", new String(message.getBody()));
	}
}

还有一些Listener都是一样的内容, 只是类名不同, 通过日志打印的类名查看区别
com.mrathena.rabbit.mq.listener.base.QueueWorkQueueOneListener
com.mrathena.rabbit.mq.listener.base.QueueWorkQueueTwoListener
com.mrathena.rabbit.mq.listener.base.QueueDirectListener
com.mrathena.rabbit.mq.listener.base.QueueFanoutListener
com.mrathena.rabbit.mq.listener.base.QueueTopicOneListener
com.mrathena.rabbit.mq.listener.base.QueueTopicTwoListener
com.mrathena.rabbit.mq.listener.base.QueueTopicThreeListener

本文地址:https://blog.csdn.net/mrathena/article/details/110504553

相关标签: rabbitmq