当前位置: 首页 > article >正文

Spring集成RabbitMQ

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
     */
    @Bean
    public Queue queue(){
        return new Queue("topic");
    }


    /**
     * 申明交换机
     * @return
     */
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topicExchange");
    }

    /**
     * 将队列绑定到交换机上
     * @return
     */
    @Bean
    public 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 {
    @Resource
    private RabbitTemplate rabbitTemplate;

    public void send(){
        String message = "Hello World";
        /*
            1、交换机参数
            2、路由key
            3、消息内容
         */
        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 {


    // 接收消息的处理方法
    @RabbitHandler
    public 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 {

    @Resource
    private Send send;

    @Test
    public void testSend(){
        send.send();
    }

}


http://www.kler.cn/a/411664.html

相关文章:

  • 【spark-spring boot】学习笔记
  • IDEA某个Impl下的引入的文件红色
  • mp4视频流推送的学习
  • IDEA插件CamelCase,快速转变命名格式
  • 《硬件架构的艺术》笔记(八):消抖技术
  • css:转换
  • SQL优化笔记--explain看执行计划--主要还是看用了哪些索引,所以你是否失效--分库分表的注意事项(未完)
  • C#中面试的常见问题008
  • 列表代码思路
  • 前端技术选型之uniapp
  • MySQL中char和varchar的区别详解
  • JavaWeb——请求响应(5/8)-请求:日期参数json参数(使用场景及封装类型、接收方式、在 Postman 中传递、在服务端接收)
  • Spring框架使用xml方式配置ThreadPoolTaskExecutor线程池,并且自定义线程工厂
  • jdk17-LongAddr 类increment()方法注释说明
  • c++中的lambda表达式!
  • 【H2O2|全栈】JS进阶知识(十一)axios入门
  • ChatGPT如何辅助academic writing?
  • 学习路之linux--多php版本下指定php版本执行命令
  • 基于Java Springboot华为数码商城交易平台
  • 数据结构初阶---复杂度