Cloud Foundry provides default buildpacks that come into play if no other buildpack is
specified. The Cloud Foundry buildpack has excellent support for Spring applications,
including Spring Boot. You can deploy stand-alone executable jar applications, as well as
traditional war
packaged applications.
Once you’ve built your application (using, for example, mvn clean install
) and
installed the cf
command line tool,
simply answer the cf push
command’s prompts as follows:
$ cf push --path target/demo-0.0.1-SNAPSHOT.jar Name> $YOURAPP Instances> 1 Memory Limit> 256M Creating $YOURAPP... OK 1: $YOURAPP 2: none Subdomain> $YOURAPP 1: cfapps.io 2: none Domain> cfapps.io Creating route $YOURAPP.cfapps.io... OK Binding $YOURAPP.cfapps.io to $YOURAPP... OK Create services for application?> n Bind other services to application?> n Save configuration?> y
At this point cf
will start uploading your application:
Saving to manifest.yml... OK Uploading $YOURAPP... OK Preparing to start $YOURAPP... OK -----> Downloaded app package (8.7M) -----> Java Buildpack source: system -----> Downloading Open JDK 1.7.0_51 from .../openjdk-1.7.0_51.tar.gz (1.4s) Expanding Open JDK to .java-buildpack/open_jdk (1.3s) -----> Downloading Spring Auto Reconfiguration 0.8.7 from .../auto-reconfiguration-0.8.7.jar (0.0s) -----> Uploading droplet (43M) Checking status of app $YOURAPP... 0 of 1 instances running (1 starting) 0 of 1 instances running (1 starting) 1 of 1 instances running (1 running) Push successful! App $YOURAPP available at http://$YOURAPP.cfapps.io
Note | |
---|---|
Here we are substituting |
Once Cloud Foundry acknowledges that your application has been deployed, you should be
able to hit the application at the URI provided:
http://$YOURAPP.cfapps.io/
.
By default, meta-data about the running application as well as service connection
information is exposed to the application as environment variables (for example:
$VCAP_SERVICES
). This architecture decision is due to Cloud Foundry’s polyglot
(any language and platform can be supported as a buildpack) nature; process-scoped
environment variables are language agnostic.
Environment variables don’t always make for the easiest API so Spring Boot automatically
extracts them and flattens the data into properties that can be accessed through
Spring’s Environment
abstraction:
@Component class MyBean implements EnvironmentAware { private String instanceId; @Override public void setEnvironment(Environment environment) { this.instanceId = environment.getProperty("vcap.application.instance_id"); } // ... }
All Cloud Foundry properties are prefixed with vcap
. You can use vcap properties to
access application information (such as the public URL of the application) and service
information (such as database credentials). See VcapApplicationListener
Javdoc for
complete details.
Tip | |
---|---|
The Spring Cloud project is a better fit for tasks such as configuring a DataSource; and you can also use Spring Cloud with Heroku too! |