Home » PCF » Deploy Spring Boot application to Pivotal Cloud Foundry

Deploy Spring Boot application to Pivotal Cloud Foundry

In this tutorial we will create a simple Spring Boot Application and after development of application we will deploy it to Pivotal Cloud Foundry(PCF). Lets see how we can do that.

Technology Stack

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

Generate Spring boot application

launch spring initializer website by visiting start.spring.io which is a great starting point for creating any spring boot based application.Provide the name of the project as "spring-boot-hello-world" which need to be provided against artifact. Select the spring boot version from drop down.Keep the project as Maven project. select spring web depency and click on generate button.Once we generate the project, one zip file will be downloaded, which we will simply import in eclipse after unzipping.

CF CLI


Import the project to eclipse as existing maven project. Once project got imported then maven will download the dependencies and setup class path entries for you.

Create REST Controller and Endpoint

We created a REST Controller Class file which contains three get request to test the deployment from cloud foundry.

package com.jwt.pcf.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.jwt.pcf.demo.model.HelloWorld;

@RestController
public class HelloWorldController {

	@GetMapping(path = "/hello")
	public String helloWorld() {
		return "Hello World - PCF ";
	}

	@GetMapping(path = "/hello-bean")
	public HelloWorld helloWorldBean() {
		return new HelloWorld("Hello World from Bean");
	}

	@GetMapping(path = "/hello/name/{name}")
	public HelloWorld helloWorldPathVariable(@PathVariable String name) {
		return new HelloWorld(String.format("Hello World, ", name));
	}

}

Create Bean class

package com.jwt.pcf.demo.model;

public class HelloWorld {

	private String message;

	public HelloWorld(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public String toString() {
		return "HelloWorld [message=" + message + "]";
	}

}

Spring Boot Main class

The main Spring Boot application class file is given below.

package com.jwt.pcf.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootHelloWorldApplication {

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

}

Test locally

Right click on the main class and start the application as spring boot application or java application. Once application starts then Go to browser and type http://localhost:8080/hello and we will receive response from the server in browser.

CF CLI


Now our application is ready to be pushed to pivotal cloud foundry. Let's see how we can deploy this simple spring boot application to PCF. We'll be using CF CLI(Cloud Foundry Command Line Interface) for the deployment.

Deploying Spring Boot application in Pivotal Cloud Foundry

As we have Cloud Foundry CLI already configured, we will use CLI cf push command to deploy the application in cloud foundry. Follow the steps mentioned below for the deployment.

Step 1 . Login to PWS Console

Open command prompt and navigate to our application’s home directory and use cf login -a api.run.pivotal.io. command to login to pivotal web service console.It will prompt for the registered credentials and after successfull validation it would say OK and it would directly target the organization what you created and space of development. Currently, "javawebrutorpcf" is the only org which is present and "development" is the only space which is associated with this org.

Note:- As we are going to deploy our application on PWS whose URL is api.run.pivotal.io. In real time project this URL could be your project PCF url.

PCF Deployment



If you type cf target, you would actually get all the details of what we are targeting against.So, it says we are targeting against this API endpoint, version ,user and the org and space are so and so as shown below.

PCF Deployment


If you want to change the org and space that you would want to use as target,you can easily do that by using command cf target -o and provide your org or -s and provide your space.

You can just type cf help target to get more details about the target command.You can see the options which are present in here.

PCF Deployment


Step 2 . Push Application to PCF

Now we can push the application by using command cf push.cf push is most important command in Pivotal Cloud Foundry.This is the command which we will use most number of times in PCF.

Just try to push the application using cf push command and see if it really works. No we got Error and it says, push command needs a application name.

PCF Deployment


Now please supply an application name and type the command cf push spring-boot-hello-world and see it worked or not?

No this would also result in an error.It says, The app cannot be mapped to route spring-boot-hello-world.cfapps.io because the route exists in a different space.

PCF Deployment


Now, what we are trying to do is, we are pushing an app called spring-boot-hello-world. What happens when we push an app is, a default route with the application name gets mapped to it,so,we can access that application by using this route. So, a route basically maps a URI to an application. Cloud Foundry in the background is creating a route but it's not able to do that because somebody else has already used this specific route in past.

Now, what we'll do is, we'll say, Hey,I don't really care about Route and assign something at random. We can use random-route parameter with cf push.Let's see what will happen now with cf push spring-boot-hello-world --random-route command.

PCF Deployment


Now you can see that the name is assigned, the route is assigned, and it's trying to create an app but oops again Error.Now, why did it fail?It tried to create an application with this name and it was trying to map a route to it and after that what it uploaded a few files and then you can see that it tried to download a number of build packs and it says, "None of the buildpacks detected a compatible application." What it tried to do is, it tried to upload whatever code is present in our local to Pivotal Cloud Foundry and it tried to see if it would be able to deploy it directly and it says, nope I'm not able to do that.I'm not able to find a build pack to deploy an application.

Now open the pom.xml of your application and you can see there we have defined this to be packaged as a JAR. What we have to do now is, we'll create a JAR of this application and then try to use that jar to deploy the application.

Now right-click on the project then Run As Maven Build and in next screen provide the goal as clean package and click on Run button.

PCF Deployment


Once the build is success you can see a jar file generated inside target folder. Now provide the fully qualified name of jar file in cf push command. This time no need of random-route because route is already assigned earlier. Type the command cf push spring-boot-hello-world -p target\spring-boot-hello-world-0.0.1-SNAPSHOT.jar

Now You'd see that it's actually downloading all the build packs. We'll talk about build packs a little later.You can see that this matched the Java buildpack. So, whatever you are setting in is a JAR and to deploy a JAR in Cloud Foundry, we will use a build pack, which is called Java Buildpack.It does a lot of things and it finally creates something called a droplet and the droplet is what is finally deployed on to the server and finally started the app.

PCF Deployment


step 3 . Verify Application Deployment

Login to your PWS console and check ifnewly deployed application is listed there. If everything went fine in the previous steps then, you can see your application there as shown below.You will see that there is one application running in our development space.

PCF Deployment


You can also see the route on which this application has been deployed. The Route is url where application has been deployed as highlighted. Test this URL from browser by clicking on the route. In my case it will be somethig like https://spring-boot-hello-world-forgiving-klipspringer.cfapps.io/.This URL will change based on the application name we choose.

PCF Deployment


Test REST Endpoint

Now open the browser and access the application with the url host published in the cf console. For this application url is https://spring-boot-hello-world-forgiving-klipspringer.cfapps.io/hello and https://spring-boot-hello-world-forgiving-klipspringer.cfapps.io/hello-bean.

PCF Deployment


PCF Deployment

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

So Finally we have successfully deployed spring boot application in Pivotal Cloud Foundry and successfully accessed it from our local workstation.For this demo we used PWS trial account which is more than sufficient for our demo application and to understand the PCF concept.

Whats Next

Let's explore Cloud Foundry concepts like apps,routes,start,stop,restart etc..

Previous Next Article

comments powered by Disqus