[英]Connecting to multiple clusters spring kafka
我想使用來自一個 kafka 集群的消息並發布到另一個 kafka 集群。 想知道如何使用 spring-kafka 進行配置?
只需使用不同的bootstrap.servers
屬性配置消費者和生產者工廠。
如果您使用的是 Spring 引導,請參閱
和
如果您正在創建自己的工廠@Bean
,請在此處設置屬性。
https://docs.spring.io/spring-kafka/docs/current/reference/html/#connecting
您可以使用 spring 雲 stream 卡夫卡粘合劑。
創建兩個 stream 一個用於消費,一個用於生產。
為消費者
public interface InputStreamExample{
String INPUT = "consumer-in";
@Input(INPUT)
MessageChannel readFromKafka();
}
對於生產者
public interface ProducerStreamExample{
String OUTPUT = "produce-out";
@Output(OUTPUT)
MessageChannel produceToKafka();
}
用於消費消息用途:
@StreamListener(value = ItemStream.INPUT)
public void processMessage(){
/*
code goes here
*/
}
用於生產
//here producerStreamExample is instance of ProducerStreamExample
producerStreamExample.produceToKafka().send(/*message goes here*/);
現在使用 binder 配置 consumer 和 producer 集群,您可以使用 consumer-in 用於 consumer 集群和 producer-out 用於 producer 集群。
屬性文件
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.