1、监听配置
package *************.listener;
import cn.hutool.extra.spring.SpringUtil;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.config.MethodKafkaListenerEndpoint;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.listener.MessageListener;
import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.stereotype.Service;
import java.lang.reflect.Method;
import java.util.List;
@Service
public class KafkaListenerService {
@Autowired
@Qualifier("kafkaListenerService")
private KafkaListenerEndpointRegistry registry;
@Autowired
private KafkaMessageHandlerService kafkaMessageHandlerService;
public void addListener(String topic, String groupId) throws NoSuchMethodException {
String id = topic + "-" + groupId;
if (registry.getListenerContainer(id) != null) {
System.out.println("Listener already exists for topic: " + topic + " and group: " + groupId);
return;
}
MethodKafkaListenerEndpoint<String, String> endpoint = new MethodKafkaListenerEndpoint<>();
//设置监听器端点相关信息
//设置Id
endpoint.setId(id);
//设置消费者组
endpoint.setGroupId(groupId);
//设置要监听的topic数组,可以是多个
endpoint.setTopics(topic);
//设置每个监听器线程数
endpoint.setConcurrency(1);
//设置批量监听
endpoint.setBatchListener(true);
//设置消息处理工厂类,这里用的是默认工厂
endpoint.setMessageHandlerMethodFactory(new DefaultMessageHandlerMethodFactory());
//设置实际处理的Bean对象,即实际的对象,比如new Class();
endpoint.setBean(kafkaMessageHandlerService);
//设置实际处理的方法(包含方法名和参数)
Method method = KafkaMessageHandlerService.class.getDeclaredMethod("handleMessage", List.class);
endpoint.setMethod(method);
//注册Container并启动,startImmediately表示立马启动
registry.registerListenerContainer(endpoint, SpringUtil.getBean(KafkaListenerContainerFactory.class), true);
System.out.println("Added listener for topic: " + topic + " and group: " + groupId);
}
}
2、初始化监听
方法一:放代码中 项目启动加载相关的config调用,相关的topic可以维护在数据库中,启动查询遍历初始化public void createKafkaListener(String topic){
log.info("初始化topic信息topic:{}",topic);
try {
kafkaListenerService.addListener(topic, Const.ALARM_GROUP);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
方法二:放启动类里面固定tpoic用来方便测试
@SpringBootApplication
@MapperScan(value = {"com.aaa.aaa.module.aaa.dal.mapper.*"})
public class Application {
public static void main(String[] args) throws NoSuchMethodException {
SpringApplication.run(Application.class, args);
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
KafkaListenerService kafkaListenerService = context.getBean(KafkaListenerService.class);
kafkaListenerService.addListener("testTopic", "testGroup");
System.out.printf("开启");
}
3、接收到Kafka消息
package com.inspur.industry.module.alarm.listener;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.stereotype.Service;
import java.util.List;
@Slf4j
@Service
public class KafkaMessageHandlerService {
public void handleMessage(List<ConsumerRecord<String, Object>> records) {
log.info("kafka收到消息records:{}", records);
for (ConsumerRecord<String, Object> record : records) {
// 打印消息的内容和元数据
// System.out.println("Received Message: " + record.value());
// System.out.println("Topic: " + record.topic());
// System.out.println("Partition: " + record.partition());
// System.out.println("Offset: " + record.offset());
String recordValue = (String) record.value();
log.info("kafka消息转换为recordValue:{}", recordValue);
parse(recordValue.toString());
}
}
/**
* @param recordValue 业务数据
*/
private void parse(String recordValue) {
log.info("进入parseAlarmRules()方法,recordValue:" + recordValue);
//将收到的业务数据转换成JSON对象
JSONObject valueObject = JSONObject.parseObject(recordValue);
log.info("将接收到的数据转换为valueObject");
//具体业务逻辑处理
}
}
注意:初始化监听非常重要,如果不初始化监听,就会接收不到Kafka消息
具体的Kafka配置和发送,前面文章已经提及,可以参考前面文章,不在叙述