Bootstrap

项目集成RabbitMQ

1.common-rabbitmq-starter

1.创建common-rabbitmq-starter

CleanShot 2024-12-31 at 18.38.04@2x

2.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>
    <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
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
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunxiansheng.rabbitmq.config.RabbitMQAutoConfiguration

2.测试使用

1.创建common-rabbitmq-starter-demo

CleanShot 2024-12-31 at 18.48.22@2x

2.目录结构

CleanShot 2024-12-31 at 19.13.13@2x

3.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>
    <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
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 配置交换机和队列
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 监听队列
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 发布消息
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.结果

CleanShot 2024-12-31 at 19.15.06@2x

;