Cloudfoundry convieniently manages service dependencies for the apps. This repo takes the example of injecting a datasource to explain different ways wiring services in the cloudfoundry.
mvn clean package && java -jar target/demo-0.0.1-SNAPSHOT.jar
The app will run locally with H2 in memory database configured as H2DataSourceConfig
http://localhost:8080/test would show some dummy string in database
There are four ways of injecting dependencies, in this example:
- EnvironmentDataConfig: Using environment variables with spring profile, parse-env
- ParseServiceDataConfig: Bind with cloudfoundry broked service and parse the VCAP_SERVICES env variables with spring profile, parse-vcap
- ParseUserDefinedDataConfig: Bind with cloudfoundry user defined service and parse the VCAP_SERVICES env variables with spring profile, user-defined
- SpringCloudDataConfig: Bind with cloudfoundry services with spring cloud framework. spring profile, spring-cloud
- The manifest file is using spring profile: "default"
cf push- Create a service with pivotal cf mysql services broker
cf create-service p-mysql plan test- Bind the service to the demo app
cf bind-service demo test- Find the database credentials for the demo app
cf files demo logs/env.logThe database information will be found in the VCAP_SERVICES env variables and user should connect to the database with any mysql client and run the test.sql to create data
By changing the manifest.yml SPRING_PROFILES_ACTIVE value, user is able to test and understand different strategies of the service dependencies.