How to Deploy Spring Boot Application to Cloud Foundry Platform

Now a days Cloud Computing and Microservice have become very popular concept and almost all the organizations are investing and adapting it very fast. Currently there are only few popular cloud providers in the market and Cloud Foundry is one of them. It is a PaaS service where we can easily deploy and manage our applications and the Cloud Foundry will take care of the rest of the cloud based offerings like scalability, high availability etc.

Today we will learn to deploy spring boot application in cloud foundry starting from setting up cloud foundry in local workstation. There are many cloud foundry distributions currently available and in this article, we will mainly concentrate on the Pivotal Cloud Foundry platform called as Pivotal Web Services.

Table of Contents

What is Cloud Foundry
Pivotal Cloud Foundry Installation for Windows
Setup PWS Console
Create Spring Boot Application
Deploy Spring Boot Application in Cloud Foundry Platform
Summary

What is Cloud Foundry

Cloud Foundry is an open-source platform as a service (PaaS) that provides you with a choice of clouds, developer frameworks, and application services. It is open source and it is governed by the Cloud Foundry Foundation. The original Cloud Foundry was developed by VMware and currently it is managed by Pivotal, a joint venture company by GE, EMC and VMware.

Now since Cloud Foundry is open source product many popular organizations currently provides this platform separately and below are the list of current certified providers.

  • Pivotal Cloud Foundry
  • IBM Bluemix
  • HPE Helion Stackato 4.0
  • Atos Canopy
  • CenturyLink App Fog
  • GE Predix
  • Huawei FusionStage
  • SAP Cloud Platform
  • Swisscom Application Cloud

Cloud Foundry Installation for Windows

Here are the installation steps for Windows, for other operating system, cloud foundry has a very good documentation which we can easily follow.

The cloud foundry works very well from command prompt and cloud foundry has provided one command line tool called cf which does almost all the activities for us. So to make this tool (cf command) available in local workstation, first we need to install and configure the Cloud Foundry Command line (CLI) interface.

  1. Download the CF Windows installer. It will prompt for the download. Save the zip file distribution.
  2. Unpack the zip file to a suitable place in your workstation.

    Unzip Installer
  3. After successfully unzip operation, double cick on the cf CLI executable.

    CF Installer Location
  4. When prompted, click Install, then Close. Here are the sample steps for the same. This is very straight froward, you can select the default values.
    Step 1
    Step 2
    Step 3

    Step 4
  5. Verify the installation by opening a terminal window and type cf. If your installation was successful, the cf CLI help listing appears. This indicates that you are ready to go with any cloud foundry platform from your local workstation.

    Cloud Foundry Installation Verification

We will now proceed with Pivotal Web service account sign up and development of a sample application and push to cloud foundry.

Setup PWS Console

Now we need to create one account in pivotal in order to deploy our application in Pivotal Cloud Foundry Platform. We need to register in the below page to start with the sign up process. It is free and it will just ask some very common things like email address, name etc.

Pivotal web service console signup

Once sign up is completed, we can log into the console through the log in screen of the pivotal web service console.

After providing logon credentials successfully we will get into the cloud foundry console where we can see all the deployed applications, can monitor the applications and do many more activities. Here we need to add org and space etc. which is very straight forward and self-describing. Below is one sample console screen after login.

Currently no applications are deployed as we have not yet pushed any application.

Console of PWS after login

Login and logout from PWS Console using CLI

  1. Login to PWS – We will use cf login -a api.run.pivotal.io command to login to pivotal web service console from CLI tool that we have installed in our local workstation. It will logon the CLI tool to PWS platform so that we can deploy and manage our applications from our workstation. After giving command, it will ask for registered email and password and once provided successfully, it will logon to the platform.
  2. Logout from PWS Console – We will use command cf logout to logout from the platform, once we have all the work done for that session.
//To login

>> cf login -a api.run.pivotal.io

//To logout

>> cf logout

Here is the login and logout looks like from command prompt.

Login logout with CF

Create Spring Boot Application

We will now create one Spring boot application and will deploy to PWS console and access from Cloud Foundry itself. We will create an application which will expose one simple REST endpoint, which we will test from our workstation once deployed in Pivotal Web Service Platform.

Technology Stack

We will use below technology stack for the spring boot application development and testing.

  • Spring Boot
  • Spring REST
  • Maven
  • Eclipse
  • Cloud Foundry CLI
  • Web Browser

Generate Spring boot application

Start with spring boot initializer portal which is a great starting point for creating any spring boot based application. Here we will choose only Config server starter pom. The screen shot is something like this. With this configuration, once we generate the project, one zip file will be downloaded, which we will simply import in eclipse after unzipping.

Spring Boot Project Generation

Import the project to eclipse as existing maven project. Let maven download the dependencies and setup class path entries for you.

Add REST Controller and Endpoint

We need to add simple REST endpoint to test the deployment from cloud foundry. Open boot application class that has already provided by the automated project generation SpringHelloworldCfApplication.java and add the below lines to add one simple endpoint which will just echo something based on the input.

Final class will look like below.

package com.example.howtodoinjava.springhelloworldcf;

import java.util.Date;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class SpringHelloworldCfApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringHelloworldCfApplication.class, args);
	}
}

@RestController
class MessageRestController {

	@RequestMapping("/hello")
	String getMessage(@RequestParam(value = "name") String name) {
		String rsp = "Hi " + name + " : responded on - " + new Date();
		System.out.println(rsp);
		return rsp;
	}
}

Project Configuration

Add Context path and required properties in bootstrap.properties file in src\main\resources directory and add two properties there.

server.contextPath = /hello
management.security.enabled = false

This will set one context path /hello for the application and management.security.enabled=false will disable security for management endpoints of spring boot like /env, /refresh etc.

Test locally

Finally build and test the application in Local in an embedded tomcat container. To do this, start the application as spring boot application.

Go to browser and type http://localhost:8080/hello?name=howtodoinjava. It should echo the name along with some greeting message and response process time.

REST API Output

Now we will push [deploy] the application in the pivotal cloud foundry where we have registered already.

Deploy Spring Boot Application in Cloud Foundry Platform

As we have Cloud Foundry CLI already configured, we will use CLI cf push command to deploy the application in cloud foundry console.

Login to PWS Console

To do that open command prompt and go to maven application’s home directory and use cf login -a api.run.pivotal.io command to login to pivotal web service console.

It will ask for the registered credentials and finally log on to the console.

Push Application to Console

Now we need to push the application with the command cf push.

cf push spring-helloworld-cf -p target\spring-helloworld-cf-0.0.1-SNAPSHOT.jar

This will deploy the application to the already logged in PWS console from the previous step.

Deployment by cf push

Read the full console log for push command in attached log file.

Verify Application Deployment

Verify into PWS console to check that the newly deployed application is showing up. If everything went fine in the previous steps then, screen will look like this.

Application deployed in PWS – count increased.

Now click on the Apps section highlighted in the previous step to go to the application details screen. Below view will be shown and it will show the url where application has been deployed as highlighted. Note this url to test it from browser. In this case it will be somethig like https://spring-helloworld-cf.cfapps.io/. This URL will change based on the application name we choose.

Application URL published in PWS console.

Test REST Endpoint

Now to the browser and access the application with the url host published in the cf console. For this application url is https://spring-helloworld-cf.cfapps.io/hello?name=howtodoinjava.

Application accessed from cf directly

Congratulations !! You have successfully deployed your first spring boot application into Pivotal Cloud Foundry Platform.

Summary

So we have successfully able to develop and deploy one spring boot application in Pivotal Cloud Foundry console and able to access the same from our local workstation. Also we did the cf CLI configuration and registered in PWS trial account.

That’s all about this topic. I suggest you to now deploy your own application on cloud foundry and test.

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

19 thoughts on “How to Deploy Spring Boot Application to Cloud Foundry Platform”

  1. I’m getting like this

    Downloaded app package (16.8M)
    ERROR: No buildpack groups passed detection.
    ERROR: Please check that you are running against the correct path.
    ERROR: failed to detect: no buildpacks participating
    /tmp/buildpacks/0d49cc2555a25d193418ce9b540125de/bin/detect:
    line 3: /tmp/buildpacks/0d49cc2555a25d193418ce9b540125de/bin/buildpack: No such file or directory
    None of the buildpacks detected a compatible application
    Exit status 222
    Error staging application: An app was not successfully detected by any available buildpack

    TIP: Use ‘cf buildpacks’ to see a list of supported buildpacks.
    FAILED

    what to do?

  2. Hi Sajal Chakraborty, thank you for the helpful article. CLear and straight. Managed to deploy my first CF App

    Thanks

  3. Great example! How did you configure CLI on windows? Did you set the environment variables in order to run the cf commands from command line? Appreciate it if you can share this info please.

  4. Hi Lokesh,

    I am getting below error while deploying.

    The app cannot be mapped to route spring-helloworld-cf.cfapps.io because the rou
    te exists in a different space.

    Regards
    Nihal

    • I had the same error. To resolve it do the following :
      > cf push *name of org in PWS* -p target/spring-helloworld-cf-0.0.1-SNAPSHOT.jar

      To know name of your org execute below command
      > cf target

  5. I am getting an error

    D:\spring_latest_workspace\SpringBootCrudRestful>cf push SpringBootCrudRestful -p target\SpringBootCrudRestful-0.0.1-SNAPSHOT.jar
    No org targeted, use ‘cf target -o ORG’ to target an org.
    FAILED

  6. I am getting error – The app cannot be mapped to route spring-helloworld-cf.cfapps.io because the route is not in this space. Apps must be mapped to routes in the same space.

  7. Thank You for Example!!

    But I am getting below error when I tried to deploy it

    The app cannot be mapped to route spring-helloworld-cf.cfapps.io because the route is not in this space. Apps must be mapped to routes in the same space.

    • You need to run using your snapshot like… And also important steps is missing here.. You need to create the package using maven..

      1) Install maven – run this command. “mvn clean install” or else from eclipse/sts use run as -> maven install.

      2) cf push target/spring-helloworld-cf-0.0.1-SNAPSHOT.jar

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.