项目集成RabbitMQ

文章目录

1.common-rabbitmq-starter

1.创建common-rabbitmq-starter
2.pom.xml
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>
    <parent>
        <groupId>com.sunxiansheng</groupId>
        <artifactId>sunrays-common</artifactId>
        <version>1.0.5</version>
    </parent>

    <version>1.0.5</version>

    <artifactId>common-rabbitmq-starter</artifactId>

    <dependencies>
        <!-- 工具包 -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-tool-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
        <!--AMQP依赖,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>

</project>
3.自动配置
1.RabbitMQAutoConfiguration.java
java 复制代码
package com.sunxiansheng.rabbitmq.config;

import org.springframework.context.annotation.Configuration;

/**
 * Description: RabbitMQ自动配置类
 *
 * @Author sun
 * @Create 2024/12/31 18:44
 * @Version 1.0
 */
@Configuration
public class RabbitMQAutoConfiguration {
}
2.spring.factories
java 复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunxiansheng.rabbitmq.config.RabbitMQAutoConfiguration

2.测试使用

1.创建common-rabbitmq-starter-demo
2.目录结构
3.pom.xml
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>
    <parent>
        <groupId>com.sunxiansheng</groupId>
        <artifactId>sunrays-demo</artifactId>
        <version>1.0.5</version>
    </parent>

    <artifactId>common-rabbitmq-starter-demo</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-rabbitmq-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
        <!-- 引入springboot-web模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 排除logging,防止日志冲突 -->
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 引入自己的日志 -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-log4j2-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
        <!-- spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <!-- 排除spring-boot-starter-logging,防止跟log4j2冲突 -->
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</project>
4.application.yml
yaml 复制代码
spring:
  # RabbitMQ 配置
  rabbitmq:
    # 服务器地址
    host: ip
    # 用户名
    username: guest
    # 密码
    password: guest
    # 虚拟主机
    virtual-host: /
    # 端口
    port: 6783
log:
  home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-demo/common-rabbitmq-starter-demo
  module: common-rabbitmq-starter-demo
5.TestConfig.java 配置交换机和队列
java 复制代码
package com.sunxiansheng.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description: 测试配置类
 *
 * @Author sun
 * @Create 2024/12/31 19:00
 * @Version 1.0
 */
@Configuration
public class TestConfig {

    /**
     * 创建一个fanout类型的交换机
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanout.exchange.test");
    }

    /**
     * 创建一个队列
     * @return
     */
    @Bean
    public Queue fanoutQueueTest() {
        return new Queue("fanout.queue.test");
    }

    /**
     * 交换机和队列绑定
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(fanoutQueueTest()).to(fanoutExchange());
    }
}
6.TestConfigConsumer.java 监听队列
java 复制代码
package com.sunxiansheng.rabbitmq.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * Description: 测试消费者
 *
 * @Author sun
 * @Create 2024/12/31 19:03
 * @Version 1.0
 */
@Component
@Slf4j
public class TestConfigConsumer {

    @RabbitListener(queues = "fanout.queue.test")
    public void receive(String message) {
        log.info("接收到的消息:{}", message);
    }
}
7.TestConfigPublisher.java 发布消息
java 复制代码
package com.sunxiansheng.rabbitmq.publisher;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * Description: 测试发布者
 *
 * @Author sun
 * @Create 2024/12/31 19:05
 * @Version 1.0
 */
@SpringBootTest
public class TestConfigPublisher {

    @Resource
    private AmqpTemplate amqpTemplate;

    @Test
    public void send() {
        amqpTemplate.convertAndSend("fanout.exchange.test", "", "hello rabbitmq");
    }
}
8.结果
相关推荐
RainbowSea13 小时前
6. RabbitMQ 死信队列的详细操作编写
java·消息队列·rabbitmq
RainbowSea13 小时前
5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
java·消息队列·rabbitmq
数据智能老司机14 小时前
CockroachDB权威指南——CockroachDB SQL
数据库·分布式·架构
数据智能老司机15 小时前
CockroachDB权威指南——开始使用
数据库·分布式·架构
数据智能老司机16 小时前
CockroachDB权威指南——CockroachDB 架构
数据库·分布式·架构
IT成长日记16 小时前
【Kafka基础】Kafka工作原理解析
分布式·kafka
州周17 小时前
kafka副本同步时HW和LEO
分布式·kafka
ChinaRainbowSea19 小时前
1. 初始 RabbitMQ 消息队列
java·中间件·rabbitmq·java-rabbitmq
爱的叹息19 小时前
主流数据库的存储引擎/存储机制的详细对比分析,涵盖关系型数据库、NoSQL数据库和分布式数据库
数据库·分布式·nosql
千层冷面20 小时前
RabbitMQ 发送者确认机制详解
分布式·rabbitmq·ruby