一个消费者组有一个或者多个消费者
如果一个消费者组订阅了主题,那么这个主题中的某个分区只能分配给消费者组中的某个消费者,不能分配给多个消费者。
当1个消费者,6个分区时,将读取6个分区中的数据。
当3个消费者,6个分区时,每个消费者读取其中的2个分区的数据
当6个消费者,6个分区时,每个消费者读取其中的一个分区的数据。
当7个消费者,6个分区时,有一个消费者将不读取数据,这样会造成资源浪费。
在订阅主题时,自动或手动分配分区。
单数据消费的MessageListener
批量消费的BatchMessageListener
具备ACK机制的AcknowledgingMessageListener和BatchAcknowledgingMessageListener
@KafkaListener
id:消费者的id,当GroupId没有被配置的时候,默认id为GroupId
containerFactory:上面提到了@KafkaListener区分单数据还是多数据消费只需要配置一下注解的containerFactory属性就可以了,
这里面配置的是监听容器工厂,也就是ConcurrentKafkaListenerContainerFactory,配置BeanName
topics:需要监听的Topic,可监听多个
topicPartitions:可配置更加详细的监听信息,必须监听某个Topic中的指定分区,或者从offset为200的偏移量开始监听
errorHandler:监听异常处理器,配置BeanName
groupId:消费组ID
idIsGroup:id是否为GroupId
clientIdPrefix:消费者Id前缀
beanRef:真实监听容器的BeanName,需要在 BeanName前加 "__"
springboot整合kafka消费者代码
com.xml.utils.XmlParse : xml解析类
packagecom.xml.utils;importcom.mongodb.domain.BaseDeviceLog;importcom.mongodb.domain.Key;importcom.utils.common.DateUtils;importorg.dom4j.Document;importorg.dom4j.DocumentHelper;importorg.dom4j.Element;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importjava.text.SimpleDateFormat;import java.util.*;public classXmlParse {private final static Logger logger= LoggerFactory.getLogger(XmlParse.class);public static ListparseXmlFromString(String xmlString){
List baseDeviceLogList=new ArrayList<>();try{
Document document=null;
document=DocumentHelper.parseText(xmlString);/*SAXReader reader =new SAXReader();
Document document=reader.read("E:\\tmp\\test1.xml");*/Element rootElement=document.getRootElement().element("Logs");
Iterator iterator= rootElement.elementIterator("Log");while(iterator.hasNext()){
BaseDeviceLog baseDeviceLog=newBaseDeviceLog();
Element logElement=(Element) iterator.next();if(logElement.elementTextTrim("Guid")!=null){
baseDeviceLog.setLogGuid(logElement.elementTextTrim("Guid"));
}if(logElement.elementTextTrim("SerialNumber")!=null){
baseDeviceLog.setSerialNumber(logElement.elementTextTrim("SerialNumber"));
}if(logElement.elementTextTrim("ProductNumber")!=null){
baseDeviceLog.setProductNumber(logElement.elementTextTrim("ProductNumber"));
}if(logElement.elementTextTrim("SystemID")!=null){
baseDeviceLog.setSystemId(logElement.elementTextTrim("SystemID"));
}if(logElement.elementTextTrim("Type")!=null){
baseDeviceLog.setLogType(logElement.elementTextTrim("Type"));
}if(logElement.elementTextTrim("SourceName")!=null){
baseDeviceLog.setSourceName(logElement.elementTextTrim("SourceName"));
}if(logElement.elementTextTrim("Description")!=null){
baseDeviceLog.setDescription(logElement.elementTextTrim("Description"));
}if(logElement.elementTextTrim("Level")!=null){
baseDeviceLog.setLogLevel(logElement.elementTextTrim("Level"));
}if(logElement.elementTextTrim("Priority")!=null){
baseDeviceLog.setLogType(logElement.elementTextTrim("Priority").toUpperCase());
}if(logElement.elementTextTrim("UID")!=null){
baseDeviceLog.setUid(logElement.elementTextTrim("UID"));
}if(logElement.elementTextTrim("AttamentFilePath")!=null){
baseDeviceLog.setAttamentFilePath(logElement.elementTextTrim("AttamentFilePath"));
}if(logElement.elementTextTrim("AttamentFileMD5")!=null){
baseDeviceLog.setAttamentFileMD5(logElement.elementTextTrim("AttamentFileMD5"));
}if(logElement.elementTextTrim("DevLogFilePath")!=null){
baseDeviceLog.setDevLogFilePath(logElement.elementTextTrim("DevLogFilePath"));
}if(logElement.elementTextTrim("DevLogFileMD5