简体   繁体   中英

Db2Service issue-spring Cloud foundry

I am having an issue while connecting db2 service in the cloud foundry. I have created the service as cf cups db2service -p "jdbcUrl,user,password" .While deploying the application on cloud I am facing a class cast exception and also no suitable connector found exception.The below is my configuration class.

@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); 
    } 

}

I have also added the below dependencies in my gradle file.

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")

Can you please help me with this issue.

Based on the java.lang.ClassCastException: org.springframework.cloud.service.BaseServiceInfo cannot be cast to org.springframework.cloud.service.common.DB2ServiceInfo exception, it appears to me that you aren't getting the Connectors dependencies you think you are getting.

To confirm this, run the gradle :dependencies task and see what versions you are actually getting. The relevant part of the output of that task should look something like this:

+--- 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

If my hunch is correct, you might instead see something in the output like org.springframework.cloud:spring-cloud-spring-service-connector:1.2.0.RELEASE -> 1.1.1.RELEASE (*) , indicating that gradle is choosing to use an older version that what you've asked it to based on other transitive dependencies. I've seen this happen with Spring Boot apps, as Boot pulls in an older version of Connectors.

If you do see that version downgrade, you should look at pulling in the Spring propdeps plugin for gradle to get the dependencies straight.

If this isn't the problem, then you will need to share a sample project that demonstrates this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM