其他分享
首页 > 其他分享> > springboot整合rabbitmq(fanout模式)

springboot整合rabbitmq(fanout模式)

作者:互联网

生产者

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);
    }
}

image

标签:return,springboot,rabbitmq,Queue,Bean,fanout,public,String
来源: https://www.cnblogs.com/kaka-qiqi/p/14880921.html