发现问题 项目引入shardingsphere-jdbc
和shardingsphere-cluster-mode-repository-zookeeper
后启动报错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Caused by: java.lang.NoClassDefFoundError: org/apache/curator/connection/StandardConnectionHandlingPolicy at org.apache.curator.framework.CuratorFrameworkFactory$Builder.<init>(CuratorFrameworkFactory.java:147) at org.apache.curator.framework.CuratorFrameworkFactory$Builder.<init>(CuratorFrameworkFactory.java:130) at org.apache.curator.framework.CuratorFrameworkFactory.builder(CuratorFrameworkFactory.java:78) at org.apache.shardingsphere.mode.repository.cluster.zookeeper.ZookeeperRepository.<init>(ZookeeperRepository.java:65) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at java.lang.Class.newInstance(Class.java:442) at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:380) ... 90 common frames omitted Caused by: java.lang.ClassNotFoundException: org.apache.curator.connection.StandardConnectionHandlingPolicy at java.net.URLClassLoader.findClass(URLClassLoader.java:387) at java.lang.ClassLoader.loadClass(ClassLoader.java:419) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352) at java.lang.ClassLoader.loadClass(ClassLoader.java:352) ... 100 common frames omitted
可以看到CuratorFrameworkFactory
类中引用的StandardConnectionHandlingPolicy
没有被找到
项目的pom.xml
配置如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 <?xml version="1.0" encoding="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > org.example</groupId > <artifactId > mvn-test</artifactId > <version > 1.0.0-SNAPSHOT</version > <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-dependencies</artifactId > <version > Hoxton.SR4</version > <type > pom</type > <scope > import</scope > </dependency > <dependency > <groupId > org.apache.shardingsphere</groupId > <artifactId > shardingsphere-jdbc-core</artifactId > <version > 5.4.1</version > </dependency > <dependency > <groupId > org.apache.shardingsphere</groupId > <artifactId > shardingsphere-cluster-mode-repository-zookeeper</artifactId > <version > 5.4.1</version > </dependency > </dependencies > </dependencyManagement > <dependencies > <dependency > <groupId > org.apache.shardingsphere</groupId > <artifactId > shardingsphere-jdbc-core</artifactId > </dependency > <dependency > <groupId > org.apache.shardingsphere</groupId > <artifactId > shardingsphere-cluster-mode-repository-zookeeper</artifactId > </dependency > </dependencies > </project >
其中shardingsphere-cluster-mode-repository-zookeeper
会引入依赖curator-framework
curator-client
curator-recipes
定位问题 按照经验, 出现依赖项冲突后执行mvn dependency:tree > tree
后检查依赖树发现了奇怪的现象
1 2 3 4 5 6 7 8 [INFO] org.example:mvn-test:jar:1.0.0-SNAPSHOT [INFO] +- org.apache.shardingsphere:shardingsphere-jdbc-core:jar:5.4.1:compile [INFO] \- org.apache.shardingsphere:shardingsphere-cluster-mode-repository-zookeeper:jar:5.4.1:compile [INFO] +- org.apache.shardingsphere:shardingsphere-cluster-mode-repository-api:jar:5.4.1:compile [INFO] +- org.apache.curator:curator-framework:jar:4.0.1:compile [INFO] +- org.apache.curator:curator-client:jar:5.5.0:compile [INFO] | \- org.apache.zookeeper:zookeeper:jar:3.7.1:compile [INFO] \- org.apache.curator:curator-recipes:jar:4.0.1:compile
5.4.1版本的shardingsphere-cluster-mode-repository-zookeeper
引入的curator-client
是正确版本5.5.0, 但是curator-framework
和curator-recipes
都是错误的旧版本4.0.1
检查了shardingsphere-cluster-mode-repository-zookeeper
的pom文件后确认curator的几个库期望结果应该均为5.5.0
控制变量法, dependency
没有异常的话问题可能出在dependencyManagement
中, 删除其他无关依赖项后发现问题出在spring-cloud-dependencies
中, 定位具体原因的话需要使用mvn help:effective-pom
1 mvn help :effective-pom > effective.pom
输出的文件中, 配置的dependencyManagement
会展开, 其中可以发现指定了curator-framework
和curator-recipes
的版本
1 2 3 4 5 6 7 8 9 10 <dependency > <groupId > org.apache.curator</groupId > <artifactId > curator-framework</artifactId > <version > 4.0.1</version > </dependency > <dependency > <groupId > org.apache.curator</groupId > <artifactId > curator-recipes</artifactId > <version > 4.0.1</version > </dependency >
解决问题 dependencyManagement
不支持exclusion, 需要自己声明所需的版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-dependencies</artifactId > <version > Hoxton.SR4</version > <type > pom</type > <scope > import</scope > </dependency > <dependency > <groupId > org.apache.shardingsphere</groupId > <artifactId > shardingsphere-jdbc-core</artifactId > <version > 5.4.1</version > </dependency > <dependency > <groupId > org.apache.shardingsphere</groupId > <artifactId > shardingsphere-cluster-mode-repository-zookeeper</artifactId > <version > 5.4.1</version > </dependency > <dependency > <groupId > org.apache.curator</groupId > <artifactId > curator-framework</artifactId > <version > 5.5.0</version > </dependency > <dependency > <groupId > org.apache.curator</groupId > <artifactId > curator-recipes</artifactId > <version > 5.5.0</version > </dependency > </dependencies > </dependencyManagement >
其他
基于springboot的项目引入sharding-jdbc
5.0-5.2版本官方建议使用shardingsphere-cluster-mode-repository-zookeeper-curator
使用 Spring Boot Starter :: ShardingSphere
5.3版本开始
模式配置 :: ShardingSphere
shardingsphere-jdbc-core-spring-boot-starter,When will version 5.3.0 be released? · Issue #24258 · apache/shardingsphere
参考
Apache Maven Help Plugin – Introduction