Get Started with CloudCaptain & Spring Boot

 

This tutorial will get you started with CloudCaptain and Spring Boot. It should take you about 5-10 minutes to complete.

Prerequisites

Before you begin, ensure you have successfully:

  1. created a CloudCaptain Account (simply log in with your GitHub account, it's free)
  2. downloaded and installed the latest CloudCaptain Client
  3. downloaded and installed the latest version of VirtualBox
  4. downloaded and installed the latest JDK with JAVA_HOME set up correctly

Creating the Spring Boot application

Creating a new Spring Boot app is incredibly easy. The Spring Boot website provides a simple service at start.spring.io to create application skeletons directly via http.

Option 1: Download & extract

To create the application for this post, you can either click to generate the application and extract it.

Option 2: Use curl

Alternatively, you can also type this one-liner:

> curl "https://start.spring.io/starter.zip?baseDir=getstarted-springboot&groupId=boxfuse-getstarted&artifactId=getstarted-springboot&version=1.0&name=getstarted-springboot&description=Get+Started+with+CloudCaptain+and+Spring+Boot+guide&packageName=getstarted.springboot&type=maven-project&packaging=jar&javaVersion=1.8&language=java&bootVersion=1.2.7.RELEASE&style=web&style=actuator&generate-project=" > getstarted-springboot.zip && unzip getstarted-springboot.zip

Adding a controller

Now change to the newly created directory:

> cd getstarted-springboot

To be able to interact with the application you'll need a controller, so go ahead and create one as getstarted.springboot.HelloController with the following contents:

package getstarted.springboot;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public Map<String, Object> hello(@RequestParam(value = "name", defaultValue = "CloudCaptain") String name) {
        Map<String, Object> result = new HashMap<>();
        result.put("greeting", "Hello " + name + "!");
        return result;
    }
}

Once that's done, you will have the following structure:

 getstarted-springboot
   .mvn
   src
     main
       java
         getstarted
           springboot
             GetstartedSpringbootApplication.java
             HelloController.java
       resources
         static
         templates
         application.properties
     test
       java
         getstarted
           springboot
             GetstartedSpringbootApplicationTests.java
   mvnw
   mvnw.cmd
   pom.xml

Your application is now fully set up. As your skeleton also includes Spring Boot's actuator (Spring Boot's turn-key production readiness features), you now have two urls available:

  • / : your controller
  • /health : Spring Boot's health check page

Go ahead and build it:

getstarted-springboot> mvnw package

Great. Your Spring Boot application is now available under target/getstarted-springboot-1.0.jar.

Fusing a CloudCaptain image and running it locally on VirtualBox

Now it's time to fuse your application into a CloudCaptain image and launch an instance of it on VirtualBox:

getstarted-springboot> boxfuse run

Fusing Image for getstarted-springboot-1.0.jar ...
Image fused in 00:06.915s (54656 K) -> myuser/getstarted-springboot:1.0
Launching Instance of myuser/getstarted-springboot:1.0 on VirtualBox ...
Forwarding http port localhost:10080 -> vb-b18d6746:80
Instance launched in 00:03.331s -> vb-b18d6746
Waiting for Payload to start on Instance vb-b18d6746 ...
Payload started in 00:06.688s -> https://127.0.0.1:10080

CloudCaptain has automatically detected the Spring Boot Actuator and used the /health endpoint to check whether the instance came up correctly.

Open a browser at this address to see your new application up and running within the VirtualBox VM by simply executing:

getstarted-springboot> boxfuse open

You can also see your newly created image:

getstarted-springboot> boxfuse ls

Images available locally:
+----------------------------------+-------------------------------+-------+---------+-------------+------------+---------+---------------------+
| Image                            |            Payload            | Debug |  Java   |  AppServer  |   Ports    |  Size   |    Generated at     |
+----------------------------------+-------------------------------+-------+---------+-------------+------------+---------+---------------------+
| myuser/getstarted-springboot:1.0 | getstarted-springboot-1.0.jar | false | 8.60.22 | Spring Boot | http -> 80 | 54656 K | 2015-11-12 14:57:50 |
+----------------------------------+-------------------------------+-------+---------+-------------+------------+---------+---------------------+
Total: 1

As well as the instance that is running:

getstarted-springboot> boxfuse ps

Running Instances on VirtualBox in the dev environment :
+-------------+----------------------------------+---------------------+------------------------+---------------------+
|  Instance   |              Image               |        Type         |           URL          |     Launched at     |
+-------------+----------------------------------+---------------------+------------------------+---------------------+
| vb-b18d6746 | myuser/getstarted-springboot:1.0 | 4 CPU / 1024 MB RAM | https://127.0.0.1:10080 | 2015-11-12 14:57:56 |
+-------------+----------------------------------+---------------------+------------------------+---------------------+
Total: 1

