简体   繁体   中英

Connecting to multiple clusters spring kafka

I would like to consume messages from one kafka cluster and publish to another kafka cluster. Would like to know how to configure this using spring-kafka?

you can use spring cloud stream kafka binders.

create two stream one for consume and one for producing.

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.

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

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