项目集成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.结果
相关推荐
问道飞鱼2 小时前
【分布式知识】Spring Cloud Gateway实现跨集群应用访问
分布式·eureka·gateway
Shinobi_Jack2 小时前
c#使用Confluent.Kafka实现生产者发送消息至kafka(远程连接kafka发送消息超时的解决 Local:Message timed out)
分布式·kafka
S-X-S3 小时前
RabbitMQ的消息可靠性保证
分布式·rabbitmq
小林想被监督学习8 小时前
RabbitMQ 在实际应用时要注意的问题
分布式·rabbitmq
乙卯年QAQ8 小时前
【RabbitMq】RabbitMq高级特性-延迟消息
java·rabbitmq·java-rabbitmq
DA02218 小时前
Win10系统部署RabbitMQ Server
分布式·rabbitmq
想做富婆13 小时前
大数据,Hadoop,HDFS的简单介绍
大数据·hadoop·分布式
小白的一叶扁舟14 小时前
Kafka 入门与应用实战:吞吐量优化与与 RabbitMQ、RocketMQ 的对比
java·spring boot·kafka·rabbitmq·rocketmq
霍格沃兹测试开发学社测试人社区14 小时前
软件测试丨消息管道(Kafka)测试体系
软件测试·分布式·测试开发·kafka