Spring集成RabbitMQ
官网:https://spring.io/projects/spring-amqp
创建聚合项目
父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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><packaging>pom</packaging><modules><module>provider</module><module>consumer</module></modules><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>cn.yanghuisen</groupId><artifactId>spring-rabbitmq</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-rabbitmq</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><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><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit-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>
生产者
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"><parent><artifactId>spring-rabbitmq</artifactId><groupId>cn.yanghuisen</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>provider</artifactId><name>provider</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies></dependencies></project>
package cn.yanghuisen;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/
@SpringBootApplication
public class App
{public static void main( String[] args ){SpringApplication.run(App.class);}
}
package cn.yanghuisen;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;/*** @author admin* @version 1.0* @date 2020/5/29 22:28* @Description TODO*/
@Configuration
public class RabbitMQConfig {/*** 申明队列* @return*/@Beanpublic Queue queue(){return new Queue("topic");}/*** 申明交换机* @return*/@Beanpublic TopicExchange topicExchange(){return new TopicExchange("topicExchange");}/*** 将队列绑定到交换机上* @return*/@Beanpublic Binding binding(){return BindingBuilder.bind(queue()).to(topicExchange()).with("*.msg.#");}}
package cn.yanghuisen;import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;import javax.annotation.Resource;/*** @author admin* @version 1.0* @date 2020/5/29 22:33* @Description TODO*/
@Component
public class Send {@Resourceprivate RabbitTemplate rabbitTemplate;public void send(){String message = "Hello World";/*1、交换机参数2、路由key3、消息内容*/rabbitTemplate.convertAndSend("topicExchange","topic.msg",message);System.out.println("发送消息:"+message);}
}
消费者
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"><parent><artifactId>spring-rabbitmq</artifactId><groupId>cn.yanghuisen</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>consumer</artifactId><name>consumer</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies></dependencies><build></build>
</project>
package cn.yanghuisen;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/
@SpringBootApplication
public class App
{public static void main( String[] args ){SpringApplication.run(App.class);}
}
package cn.yanghuisen;import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;/*** @author admin* @version 1.0* @date 2020/5/29 22:36* @Description TODO*/
@Component
@RabbitListener(queues = "topic") // 监听队列
public class Consumer {// 接收消息的处理方法@RabbitHandlerpublic void recv(String message){System.out.println("接受消息:"+message);}
}
测试
package cn.yanghuisen;import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;/*** @author admin* @version 1.0* @date 2020/5/29 22:54* @Description TODO*/
@SpringBootTest
public class TestSend {@Resourceprivate Send send;@Testpublic void testSend(){send.send();}}