Skip to main content

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()));
}

}

2.1 The Three Components That Support Injection on the dawdler Server Side​

  1. DawdlerFilter service filter

  2. DawdlerServiceListener listener

  3. Interface implementation classes annotated with @Service