生产者
-
新建producer(springboot-order-rabbitmq-producer)
- 修改application.yml
server:
port: 8080
spring:
rabbitmq:
username: admin
password: admin
virtual-host: /
host: 47.100.41.55
port: 5672
@Service
public class OrderService {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
*
* @param userId
* @param productId
* @param num
*/
public void makeOrder(String userId,String productId,int num){
String orderId = UUID.randomUUID().toString();
System.out.println("订单生成成功:" + orderId);
String exchangeName = "fanout_order_exchange";
String routingKey = "";
//@param1 交换机 @param2 路由key/queue队列名称 @param3 消息内容
rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
}
}
@Configuration
public class RabbitMqConfiguration {
//声明交换机
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("fanout_order_exchange",true,false);
}
//声明队列
@Bean
public Queue smsQueue(){
return new Queue("sms.fanout.queue",true);
}
@Bean
public Queue duanxinQueue(){
return new Queue("duanxin.fanout.queue",true);
}
@Bean
public Queue emailQueue(){
return new Queue("email.fanout.queue",true);
}
//完成交换机和队列绑定
@Bean
public Binding smsBinding(){
return BindingBuilder.bind(smsQueue()).to(fanoutExchange());
}
@Bean
public Binding duanxinBinding(){
return BindingBuilder.bind(duanxinQueue()).to(fanoutExchange());
}
@Bean
public Binding emailBinding(){
return BindingBuilder.bind(emailQueue()).to(fanoutExchange());
}
}
@SpringBootTest
class SpringbootOrderRabbitmqProducerApplicationTests {
@Autowired
private OrderService orderService;
@Test
void contextLoads() {
orderService.makeOrder("1","1",12);
}
}