Deploying your application to AWS

Now let's deploy the image to AWS. As CloudCaptain works with your AWS account, it first needs the necessary permissions to do so. So if you haven't already done it, go to the CloudCaptain Console and connect your AWS account now.

Every new CloudCaptain account comes with 3 environments: dev, test and prod. dev is your local VirtualBox environment and test and prod are on AWS.

So let's deploy our application to the prod environment on AWS:

getstarted-springboot> boxfuse run -env=prod

Pushing myuser/getstarted-springboot:1.0 ... Verifying myuser/getstarted-springboot:1.0 ... Waiting for AWS to create an AMI for myuser/getstarted-springboot:1.0 in eu-central-1 (this may take up to 50 seconds) ... AMI created in 00:34.394s in eu-central-1 -> ami-ed988b81 Creating Elastic IP ... Mapping getstartedspringboot-myuser.boxfuse.io to 52.28.107.167 ... Creating security group boxsg-myuser-prod-getstarted-springboot-1.0 ... Launching t2.micro instance of myuser/getstarted-springboot:1.0 (ami-ed988b81) in prod (eu-central-1) ... Instance launched in 00:26.922s -> i-5de042e1 Waiting for AWS to boot Instance i-5de042e1 and Payload to start at https://52.28.121.55/health ... Payload started in 00:51.516s -> https://52.28.121.55/health Remapping Elastic IP 52.28.107.167 to i-5de042e1 ... Waiting 15s for AWS to complete Elastic IP Zero Downtime transition ... Deployment completed successfully. myuser/getstarted-springboot:1.0 is up and running at https://getstartedspringboot-myuser.boxfuse.io/

Notice that CloudCaptain has reused your image unchanged instead fusing a new one.

With that one command CloudCaptain has automatically pushed your image to the CloudCaptain Vault as well as provisioned, configured and secured all necessary AWS resources. There is no manual work necessary on your behalf.

All you need to do is simply navigate to your new domain to see your Spring Boot application in action on AWS:

Bonus: update your application with zero downtime

Now let's take things one step further and deploy an update of your application with zero downtime using blue/green deployments.

Start by modifying getstarted.springboot.HelloController and change the message:

result.put("greeting", "Updated by CloudCaptain with zero downtime!");

then bump the version in pom.xml:

<version>1.1</version>

and rebuild the jar:

getstarted-springboot> mvnw clean package

Finally deploy the new version of your application to AWS:

getstarted-springboot> boxfuse run -env=prod

Fusing Image for getstarted-springboot-1.1.jar ... Image fused in 00:06.857s (54656 K) -> myuser/getstarted-springboot:1.1 Pushing myuser/getstarted-springboot:1.1 ... Verifying myuser/getstarted-springboot:1.1 ... Waiting for AWS to create an AMI for myuser/getstarted-springboot:1.1 in eu-central-1 (this may take up to 50 seconds) ... AMI created in 00:41.700s in eu-central-1 -> ami-48afbc24 Creating security group boxsg-myuser-prod-getstarted-springboot-1.1 ... Launching t2.micro instance of myuser/getstarted-springboot:1.1 (ami-48afbc24) in prod (eu-central-1) ... Instance launched in 00:27.066s -> i-f40fad48 Waiting for AWS to boot Instance i-f40fad48 and Payload to start at https://52.29.60.117/health ... Payload started in 00:55.363s -> https://52.29.60.117/health Remapping Elastic IP 52.29.96.95 to i-f40fad48 ... Waiting 15s for AWS to complete Elastic IP Zero Downtime transition ... Terminating instance i-5de042e1 ... Destroying Security Group sg-032a576a ... Deployment completed successfully. myuser/getstarted-springboot:1.1 is up and running at https://getstartedspringboot-myuser.boxfuse.io/

And there it is:

Summary

In this brief guide we have seen how to:

  • create a Spring Boot application
  • fuse it into a CloudCaptain image
  • deploy the image locally on VirtualBox
  • deploy the image unchanged to AWS
  • update the application with zero downtime

Now it's your turn. Take your favorite Spring Boot application and deploy it with ease and pleasure.

And don't forget, CloudCaptain also comes with a Maven and a Gradle plugin to seamlessly integrate with your CI/CD workflow.

CloudCaptain Spring Boot Documentation