Skip to content

Latest commit

 

History

History
 
 

livelessons-cloud-services

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Lesson 3: Reaching For The Clouds (Consume Cloud Services using Environment Variables)

Introduction

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.

Prerequisites

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.

Pushing the application to Cloud Foundry

To push the application type:

$ cf push andregs-ll-cc

Understanding the application

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.