一、搭建项目:
1、创建父部项目Project
1.父部环境2.父部依赖:做依赖管理 2、创建子项目module:send
1.搭建环境:2.依赖:3. application4.send配置类:5. send test: 3、创建子项目module:revc。同sendRecvInit 二、测试:此demo是简单模式
一、搭建项目: 1、创建父部项目Project 1.父部环境为什么使用spring-AMQP
1.基于spring之上,社区活跃
2.对MAQP进行了高度封装
3.极大简化了RabbitMQ的操作
4.易用性、可扩展
2、创建子项目module:send 1.搭建环境:4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.7.RELEASE com.xxxx springamp-demo 1.0-SNAPSHOT UTF-8 1.8 1.8
3. application4.0.0 com.xxxx amqp-send 1.0-SNAPSHOT com.xxxx springamp-demo 1.0-SNAPSHOT UTF-8 1.8 1.8 org.springframework.boot spring-boot-starter-amqp org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test
创建 resources文件夹
创建application.yml:
spring:
rabbitmq:
host: 192.168.75.100
port: 5672
username: yeb
password: yeb
virtual-host: /yeb
server:
port: 8081
4.send配置类:
package com.xxxx.send.config;
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.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
@Bean
public Queue queue(){
return new Queue("amqp_queue");
}
@Bean
public TopicExchange topicExchange(){
return new TopicExchange("amqp_exchange");
}
@Bean
public Binding binding(){
return BindingBuilder.bind(queue()).to(topicExchange()).with("*.amqp.#");
}
}
5. send test:
@SpringBootTest
@RunWith(SpringRunner.class)
public class RabbitMQTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testSend(){
String message = "hell0";
System.out.println("发送消息: " + message);
//发送消息:
//(交换机、路由、消息)
//其中配置类路由是:"*.amqp.#" 而 test.amqp是可以找到的。
rabbitTemplate.convertAndSend("amqp_exchange","test.amqp",message);
}
}
3、创建子项目module:revc。同send
- 2.1 和 2.2同创建send基本一模一样2.3:将端口分开:application.yml:server: port 8082没有2.4和2.5的配置类和测试文件
package com.xxxx.send;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
//监听队列:
@RabbitListener(queues = "amqp_queue")
public class RecvInit {
//监听后获取消息的方法:
@RabbitHandler
public void testRecv(String message){
System.out.println("接收到的消息: " + message);
}
}
二、测试:此demo是简单模式
必须先启动send,进行发送:
然后启动recv,进行监听,接受消息:



