SpringBoot整合kafka遇到版本不对应
SpringBoot 整合 kafka 需要在SpringBoot项目里增加kafka的jar,而最为关键的一点是版本要对应好。
如果你的SpringBoot是2.0.3版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
那么你可以用2.1.7版本的kafka
<!-- kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
如果你的SpringBoot比较新,用的2.1.0版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
那么对应的,你的kafka也应该用新版本
<!-- https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
当然,版本号可能其他方式也可以。
不过如果你用2.1.0版本的SpringBoot,却仍使用2.1.7版本的kafka,则会遇到如下错误:
D:\DevelopFiles\Java\jdk1.8.0_131\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8247 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=8248:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\charsets.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\deploy.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\javaws.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\jce.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\jfr.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\jsse.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\management-agent.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\plugin.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\resources.jar;D:\DevelopFiles\Java\jdk1.8.0_131\jre\lib\rt.jar;D:\workspace\IDEA\chinaunicom\alarm-query-kafka\target\classes;D:\DevelopFiles\Maven\m2\org\springframework\boot\spring-boot-starter\2.1.0.RELEASE\spring-boot-starter-2.1.0.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\springframework\boot\spring-boot\2.1.0.RELEASE\spring-boot-2.1.0.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\springframework\boot\spring-boot-autoconfigure\2.1.0.RELEASE\spring-boot-autoconfigure-2.1.0.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\springframework\boot\spring-boot-starter-logging\2.1.0.RELEASE\spring-boot-starter-logging-2.1.0.RELEASE.jar;D:\DevelopFiles\Maven\m2\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\DevelopFiles\Maven\m2\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\DevelopFiles\Maven\m2\org\apache\logging\log4j\log4j-to-slf4j\2.11.1\log4j-to-slf4j-2.11.1.jar;D:\DevelopFiles\Maven\m2\org\apache\logging\log4j\log4j-api\2.11.1\log4j-api-2.11.1.jar;D:\DevelopFiles\Maven\m2\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\DevelopFiles\Maven\m2\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\DevelopFiles\Maven\m2\org\springframework\spring-core\5.1.2.RELEASE\spring-core-5.1.2.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\springframework\spring-jcl\5.1.2.RELEASE\spring-jcl-5.1.2.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\DevelopFiles\Maven\m2\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\DevelopFiles\Maven\m2\org\springframework\kafka\spring-kafka\2.1.7.RELEASE\spring-kafka-2.1.7.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\springframework\spring-context\5.1.2.RELEASE\spring-context-5.1.2.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\springframework\spring-aop\5.1.2.RELEASE\spring-aop-5.1.2.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\springframework\spring-beans\5.1.2.RELEASE\spring-beans-5.1.2.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\springframework\spring-expression\5.1.2.RELEASE\spring-expression-5.1.2.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\springframework\spring-messaging\5.1.2.RELEASE\spring-messaging-5.1.2.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\springframework\spring-tx\5.1.2.RELEASE\spring-tx-5.1.2.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\springframework\retry\spring-retry\1.2.2.RELEASE\spring-retry-1.2.2.RELEASE.jar;D:\DevelopFiles\Maven\m2\org\apache\kafka\kafka-clients\2.0.0\kafka-clients-2.0.0.jar;D:\DevelopFiles\Maven\m2\org\lz4\lz4-java\1.4.1\lz4-java-1.4.1.jar;D:\DevelopFiles\Maven\m2\org\xerial\snappy\snappy-java\1.1.7.1\snappy-java-1.1.7.1.jar;D:\DevelopFiles\Maven\m2\com\alibaba\fastjson\1.2.47\fastjson-1.2.47.jar;D:\DevelopFiles\Maven\m2\org\apache\commons\commons-lang3\3.8.1\commons-lang3-3.8.1.jar com.unicom.AlarmQueryKafkaApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.RELEASE)
2018-10-31 19:53:47.061 INFO 14260 --- [ main] com.unicom.AlarmQueryKafkaApplication : Starting AlarmQueryKafkaApplication on SHNWANGC260 with PID 14260 (D:\workspace\IDEA\chinaunicom\alarm-query-kafka\target\classes started by wangc260 in D:\workspace\IDEA\chinaunicom)
2018-10-31 19:53:47.068 INFO 14260 --- [ main] com.unicom.AlarmQueryKafkaApplication : No active profile set, falling back to default profiles: default
2018-10-31 19:53:48.299 INFO 14260 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.kafka.annotation.KafkaBootstrapConfiguration' of type [org.springframework.kafka.annotation.KafkaBootstrapConfiguration$$EnhancerBySpringCGLIB$$60ef0a74] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-10-31 19:53:48.524 WARN 14260 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springConsumer' defined in file [D:\workspace\IDEA\chinaunicom\alarm-query-kafka\target\classes\com\unicom\kafka\SpringConsumer.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchFactory' defined in class path resource [com/unicom/kafka/kafkaConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.kafka.config.KafkaListenerContainerFactory]: Factory method 'batchFactory' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory.getContainerProperties()Lorg/springframework/kafka/listener/ContainerProperties;
2018-10-31 19:53:48.536 INFO 14260 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-10-31 19:53:48.551 ERROR 14260 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call the method org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory.getContainerProperties()Lorg/springframework/kafka/listener/ContainerProperties; but it does not exist. Its class, org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory, is available from the following locations:
jar:file:/D:/DevelopFiles/Maven/m2/org/springframework/kafka/spring-kafka/2.1.7.RELEASE/spring-kafka-2.1.7.RELEASE.jar!/org/springframework/kafka/config/ConcurrentKafkaListenerContainerFactory.class
It was loaded from the following location:
file:/D:/DevelopFiles/Maven/m2/org/springframework/kafka/spring-kafka/2.1.7.RELEASE/spring-kafka-2.1.7.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory
Process finished with exit code 1
SpringBoot整合kafka问题的注意事项
要想外网的机器可以连接成功需要在kafka的server.properties配置文件中需要如下配置
这个需要注意一下,填写主机IP的时候不能写localhost,如果这样写外边的主机是无法连接kafka的,需要标明具体的机器外网IP。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。