SpringBoot + DigitalOcean Droplets

  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

@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!";
}
}
}
./mvnw spring-boot:run
./mvnw clean package

Systemd service file for running the Web App

[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

Create a DigitalOcean Droplet

Create a key pair

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

Create a project

Create a Droplet

SSH into the Droplet

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

Configure the Droplet for SpringBoot Web App

Install JRE in the Droplet

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

Copy Artifacts into the Droplet

mkdir /artifact
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
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

systemctl start hello-world-spring-boot.service
systemctl enable hello-world-spring-boot.service

Test the SpringBoot Web App

curl http://localhost:8080/hello

Clean up

Resources

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store