2016-07-15 36 views
0

我试图连接到在我的Cloudfoundry中定义的db2服务,并且它绑定到我的应用程序。db2作为Cloud Foundry中的服务

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

} 

低于资源路径2页的文件,我已经添加/ META-INF /服务

org.springframework.cloud.cloudfoundry.CloudFoundryServiceInfoCreator(File) 
org.springframework.cloud.cloudfoundry.DB2ServiceInfoCreator 

org.springframework.cloud.service.ServiceConnectorCreator (File) 
org.springframework.cloud.service.relational.DB2DataSourceCreator 

获得以下错误

2016-07-14T03:19:44.44-0400 [ APP/0] OUT 2016-07-14 07:19:44.444 WARN 14 --- [main] ocroscloud.AbstractCloudConnector:不适合 服务信息创建者发现服务db2-srvc您忘记添加 a Serv iceInfoCreator?

2016-07-14T03:19:44.51-0400 [APP/0] OUT 2016年7月14日07:19:44.510 INFO 14 --- [主要] nfigurationApplicationContextInitializer:添加云服务自动重新配置到ApplicationContext 2016-07-14T03:19:45.15-0400 [APP/0] OUT 2016-07-14 07:19:45.154 INFO 14 --- [main] ceconfig.ApplicationIntializer:Cloud profile active

。 ....

.....

org.springframework.beans.factory.BeanCreationException:错误创建豆名称为'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration':注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource;嵌套异常是org.springframework.beans.factory.BeanCreationException:在类路径资源[com/eclipselite/config/Db2CloudConfig.class]中定义名称为'db2service'的bean时出错:通过工厂方法的Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[javax.sql.DataSource]:工厂方法'db2service'抛出异常;嵌套异常是java.lang.ClassCastException:org.springframework.cloud.service.BaseServiceInfo不能转换为org.springframework.cloud.service.common.DB2ServiceInfo 2016-07-14T03:19:52.50-0400 [APP/0] OUT at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)〜[spring-beans-4.2.6.RELEASE.jar!/:4.2.6.RELEASE] 2016-07-14T03 :19:52.50-0400 [APP/0] OUT at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)〜[spring-beans-4.2.6.RELEASE.jar!/:4.2 .6.RELEASE] 2016-07-14T03:19:52.50-0400 [APP/0] OUT at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)〜[spring-beans- 4.2.6.RELEASE.jar!/:4.2.6.RELEASE] 2016-07-14T03:19:52.50-0400 [APP/0] OUT在org.springframework.beans.factory.support

......

......

:52.50-0400 [APP/0] OUT引起:java.lang.ClassCastException:org.springframework.cloud.service.BaseServiceInfo无法转换为org.springframework.cloud.service.common.DB2ServiceInfo 2016-07- 14T03:19:52.50-0400 [com/0] OUT at com.eclipselite.config.Db2CloudConfig.db2service(Db2CloudConfig.java:43)〜[app /:na] 2016-07-14T03:19:52.50-0400 [ APP/0] OUT at com.eclipselite.config.Db2CloudConfig $ EnhancerBySpringCGLIB $$ 95090e1d。CGLIB $ db2service $ 1()〜[app /:na] 2016-07-14T03:19:52.50-0400 [APP/0] OUT at com.eclipselite.config.Db2CloudConfig $ EnhancerBySpringCGLIB $$ 95090e1d $ FastClassBySpringCGLIB $$ 5de8a91d。 invoke()〜[app /:na] 2016-07-14T03:19:52.50-0400 [APP/0] OUT at org.springfr

+0

您是否在引用Bluemix上的“DB2 on Cloud”服务或其他方案?您的CF环境托管在哪里?请添加有关常规设置的详细信息 –

+0

我们的组织中有一个私有云,与Pivotal Cloud代工厂相关联。我们在同一个GRN中有db2数据库。我能够直接访问相同的db2。但是当我定义相同的用户定义服务获取上述错误。 –

回答

0

我想如果您使用serviceInfo创建,它会查找某些标签/在杯子里的属性。您可以使用connectionFactory.datasource(“cups服务名称”)创建数据源,也可以按照以下文档中的说明正确定义杯子。http://cloud.spring.io/spring-cloud-connectors/spring-cloud-cloud-foundry-connector.html#_db2

+0

欢迎来到堆栈溢出你的答案可能会更好。 [如何回答](http://stackoverflow.com/help/how-to-answer)可以帮助你写出将被接受和upvoted的答案。 – zhon