启动含Kafka的Spring Cloud Stream应用遇UnsupportedVersionException如何解决?
Hey there, let's break down this issue you're facing! The error org.apache.kafka.common.errors.UnsupportedVersionException: The broker does not support CREATE_PARTITIONS is a classic version mismatch problem between your Kafka client (used by Spring Cloud Stream) and the Kafka broker you're connecting to. Since another project works fine with the same broker, the root cause is almost certainly a version difference in your dependencies. Here's how to fix it:
1. 对齐Kafka客户端依赖版本
First, compare the kafka-clients or spring-kafka dependency versions between your problematic project and the working one. The CREATE_PARTITIONS API was introduced in Kafka 1.0.0 for brokers—if your broker is older than that (e.g., 0.11.x or lower), using a newer client version will trigger this error.
- Open your
pom.xmland explicitly set thekafka-clientsversion to match the working project:<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>匹配的版本号</version> </dependency> - Make sure this version is compatible with your Spring Cloud Stream binder version (stick to the version combinations used in the working project to avoid extra issues).
2. 同步Spring Cloud Stream绑定器版本
Spring Cloud Stream's Kafka binder has strict version alignment with Spring Boot and Spring Cloud. If your project uses a newer binder version, it might automatically pull in a higher kafka-clients version that your broker can't support.
- Update your Spring Cloud Stream Kafka binder dependency to match the working project's version. For example:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-kafka</artifactId> <version>对应版本号</version> </dependency> - Alternatively, use the Spring Cloud BOM in your pom to manage versions consistently, so you don't have to specify each binder version manually.
3. 关闭自动创建分区的功能
If you can't adjust dependency versions right now, you can disable the automatic partition creation that's triggering the unsupported API call. Add this configuration to your application.properties or application.yml:
spring.cloud.stream.kafka.binder.create-partitions=false
Note: The exact property key might vary slightly based on your binder version—double-check the docs for your specific version if this doesn't work.
4. 验证Broker版本(可选)
Just to be thorough, confirm your Kafka broker's version by running this command on the broker server:
kafka-server-start.sh --version
If it's older than 1.0.0, upgrading the broker to a version that supports CREATE_PARTITIONS (1.0.0+) will resolve the issue long-term, though this requires more operational work.
Since another project works with the same broker, aligning your dependency versions to match that project is the quickest and most reliable fix.
内容的提问来源于stack exchange,提问作者zhangtao

