dawdler-server-plug-kafka
Module Introduction
Provides support for injecting KafkaProvider and the KafkaListener annotation on the dawdler-server side.
1. Adding the Dependency to pom
<groupId>club.dawdler</groupId>
<artifactId>dawdler-server-plug-kafka</artifactId>
2. Usage
Producer: simply use the @KafkaInjector annotation to mark a global variable as a KafkaProvider-typed variable.
Consumer: mark a consumer method with @KafkaListener; the method parameter is of type Message.
public class UserServiceImpl implements UserService {
@KafkaInjector("myKafka")// myKafka is the name of the configuration file, without the properties suffix
KafkaProvider kafkaProvider;
public void pushMessage(String message) {
kafkaProvider.publish("test-topic", message.getBytes());// use the kafkaProvider object
}
@KafkaListener(fileName = "myKafka", topic = "test-topic") // listen to the test-topic topic
public void consumer(Message message) {
System.out.println(new String(message.getBody()));
}
}