第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Spring Cloud Stream實(shí)現(xiàn)消息過(guò)濾消費(fèi)

標(biāo)簽:
Spring Cloud

TIPS

本文基于Spring Cloud Greenwich SR1 + spring-cloud-starter-stream-rocketmq 0.9.0

理论兼容:Spring Cloud Finchley+ + spring-cloud-starter-stream-rocketmq 0.2.2+

MQ使用的是RocketMQ,也可使用Kafka或者RabbitMQ。

本文探讨Spring Cloud Stream & RocketMQ过滤消息的各种姿势。

在实际项目中,我们可能需要实现消息消费的过滤。

举个例子:实现消息的分流处理:

生产者生产的消息,虽然消息体可能一样,但是header不一样。可编写两个或者更多的消费者,对不同header的消息做针对性的处理!

condition

生产者

生产者设置一下header,比如my-header,值根据你的需要填写:

@Autowired
private Source source;

public String testStream() {
  this.source.output()
    .send(
    MessageBuilder
    .withPayload("消息体")
    .setHeader("my-header","你的header")
    .build()
  );
  return "success";
}

消费者

@Service
@Slf4j
public class TestStreamConsumer {
    @StreamListener(value = Sink.INPUT,condition = "headers['my-header']=='你的header'")
    public void receive(String messageBody) {
        log.info("通过stream收到了消息:messageBody ={}", messageBody);
    }
}

如代码所示,使用 StreamListener 注解的 condition 属性。当 headers['my-header']=='你的header' 条件满足,才会进入到方法体。

Tags

TIPS

该方式只支持RoketMQ,不支持Kafka/RabbitMQ

生产者

@Autowired
private Source source;

public String testStream() {
  this.source.output()
    .send(
    MessageBuilder
    .withPayload("消息体")
    // 注意:只能设置1个tag
    .setHeader(RocketMQHeaders.TAGS, "tag1")
    .build()
  );
  return "success";
}

消费者

  • 接口

    public interface MySink {
        String INPUT1 = "input1";
        String INPUT2 = "input2";
    
        @Input(INPUT1)
        SubscribableChannel input();
    
        @Input(INPUT2)
        SubscribableChannel input2();
    }
    
  • 注解

    @EnableBinding({MySink.class})
    
  • 配置

    spring:
      cloud:
        stream:
          rocketmq:
            binder:
              name-server: 127.0.0.1:9876
            bindings:
              input1:
                consumer:
                  # 表示input2消费带有tag1的消息
                  tags: tag1
              input2:
                consumer:
                  # 表示input2消费带有tag2或者tag3的消息
                  tags: tag2||tag3
          bindings:
            input1:
              destination: test-topic
              group: test-group1
            input2:
              destination: test-topic
              group: test-group2
    
  • 消费代码

    @Service
    @Slf4j
    public class MyTestStreamConsumer {
        /**
         * 我消费带有tag1的消息
         *
         * @param messageBody 消息体
         */
        @StreamListener(MySink.INPUT1)
        public void receive1(String messageBody) {
            log.info("带有tag1的消息被消费了:messageBody ={}", messageBody);
        }
    
        /**
         * 我消费带有tag1或者tag2的消息
         *
         * @param messageBody 消息体
         */
        @StreamListener(MySink.INPUT2)
        public void receive2(String messageBody) {
            log.info("带有tag2/tag3的消息被消费了:messageBody ={}", messageBody);
        }
    }
    
  • 日志:

    2019-08-04 19:10:03.799  INFO 53760 --- [MessageThread_1] c.i.u.rocketmq.MyTestStreamConsumer      : 带有tag1的消息被消费了:messageBody =消息体
    

Sql 92

TIPS

  • 该方式只支持RoketMQ,不支持Kafka/RabbitMQ
  • 用了sql,就不要用Tag

RocketMQ支持使用SQL语法过滤消息。官方文档:http://rocketmq.apache.org/rocketmq/filter-messages-by-sql92-in-rocketmq/

Spring Clous Stream RocketMQ也为此特性提供了支持。

开启SQL 92支持

默认情况下,RocketMQ的SQL过滤支持是关闭的,要想使用SQL 92过滤消息,需要:

  • conf/broker.conf 添加

    enablePropertyFilter = true
    
  • 启动RocketMQ

    nohup sh bin/mqbroker -n localhost:9876 -c ./conf/broker.conf &
    

生产者

@Autowired
private Source source;

public String testStream() {
  this.source.output()
    .send(
    MessageBuilder
    .withPayload("消息体")
    .setHeader("index", 1000)
    .build()
  );
  return "success";
}

消费者

  • 接口

    public interface MySink {
        String INPUT1 = "input1";
        String INPUT2 = "input2";
    
        @Input(INPUT1)
        SubscribableChannel input();
    
        @Input(INPUT2)
        SubscribableChannel input2();
    }
    
  • 注解

    @EnableBinding({MySink.class})
    
  • 配置

    spring:
      cloud:
        stream:
          rocketmq:
            binder:
              name-server: 127.0.0.1:9876
            bindings:
              input1:
                consumer:
                  sql: 'index < 1000'
              input2:
                consumer:
                  sql: 'index >= 1000'
          bindings:
            input1:
              destination: test-topic
              group: test-group1
            input2:
              destination: test-topic
              group: test-group2
    
  • 消费代码

    @Service
    @Slf4j
    public class MyTestStreamConsumer {
        /**
         * 我消费带有tag1的消息
         *
         * @param messageBody 消息体
         */
        @StreamListener(MySink.INPUT1)
        public void receive1(String messageBody) {
            log.info("index > 1000的消息被消费了:messageBody ={}", messageBody);
        }
    
        /**
         * 我消费带有tag1或者tag2的消息
         *
         * @param messageBody 消息体
         */
        @StreamListener(MySink.INPUT2)
        public void receive2(String messageBody) {
            log.info("index <=1000 的消息被消费了:messageBody ={}", messageBody);
        }
    }
    
  • 日志

    2019-08-04 19:58:59.787  INFO 56375 --- [MessageThread_1] c.i.u.rocketmq.MyTestStreamConsumer      : index <=1000 的消息被消费了:messageBody =消息体
    

相关代码

org.springframework.cloud.stream.binder.rocketmq.properties.RocketMQConsumerProperties

参考文档

本文首发

······················
欢迎关注课程:

點(diǎn)擊查看更多內(nèi)容
5人點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消