Skip to main content

dawdler-client-plug-kafka

Module Introduction​

Provides support for injecting KafkaProvider and the KafkaListener annotation on the web side.

1. Adding the Dependency to pom​

<groupId>club.dawdler</groupId>
<artifactId>dawdler-client-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.

@Controller
public class UserController{

@KafkaInjector("myKafka")// myKafka is the name of the configuration file, without the properties suffix
KafkaProvider kafkaProvider;

@RequestMapping(value = "/pushMessage", method = RequestMethod.POST)
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 Web Side​

  1. Web-side controller

  2. Web-side interceptor HandlerInterceptor

  3. Web-side listener WebContextListener