单个项目中的多个 Spring Cloud GCP 库导致 NoClassDefFoundError

Multiple Spring Cloud GCP libraries in single project causes NoClassDefFoundError

如果我使用单个 Spring Cloud GCP 库,例如implementation("org.springframework.cloud:spring-cloud-gcp-starter-sql-postgresql:1.1.1.RELEASE") 和 属性:spring.cloud.gcp.credentials.encoded-key= 凭据...一切正常。

但是如果我还想通过以下方式将 pub/sub 添加到我的项目中:implementation("org.springframework.cloud:spring-cloud-gcp-starter-pubsub:1.1.3.RELEASE") 那么我会得到以下异常。

我试过:

  1. 使用不同的 Java 版本 11 和 12
  2. 不同的 Spring Cloud GCP 库作为 "second" 库 implementation("org.springframework.cloud:spring-cloud-gcp-starter:1.1.3.RELEASE")implementation("org.springframework.cloud:spring-cloud-gcp-starter-logging:1.1.3.RELEASE")
  3. 使用 spring.cloud.gcp.credentials.location 代替 spring.cloud.gcp.credentials.encoded-key
  4. 使用管理员服务帐户确保这不是 IAM 角色问题。

implementation("org.springframework.cloud:spring-cloud-gcp-starter:1.1.3.RELEASE")implementation("org.springframework.cloud:spring-cloud-gcp-starter-logging:1.1.3.RELEASE")

一旦我包含多​​个 Spring Cloud GCP 库,同样的 SocketFactory 错误就会出现。从文档来看,它应该可以正常工作。第二个库应该只使用相同的凭据。

如果我恢复到单个库,那么它工作正常。

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.jdbc.DataSourceProperties]: Factory method 'cloudSqlDataSourceProperties' threw exception; nested exception is java.lang.NoClassDefFoundError: com/google/cloud/sql/core/CoreSocketFactory
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    ... 171 common frames omitted
Caused by: java.lang.NoClassDefFoundError: com/google/cloud/sql/core/CoreSocketFactory
    at org.springframework.cloud.gcp.autoconfigure.sql.GcpCloudSqlAutoConfiguration$CloudSqlDataSourcePropertiesConfiguration.cloudSqlDataSourceProperties(GcpCloudSqlAutoConfiguration.java:209) ~[spring-cloud-gcp-autoconfigure-1.1.3.RELEASE.jar:1.1.3.RELEASE]
    at org.springframework.cloud.gcp.autoconfigure.sql.GcpCloudSqlAutoConfiguration$CloudSqlDataSourcePropertiesConfiguration$$EnhancerBySpringCGLIB$f5495da.CGLIB$cloudSqlDataSourceProperties[=11=](<generated>) ~[spring-cloud-gcp-autoconfigure-1.1.3.RELEASE.jar:1.1.3.RELEASE]
    at org.springframework.cloud.gcp.autoconfigure.sql.GcpCloudSqlAutoConfiguration$CloudSqlDataSourcePropertiesConfiguration$$EnhancerBySpringCGLIB$f5495da$$FastClassBySpringCGLIB$c2377.invoke(<generated>) ~[spring-cloud-gcp-autoconfigure-1.1.3.RELEASE.jar:1.1.3.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.cloud.gcp.autoconfigure.sql.GcpCloudSqlAutoConfiguration$CloudSqlDataSourcePropertiesConfiguration$$EnhancerBySpringCGLIB$f5495da.cloudSqlDataSourceProperties(<generated>) ~[spring-cloud-gcp-autoconfigure-1.1.3.RELEASE.jar:1.1.3.RELEASE]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    ... 172 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.google.cloud.sql.core.CoreSocketFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
    ... 183 common frames omitted

我还在此处创建了一个 Github 问题:https://github.com/spring-cloud/spring-cloud-gcp/issues/1948

由于没有检查版本而从 Maven Central 复制粘贴导入,因此使用了不同版本的库。

使用的版本包括 1.1.1.RELEASE1.1.3.RELEASE。一次只能使用其中一个版本号。

重构代码以消除此错误的可能性:

implementation("org.springframework.cloud:spring-cloud-gcp-starter-sql-postgresql:$springCloudGCPVersion")
implementation("org.springframework.cloud:spring-cloud-gcp-starter-pubsub:$springCloudGCPVersion")
implementation("org.springframework.cloud:spring-cloud-gcp-dependencies:$springCloudGCPVersion")
implementation("org.springframework.cloud:spring-cloud-gcp-starter:$springCloudGCPVersion")