简体   繁体   中英

How to set dynamic partition key in spring cloud aws kinesis binder?

I am trying to use Supplier/Consumer to produce and consume messages from Kinesis data stream. Is there a way to add partition key dynamically?

    private BlockingQueue<Message> messages = new LinkedBlockingQueue<>();

    @Bean
    public Supplier<Message<String>> produceMessages() {
        return () ->  this.messages.poll();
    }

    @Override
    public void produce(main.Test request, StreamObserver<Test> response) {
        Message input = MessageBuilder.withPayload(request.getMessage())
                .setHeader("partitionKey", "los").build();
        this.messages.offer(input);
        response.onCompleted();
    }

application.properties

spring.cloud.stream.bindings.produceMessages-out-0.producer.partitionKeyExpression=headers['partitionKey']

We don't know what is channel1 , but according to Spring Cloud Stream docs it has to be like this:

spring.cloud.stream.bindings.produceMessages-out-0.producer.partitionKeyExpression='some-key'

https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream.html#_producer_properties

You can find sample here: https://github.com/spring-cloud/spring-cloud-stream-samples/tree/main/kinesis-samples/kinesis-produce-consume

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM