Springboot RabbitMq广播设置

服务C对接微博RTB服务,服务B本地组装数据,服务A接收业务变化;

当服务A变化时,需要通知服务B及时修改

服务A注入:

/**
 * @author Doctor
 */
@Component
public class WaxPutPlanSendToAdxConfiguration {
   
   /** 交换名称 */
 public static final String BUSINESS_FLOW_WAX_PUT_TO_ADX_EXCHANG = "business-flow-wax-put-to-adx-exchang";

 /**
 *FanoutExchange广播交换
 * @return
 */
 @Bean(name = BUSINESS_FLOW_WAX_PUT_TO_ADX_EXCHANG)
   public FanoutExchange toAdxExchange(){
      return new FanoutExchange(BUSINESS_FLOW_WAX_PUT_TO_ADX_EXCHANG, true, false);
 }

}

触发:

/**
 * amqpTemplate
 */
@Autowired
private AmqpTemplate amqpTemplate;
/**
 * @描述:通知adx-wax计划列表有更新(包含有规则和无规则)
 */
public void noticeAdx(){
    MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
        @Override
 public Message postProcessMessage(Message message) {
            return message;
 }
    };
 amqpTemplate.convertAndSend(WaxPutPlanSendToAdxConfiguration.BUSINESS_FLOW_WAX_PUT_TO_ADX_EXCHANG,"","changed",messagePostProcessor);
}

服务B注入:(IpUtils.getLocalhostIp()动态)

/**
 * @author Doctor
 */
@Component
public class WaxPutPlanReceiveServerConfiguration {

   /** 交换名称 */
 public static final String BUSINESS_FLOW_WAX_PUT_TO_ADX_EXCHANG = "business-flow-wax-put-to-adx-exchang";

 /** 通用队列 */
 public static final String BUSINESS_FLOW_WAX_PUT_TO_ADX_QUEUE = "business-flow-wax-put-to-adx-queue-"+ IpUtils.getLocalhostIp();
 

 /**
 * 交换
 */
 @Bean
   public FanoutExchange toAdxExchange(){
      return new FanoutExchange(BUSINESS_FLOW_WAX_PUT_TO_ADX_EXCHANG, true, false);
 }

   /**
 * 队列
 */
 @Bean
   public Queue toAdxQueue() {
      return new Queue(BUSINESS_FLOW_WAX_PUT_TO_ADX_QUEUE,true);
 }

/**
 * 绑定 队列 到 交换
 *
 * @param toAdxExchange 交换
 * @param toAdxQueue 队列
 */
 @Bean
   public Binding adxBinding(FanoutExchange toAdxExchange, Queue toAdxQueue) {
      return BindingBuilder.bind(toAdxQueue).to(toAdxExchange);
 }

   
}

监听:

/**
 * @描述:接收缓存变更通知 监听队列配置动态名称很重要
 * @param msg
 * @throws Exception
 */
@RabbitListener(queues = "#{toAdxQueue.name}")
public void receiveServerWaxPlanChange(String msg){
    //处理业务逻辑
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值