Db2Service问题 - spring Cloud Foundry

时间:2016-02-20 11:36:38

标签: spring-boot spring-cloud spring-cloud-connectors

我在云代工厂连接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")

请你帮我解决这个问题。

1 个答案:

答案 0 :(得分: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引入旧版本的连接器。

如果您确实看到该版本降级,您应该考虑使用gra Spring propdeps插件来直接获取依赖项。

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

相关问题