[英]Connecting to multiple clusters spring kafka
I would like to consume messages from one kafka cluster and publish to another kafka cluster.我想使用来自一个 kafka 集群的消息并发布到另一个 kafka 集群。 Would like to know how to configure this using spring-kafka?想知道如何使用 spring-kafka 进行配置?
Simply configure the consumer and producer factories with different bootstrap.servers
properties.只需使用不同的bootstrap.servers
属性配置消费者和生产者工厂。
If you are using Spring Boot, see如果您使用的是 Spring 引导,请参阅
https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#spring.kafka.consumer.bootstrap-servers https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#spring.kafka.consumer.bootstrap-servers
and和
https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#spring.kafka.producer.bootstrap-servers https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#spring.kafka.producer.bootstrap-servers
If you are creating your own factory @Bean
s, set the properties there.如果您正在创建自己的工厂@Bean
,请在此处设置属性。
https://docs.spring.io/spring-kafka/docs/current/reference/html/#connecting https://docs.spring.io/spring-kafka/docs/current/reference/html/#connecting
you can use spring cloud stream kafka binders.您可以使用 spring 云 stream 卡夫卡粘合剂。
create two stream one for consume and one for producing.创建两个 stream 一个用于消费,一个用于生产。
for consumer为消费者
public interface InputStreamExample{
String INPUT = "consumer-in";
@Input(INPUT)
MessageChannel readFromKafka();
}
for producer对于生产者
public interface ProducerStreamExample{
String OUTPUT = "produce-out";
@Output(OUTPUT)
MessageChannel produceToKafka();
}
for consuming message use:用于消费消息用途:
@StreamListener(value = ItemStream.INPUT)
public void processMessage(){
/*
code goes here
*/
}
for producing用于生产
//here producerStreamExample is instance of ProducerStreamExample
producerStreamExample.produceToKafka().send(/*message goes here*/);
Now configure consumer and producer cluster using binder, you can use consumer-in for consumer cluster and produce-out for producing cluster.现在使用 binder 配置 consumer 和 producer 集群,您可以使用 consumer-in 用于 consumer 集群和 producer-out 用于 producer 集群。
properties file属性文件
spring.cloud.stream.binders.kafka-a.environment.spring.cloud.stream.kafka.binder.brokers:<consumer cluster>
#other properties for this binders
#bind kafka-a to consumer-in
spring.cloud.stream.bindings.consumer-in.binder=kafka-a #kafka-a binding to consumer-in
#similary other properties of consumer-in, like
spring.cloud.stream.bindings.consumer-in.destination=<topic>
spring.cloud.stream.bindings.consumer-in.group=<consumer group>
#now configure cluster to produce
spring.cloud.stream.binders.kafka-b.environment.spring.cloud.stream.kafka.binder.brokers:<cluster where to produce>
spring.cloud.stream.bindings.produce-out.binder=kafka-b #here kafka-b, binding to produce-out
#similary you can do other configuration like topic
spring.cloud.stream.bindings.produce-out.destination=<topic>
Refer to this for more configuration: https://cloud.spring.io/spring-cloud-stream-binder-kafka/spring-cloud-stream-binder-kafka.html更多配置参考: https://cloud.spring.io/spring-cloud-stream-binder-kafka/spring-cloud-stream-binder-kafka.ZFC35FDC70D5FC69D269883A8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.