文章目录
- 1.common-rabbitmq-starter
- 1.创建common-rabbitmq-starter
- 2.pom.xml
- 3.自动配置
- 1.RabbitMQAutoConfiguration.java
- 2.spring.factories
- 2.测试使用
- 1.创建common-rabbitmq-starter-demo
- 2.目录结构
- 3.pom.xml
- 4.application.yml
- 5.TestConfig.java 配置交换机和队列
- 6.TestConfigConsumer.java 监听队列
- 7.TestConfigPublisher.java 发布消息
- 8.结果
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><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;
@Configuration
public class RabbitMQAutoConfiguration {
}
2.spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunxiansheng.rabbitmq.config.RabbitMQAutoConfiguration
2.测试使用
rabbitmqstarterdemo_73">1.创建common-rabbitmq-starter-demo
2.目录结构
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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><exclusions><exclusion><artifactId>spring-boot-starter-logging</artifactId><groupId>org.springframework.boot</groupId></exclusion></exclusions></dependency></dependencies>
</project>
4.application.yml
spring:rabbitmq:host: ipusername: guestpassword: guestvirtual-host: /port: 6783
log:home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-demo/common-rabbitmq-starter-demomodule: 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;
@Configuration
public class TestConfig {@Beanpublic FanoutExchange fanoutExchange() {return new FanoutExchange("fanout.exchange.test");}@Beanpublic Queue fanoutQueueTest() {return new Queue("fanout.queue.test");}@Beanpublic 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;
@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;
@SpringBootTest
public class TestConfigPublisher {@Resourceprivate AmqpTemplate amqpTemplate;@Testpublic void send() {amqpTemplate.convertAndSend("fanout.exchange.test", "", "hello rabbitmq");}
}
8.结果