Skip to content

Latest commit

 

History

History
 
 

spring-boot-jounral-cloud

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Spring Boot in the Cloud : Cloud Foundry

You can use the cloud-connectors as well and get the DataSource.

###pom.xml

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

Code

package com.apress.spring.config;

import javax.sql.DataSource;

import org.springframework.cloud.config.java.AbstractCloudConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("cloud")
public class CloudDataSourceConfig extends AbstractCloudConfig {
    
	@Bean
    public DataSource dataSource() {
        return connectionFactory().dataSource();
    }
}

Runing local (no Cloud Foundry) emulate cloud profile

VCAP_APPLICATION

{
    "application_name": "journal",
    "application_uris": [""],
    "application_version": "1",
    "host": "localhost",
	"port": 8080,
    "instance_id": "abcd",
    "instance_index": 0,
    "name": "journal",
    "version": "1.0"
}

VCAP_SERVICES

{
    "p-mysql": [
        {
            "credentials": {
                "uri": "jdbc:mysql://localhost/journal?user=spring&password=springboot"
            },
            "label": "MySQL Service",
            "name": "p-mysql",
            "plan": "100mb-dev",
            "tags": [
                "mysql"
            ]
        }
    ]
}