问题
I am trying to find example of spring cloud stream where it creates parition based producer for RMQ. I want to see how it will create binding for those queues as RMQ not natively support paritions for topic but it will create number of queues equivalent to number of partitions (I read this,I may be wrong). First I want to understand how to create producer using spring cloud stream on RMQ for partition based producer.
回答1:
@SpringBootApplication
@EnableBinding(Source.class)
public class So43614477Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(So43614477Application.class, args);
}
@Autowired
private MessageChannel output;
@Override
public void run(String... args) throws Exception {
output.send(MessageBuilder.withPayload("fiz").setHeader("whichPart", 0).build());
output.send(MessageBuilder.withPayload("buz").setHeader("whichPart", 1).build());
}
}
with properties...
spring.cloud.stream.bindings.output.destination=foo
spring.cloud.stream.bindings.output.producer.partition-key-expression=headers['whichPart']
spring.cloud.stream.bindings.output.producer.partition-count=2
spring.cloud.stream.bindings.output.producer.required-groups=bar
I added the required-groups
so you can see how the consumer queues are bound.
foo
exhange bindings:
The expression can be any valid expression evaluated against the message (e.g. payload.hashCode()
); it is then modded %
against the partition count to determine the final partition.
来源:https://stackoverflow.com/questions/43614477/how-to-use-rmq-and-spring-cloud-stream-for-creating-partition-based-producer