支持的消息模式
![]()
消息发送者
application.yml rabbitmq配置文件
# 服务端口号
server:
port: 8080
# 创建MQ服务连接
spring:
rabbitmq:
host: 192.168.43.213
port: 5672
username: admin
password: admin
virtual-host: /
config交换机与队列绑定类
package com.zhang.rabbitmq.springbootrabbitmqproducer.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;
service消息发送类(消息生产者)
package com.zhang.rabbitmq.springbootrabbitmqproducer.service;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.UUID;
test 测试类
package com.zhang.rabbitmq.springbootrabbitmqproducer;
import com.zhang.rabbitmq.springbootrabbitmqproducer.service.OrderService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootRabbitmqProducerApplicationTests {
@Autowired
private OrderService orderService;
@Test
void contextLoads() {
orderService.morikOrder("1","2",20);
}
}
消息接收者
application.yml rabbitmq配置文件
# 服务端口号
server:
port: 8081
# 创建MQ服务连接
spring:
rabbitmq:
host: 192.168.43.213
port: 5672
username: admin
password: admin
virtual-host: /
消息接收者一
package com.zhang.rabbitmq.springbootrabbitmqconsumer.service.fanout;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
消息接收者二
package com.zhang.rabbitmq.springbootrabbitmqconsumer.service.fanout;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
消息接收者三
package com.zhang.rabbitmq.springbootrabbitmqconsumer.service.fanout;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;