0

在 Cloud Foundry 中连接 db2 服务时遇到问题。我已将服务创建为cf cups db2service -p "jdbcUrl,user,password"。在云上部署应用程序时,我面临类转换异常,也没有找到合适的连接器异常。以下是我的配置类。

@Configuration
@ServiceScan
@Profile("cloud")
public class Db2CloudConfig extends AbstractCloudConfig {

    @Bean
    public DataSource db2servicenew() {

        CloudFactory cloudFactory = new CloudFactory();
        Cloud cloud = cloudFactory.getCloud();
        DB2ServiceInfo db2ServiceInfo= (DB2ServiceInfo) cloud.getServiceInfo("db2servicenew"); 
        return cloud.getServiceConnector(db2ServiceInfo.getId(), DataSource.class, null);

    }

    @Bean(name = "db2JdbcTemplate") 
    public JdbcTemplate jdbcTemplate(DataSource db2servicenew) { 
        return new JdbcTemplate(db2servicenew); 
    } 

}

我还在我的 gradle 文件中添加了以下依赖项。

compile("org.springframework.cloud:spring-cloud-spring-service-connector:1.2.0.RELEASE")
compile("org.springframework.cloud:spring-cloud-cloudfoundry-connector:1.2.0.RELEASE")
compile("org.springframework.cloud:spring-cloud-core:1.2.0.RELEASE")

你能帮我解决这个问题吗?

4

1 回答 1

0

根据java.lang.ClassCastException: org.springframework.cloud.service.BaseServiceInfo cannot be cast to org.springframework.cloud.service.common.DB2ServiceInfo例外情况,在我看来,您没有获得您认为获得的连接器依赖项。

要确认这一点,请运行gradle :dependencies任务并查看您实际获得的版本。该任务输出的相关部分应如下所示:

+--- org.springframework.cloud:spring-cloud-spring-service-connector: -> 1.2.0.RELEASE
|    +--- org.springframework.cloud:spring-cloud-core:1.2.0.RELEASE
|    \--- org.springframework:spring-context:3.1.4.RELEASE -> 4.1.7.RELEASE (*)
+--- org.springframework.cloud:spring-cloud-cloudfoundry-connector: -> 1.2.0.RELEASE
|    \--- org.springframework.cloud:spring-cloud-core:1.2.0.RELEASE

如果我的预感是正确的,您可能会在输出中看到类似 的org.springframework.cloud:spring-cloud-spring-service-connector:1.2.0.RELEASE -> 1.1.1.RELEASE (*)内容,这表明 gradle 正在选择使用您根据其他传递依赖项要求它使用的旧版本。我已经看到 Spring Boot 应用程序会发生这种情况,因为 Boot 引入了旧版本的连接器。

如果您确实看到该版本降级,您应该查看为 gradle 引入Spring propdeps插件以直接获取依赖关系。

如果这不是问题,那么您将需要共享一个演示此问题的示例项目。

于 2016-02-24T16:20:34.243 回答