Pivotal Cloud Foundry Plus MySql DB

时间:2018-05-25 19:23:59

标签: spring-boot cloudfoundry pivotal-cloud-foundry pivotal-web-services

  1. 我在Pivotal Cloud Foundry上有一个正在运行的Web服务应用程序。
  2. 我已经为它添加了一个服务,即MySql DB,我将它与Web服务应用程序绑定。
  3. 我的问题是如何在(Spring-boot)中使用Java API Plus Cloud Foundry API获取My Sql数据库的数据源。
  4. 我尝试过的事情:

     public class Configuration {
            @Configuration
            @Profile("cloud")
            static class CloudConfiguration {
                @Bean
                public DataSource dataSource() {
                    CloudFactory cloudFactory = new CloudFactory();
                    Cloud cloud = cloudFactory.getCloud();
                    String serviceID = cloud.getServiceID();
                    return cloud.getServiceConnector(serviceID, DataSource.class, null);
                }
     }
    

    执行的方法:

    Configuration.CloudConfiguration cloudConfiguration = new CloudConfiguration();
            cloudConfiguration.dataSource();
    

    我得到错误:

    我从此代码中获取CloudException,因为找不到合适的CloudConnector,getCloud()方法抛出CloudException。

    清单文件详细信息:

    applications:
        - name: springboot-cloudfoundry
          memory: 512M
          instances: 1
          path: target/springboot-cloudfoundry-0.0.1-SNAPSHOT.jar
          buildpack: https://github.com/cloudfoundry/java-buildpack
          env:
            SPRING_PROFILES_ACTIVE : cloud
    
        POM File:
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-spring-service-connector</artifactId>
    
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-cloudfoundry-connector</artifactId>
    
            </dependency>
    
    Stack Trace:
    org.springframework.cloud.CloudException: No suitable cloud connector found
        at org.springframework.cloud.CloudFactory.getCloud(CloudFactory.java:55)
        at org.springframework.cloud.config.CloudScanHelper.initializeCloud(CloudScanHelper.java:85)
        at org.springframework.cloud.config.CloudScanHelper.registerServiceBeans(CloudScanHelper.java:55)
        at org.springframework.cloud.config.java.ServiceScanConfiguration.registerBeanDefinitions(ServiceScanConfiguration.java:22)
        at org.springframework.cloud.config.java.CloudScanConfiguration.registerBeanDefinitions(CloudScanConfiguration.java:22)
        at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:344)
        at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:151)
        at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:124)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:318)
        at org.springframework.ide.eclipse.metadata.process.JdtConfigurationClassPostProcessor.postProcess(JdtConfigurationClassPostProcessor.java:88)
        at org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig$3.run(BeansJavaConfig.java:328)
        at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
        at org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig.executePostProcessor(BeansJavaConfig.java:321)
        at org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig.access$5(BeansJavaConfig.java:319)
        at org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig$2.call(BeansJavaConfig.java:233)
        at org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig$2.call(BeansJavaConfig.java:1)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
    

1 个答案:

答案 0 :(得分:0)

1将以下依赖项添加到您的pom.xml

    <dependency>
        <groupId>io.pivotal.spring.cloud</groupId>
        <artifactId>spring-cloud-services-starter-config-client</artifactId>
    </dependency>

OR

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-spring-service-connector</artifactId>
    <version>1.2.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-cloudfoundry-connector</artifactId>
    <version>1.2.3.RELEASE</version>
</dependency>

2定义数据源:

public class CloudConfig extends AbstractCloudConfig {
        @Bean
        public DataSource dataSource() {
            DataSource dataSource = connectionFactory().dataSource();
            return dataSource;
        }
}

OR

public class CloudConfig extends AbstractCloudConfig {
        @Bean
        public DataSource inventoryDataSource() {
            return connectionFactory().dataSource("mysql-db-service");
        }
}