Deploying Spring Boot Application Into Pivotal Cloud Foundry With MySQL’s Database

Cloud Foundry is promoted for continuous delivery as it supports the full application development lifecycle from initial development through all testing stages to deployment. Cloud Foundry’s container-based architecture runs apps in any programming language over a variety of cloud service providers. This multi-cloud environment allows developers to leverage the cloud platform that suits specific app workloads and move those workloads as necessary within minutes with no changes to the app.

Users have access to one or more spaces, which typically correspond to a lifecycle stage. For example, an application ready for QA testing might be pushed (deployed) to its project’s QA space. Different users can be restricted to different spaces with different access permissions in each. Developers often require an additional tool, the Cloud Foundry cf Command Line Interface. It is written in Go. Installers exist for MS Windows, MacOS, and Linux.

Setup account in Pivotal Cloud Foundry.

Use Pivotal Signup page to create the account

The Apps Manager displays after you create an organization (org). By default, PWS configures each new org with a space named development.

In the above screen, you can see the org and space sections.

“gopi-org” is the organization name

“Development” is the space name.

For trial accounts, the maximum memory is 2GB

Installing CLI (Command Line Interfaces):

Cloud Foundry CLI is the official command line client for Cloud Foundry. The latest help of each command is here (or run cf help); Further documentation is at the docs’ page for the CLI.

You can directly download and install CLI as a normal software from the following link

Command Line Interface download

To confirm your installation is a successful, type” cf help

A screen similar to the following will appear:

After the CLI installation, we need to login to CF pivotal.

To log in, use the following command:

cf login -a https://api.run.pivotal.io

It will ask you for email address and password. After a successful login, you will connect through your Pivotal account.

(Note: if the above command has not worked, try this

(cf login -a https://api.run.pivotal.io –skip-ssl-validation -o (your org-name) -s (your space name)

Cloud Foundry takes required properties from the manifest.yml file.

Sample manifest.yml file:


--
applications:
- name: adds
memory: 1G
instances: 1
random-route: true
services:
- nbos_db

In the above snippet, ‘adds’ is the application name which will be created in the Pivotal CF; memory is allocated for the application, the number of instances for application and provides a random-route to the Cloud Foundry.

Pivotal offers some services in the marketplace. ClearDB is one of them.

Instead of the manifest.yml file, we can directly write in a single command on CLI

-> cf push adds -i 1 -m 1G –random-route

Services:

In a platform, all external dependencies such as databases, messaging systems, files systems and so on are considered Services. Cloud Foundry allows administrators to create a marketplace of services, from which users can provision reserved resources on-demand. When an application is pushed to Cloud Foundry the services it needs may also be specified. This process puts the credentials in an environmental variable.

Creation of service:

We can create the service in two ways.

  1. Directly from the Pivotal marketplace (web-ui).

2) by running the commands of CLI

To see the list of available services, use command: cf marketplace

To create the service:
To see list of available services: cf services

cf create-service (SERVICE-NAME ) (PLAN-NAME) (GIVE-YOUR-NAME-TO-SERVICE)

Example:  cf create-service clearDb spark nbos_db

In the above manifest.yml file, nbos_db is the ‘my service name’ to ClearDB.

Deploying the Spring Boot project to Cloud Foundry:

A couple of modifications should be performed for uploading it, though.

Add auto-reconfiguration and in-memory database maven dependency to my pom.xml file


<dependency>
<groupId>org.cloudfoundry</groupId>
<artifactId>auto-reconfiguration</artifactId>
<version>1.7.0.RELEASE</version>
<scope>provided</scope>
</dependency>
  <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
 </dependency>

<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>http://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>pivotal-releases</id>
<name>Pivotal Releases</name>
<url>http://maven.gopivotal.com.s3.amazonaws.com/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>http://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories> 

Make sure that you also have maven dependency of MySQL.

To connect the MySQL database to the Spring-boot after its deployed into the cloud, we should perform some changes.
1. Change your database properties to the in-memory database properties as follows:


spring.datasource.url=jdbc:h2:mem:
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

Create the clearDb service with the name “nbos_db” (Pick a name of your choice) and add it to the services. After the deployment, CF automatically detects MySQL nbos_db and attaches to your application. We added the auto-reconfiguration maven dependency, it helps the Cloud Foundry to automatically detect the nbos_db properties (db username, password & db url). If we have multiple databases, in that case, Cloud Foundry may fail to do the auto-reconfiguration.

Go to your project location and do this

Cf push appname -p target/jar or war file name

And that’s it! Your project will be deployed into the Cloud Foundry.

After the successful deployment in the logs, you get the URL of your application.

You are ready to go into the market now!