-1
  1. I have a running web-service application on Pivotal Cloud Foundry.
  2. I have added a service to it which is MySql DB and I bind it with web service application.
  3. My Question is how to get the data source of My Sql db with Java API Plus Cloud Foundry API in (Spring-boot).

Things I have tried:

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

Method Executed:

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

Error I get:

I get CloudExceptionfrom this code like no suitable CloudConnector is found, the getCloud() method throws a CloudException.

Manifest File Details:

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)
IMNash
  • 25
  • 1
  • 1
  • 9
  • The `no suitable CloudConnector is found` message typically means that the Spring Cloud Connector code doesn't see the `VCAP_APPLICATION` and `VCAP_SERVICES` environment variables. The `getCloud() method throws a CloudException` message is unusual. Is there a stack trace to go with that exception? – Scott Frederick May 25 '18 at 20:13
  • Stack Trace Updated. – IMNash May 25 '18 at 20:43
  • How to set these variables VCAP_APPLICATION and VCAP_SERVICES and I want fetch it from cloud like we get it from CLI commands. Is there a way to do it and I don't want to put them in properties file, – IMNash May 25 '18 at 21:11
  • When the app is run on CF, these two env vars should be set automatically. If you are trying to test the app locally in the same way it would be run on CF, you'll need to manually set them in the app environment. – Scott Frederick May 31 '18 at 21:35
  • What CF flavor are you using? Pivotal Cloud Foundry, Blue Mix, SAP, Suse, OSS? – K.AJ Jun 09 '18 at 13:28

1 Answers1

0

1 Add below dependencies to your 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 Define datasource:

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