This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21474|Reply: 0

[Source] Spring Boot dynamically sets the Topic or GroupId of Kafka

[Copy link]
Posted on 11/14/2021 8:03:45 PM | | | |
When deploying microservices in a cluster, if we want to issue commands to all applications, we can subscribe the applications to Kafka topics, but it should be noted that different groupIDs need to be set for the same topic to be received by all applications.

Use docker to build a Kafka application with the following commands:

Note: Replace 192.168.1.166 with your actual IP address

pom.xml Introduce the spring-kafka package as follows:


To customize the Kafka consumer configuration, create a new KafkaConsumer file as follows:


Create a new consumer ConfigConsumer, which has the default configuration: topic:defaultTopic,groupid:defaultGroupId, and the code is as follows:

application.properties to configure the address of Kafka:


To start a project, the app subscription is the default configuration, as follows:



KafkaMessageListenerContainer inherits the AbstractMessageListenerContainer abstract class, and the createSimpleLoggingConsumerRebalanceListener method outputs the consumer's groupid and topic, partition.

2021-11-14 19:44:26.689  INFO 14256 --- [ntainer#0-0-C-1] o.s.k.l.KafkaMessageListenerContainer    : defaultGroupId: partitions assigned: [defaultTopic-0]
Dynamically set the topic and groupid, and the project start command is as follows:



The topic is set to a1 and the groupid is itsvse.com

2021-11-14 19:46:25.330  INFO 12256 --- [ntainer#0-0-C-1] o.s.k.l.KafkaMessageListenerContainer    : itsvse.com: partitions assigned: [a1-0]
Spring overrides the default configuration inside the jar package
https://www.itsvse.com/thread-10129-1-1.html

(End)





Previous:Slow queries for MySQL performance optimization
Next:Spring Boot Kafka restarts to discard old data
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com