This example shows how you can bind services from you cloud provider to Spring using environment variables. Here we are using Cloud Foundry but similar principles can be used for any cloud provider.
You will need a Cloud Foundry account and the
cf
command line tool installed. You
will also need to create two services named ll-car-sql-database
and
ll-car-sql-database2
using the following commands:
$ cf create-service elephantsql turtle ll-car-sql-database
$ cf create-service elephantsql turtle ll-car-sql-database2
Note
|
Cloud Foundry is clever enough to automatically wire a single datasource into your application. We create and bind two databases to prevent this auto-reconfiguration to show how environment variables can be used. See manifest.yml for the binding details. |
The application.yml
defines a
database URL that references the environment variable:
spring:
datasource:
url: ${cloud.services.ll-car-sql-database.connection.jdbcurl}
We also need to ensure that pooling is not enabled since the free Database tier doesn’t allow multiple connections:
spring:
datasource:
tomcat:
max-active: 1
You can also browse to /actuator/env
endpoint to inspect the environment variables,
including the ones with connection information to our bound services.