SpringBoot + DigitalOcean Droplets

GSSwain
7 min readJul 3, 2020

In this post, we’ll learn how to build and deploy a simple HelloWorld SpringBoot web app on a DigitalOcean Droplets (Virtual Machine). We’ll start by creating a SpringBoot web app, package that into a fat JAR (You can skip this step, if are familiar with SpringBoot). After this, we’ll create a systemd service file for running the web app. Then we create a Droplet, connect to that using SSH, configure the Droplet to run SpringBoot App, upload the artifacts and run and test our SpringBoot web app. Finally we clean up the resources.

Note: Bela is my friend and in this post we’ll all say “Hi, Bela!”

Table of Contents

  1. Building a HelloWorld SpringBoot Web App
  2. Systemd service file for running the Web App
  3. Create a DigitalOcean Droplet
    1. Create a key pair (We’ll use this for accessing the Droplet)
    2. Create a project (Optional step - Only if you don’t have one)
    3. Create a Droplet
  4. SSH into the Droplet
  5. Configure the Droplet for SpringBoot Web App
    1. Install JRE in the Droplet
    2. Copy Artifacts into the Droplet (JAR and systemd service file)
  6. Run and Test the SpringBoot Web App
    1. Run the SpringBoot Web App
    2. Test the SpringBoot Web App
  7. Clean up
  8. Resources

Building a HelloWorld SpringBoot Web App

Go to Spring Initializr, add the Spring Web dependency and click on GENERATE. As of today, the defaults on Spring Initializr are Project type (Maven), SpringBoot version (2.3.1), packaging(JAR), Java version (8). ( Also Change the group and artifact name if you would like to. I have used com.bela as my group name here)

We’ll add a /hello endpoint which returns the string “Hi, Bela!”. (I have added the controller in the DemoApplication.java. This can and should be in a different file)

@SpringBootApplication
public class DemoApplication {

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

@RestController("/")
class HelloWorld {
@GetMapping("hello")
public String hello(){
return "Hi, Bela!";
}
}
}

Run the project on local

./mvnw spring-boot:run

Test the /hello endpoint on localhost

Now we can build and package the project (This will create the fat JAR demo-0.0.1-SNAPSHOT.jar in target folder)

./mvnw clean package

Systemd service file for running the Web App

Create a hello-world-spring-boot.service file with the following content.

[Unit]
Description=Hello World Demo Spring Boot
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=/usr/bin/java -jar /artifact/demo-0.0.1-SNAPSHOT.jar
[Install]
WantedBy=multi-user.target

Note: Use a different user instead of root. Here root is used to keep this example simple. Also note the JAR absolute path which is /artifact. We’ll upload the JAR file into the /artifact directory of the Droplet. This folder name can be anything. You must ensure the JAR exists in the path we specify here.

Create a DigitalOcean Droplet

Droplets are essentially virtual machines (They are like AWS EC2 instances, Azure VMs, Google Compute Engine).

Create a key pair

Before we create a Droplet, let’s create a key pair to safely access the Droplet. I’ll be using ssh-keygen to generate the key pair and store them in a folder named key. (If you are on windows, you can follow the link in the resouces section below.)

ssh-keygen -t rsa -b 4096 -C “your-email@somedomain.com” -f ./key/id_rsa

At this point, your folder should look like this

Create a project

This step is optional. If you already have created a project, move to creating a Droplet.
If you don’t have any project, then create a new project.

Create a Droplet

Now we create a Droplet by selecting the OS distribution and version, Virtual machine plan, datacenter region, VPC Network, SSH Keys, Project. We keep everything default apart from selecting the region to Bangalore and adding our ssh keys for access control.

Copy the generated public key key/id_rsa.pub into the SSH key content section and provide a name for the key.

Make sure you select the correct project here, if you have more than one. Now click on Create Droplet.

We should see something like this where it shows the Droplet under the resources with the progress indicator in blue.

Once the Droplet is ready, take note of the IP address and copy it as per the below image. We’ll use this IP to execute the remaining steps.

SSH into the Droplet

You can use the private key to ssh into the Droplet. Open a terminal window and enter the following command. (Replace the IP in bold with the public IP of your Droplet)

ssh -i key/id_rsa root@<IP Address of the Droplet>

e.g. if I replace the IP placeholder with 157.245.100.221, then the command would look like

ssh -i key/id_rsa root@157.245.100.221

Configure the Droplet for SpringBoot Web App

Install JRE in the Droplet

Run the following commands (This needs to be run from the terminal we used to ssh into the Droplet)

sudo apt update
sudo apt install openjdk-8-jre-headless
java -version

The output of the java -version should look something like this.

Copy Artifacts into the Droplet

We’ll copy the JAR into /aritifact directory and copy the systemd service file to /etc/systemd/system directory

Create a directory named artifact in the root directory of the Droplet

mkdir /artifact

Now copy the JAR and the systemd service file into the Droplet. Open a new terminal window on your local system and run the following command (Remember to replace the IP placeholder in bold with the public IP of your Droplet)

scp -i key/id_rsa demo/target/demo-0.0.1-SNAPSHOT.jar root@<IP Address of the Droplet>:/artifactscp -i key/id_rsa hello-world-spring-boot.service root@<IP Address of the Droplet>:/etc/systemd/system

e.g. if I replace the IP placeholder with 157.245.100.221 the command would look like

scp -i key/id_rsa demo/target/demo-0.0.1-SNAPSHOT.jar root@157.245.100.221:/artifactscp -i key/id_rsa hello-world-spring-boot.service root@157.245.100.221:/etc/systemd/system

Run and Test the SpringBoot Web App

Run the SpringBoot Web App

Start the service (This needs to be run from the terminal we used to ssh into the Droplet)

systemctl start hello-world-spring-boot.service

Bonus (Optional Step):

You can make sure to bring the service up in case of a Droplet restart, by using the following command in the Droplet (Run in the ssh session).

systemctl enable hello-world-spring-boot.service

Test the SpringBoot Web App

Now let’s test our endpoint from within the ssh session

curl http://localhost:8080/hello

Seems to be working from the ssh session. Now let’s test using the browser using the public ip. (Use your Droplet IP instead of the one shown in the below image i.e. your URL would be http://<IP Address of the Droplet>:8080/hello)

Phew! now our spring boot service runs on the Droplet. Thanks for being patient and reading till this point. I hope this was helpful. Do not forget to read the clean up section to remove the resources we created (Otherwise you’ll be chraged for those resources).

Clean up

If you wanted to run this just for trying out DigitalOcean, do not forget to clean up the resources we created. Delete the Droplet, Delete the project.

For deleting the project, go to the project, then click on the Settings tab and scroll to the bottom of the page and click on Delete Project button.

Resources

--

--