Undocumented JAVA: Web Application: Deploy to the Cloud – Part 1

Introduction

Hey guys I’m, trying to start a series of steps to deploy a web application to the cloud.
This is part 1 of a four-part series. Part 1 will just create a simple spring boot application and deploy to the Heroku Cloud.
Part 2 consist of how we can make this application that we’d deployed onto the cloud be registered with a DNS you own.
Part 3 consist of how we are going to apply the application through the cloudflare and still access through our DNS.
Part 4 consist of how we can make the site into a secured site by enabling SSL to your site.
I said all this this is going to be given to you how it can be done with us minimum cost that we could speed up to get this happening.

Lets get started

To begin with, I assume that you are aware of Spring Boot and know how to run a simple Spring Boot application. If you can clone the code from https://github.com/reflexdemon/cloud-project then you may directly jump to Step 2a

Step 1

Not wasting much of our time, here is the quick run through of generating a Spring Boot application from https://start.spring.io and assembling it.

$ curl https://start.spring.io/starter.tgz -d dependencies=web \
-d language=java -d type=gradle-project -d baseDir=cloud-project | tar -xzvf -

$ cd cloud-project

Step 2

Let’s create a simple model that we would like to return when calling the rest service that we build. With your favorite editor create the below files and compile the code.
src/main/java/com/example/demo/Greetings.java
package com.example.demo;

public class Greetings {
 private final long id;
 private final String content;
 public Greetings(long id, String content) {
  this.id = id;
  this.content = content;
 }
 public long getId() {
  return id;
 }
 public String getContent() {
  return content;
 }
}

src/main/java/com/example/demo/GreetingController.java
package com.example.demo;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {
 private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

 @RequestMapping("/greeting")
     public Greetings greeting(@RequestParam(value="name", defaultValue="World") String name) {
 return new Greetings(counter.incrementAndGet(),
  String.format(template, name));
     }
}


Step 2a

Lets now compile and assemble the code.

$ ./gradlew assemble 
:compileJava
:processResources UP-TO-DATE
:classes
:findMainClass
:jar
:bootRepackage
:assemble

BUILD SUCCESSFUL
Total time: 1.095 secs

$ ls -ltr build/libs 
total 14204
-rw-rw-r-- 1 vpv vpv 2713 Feb 12 21:47 cloud-project-0.0.1-SNAPSHOT.jar.original
-rw-rw-r-- 1 vpv vpv 14540621 Feb 12 21:47 cloud-project-0.0.1-SNAPSHOT.jar

Step 3

Now let’s test the application.

$ java -jar build/libs/*.jar

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.10.RELEASE)

...
s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-02-12 21:54:00.014 INFO 17086 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.357 seconds (JVM running for 2.724)

Step 4

Test the application on your local.

$ curl -v localhost:8080/greeting 
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /greeting HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Tue, 13 Feb 2018 02:56:59 GMT
< 
* Connection #0 to host localhost left intact
{"id":1,"content":"Hello, World!"}

Congratulations! You have now successfully built the spring boot application on your local and deployed it on local. Lets now try to put that on the cloud.

Step 5

Now, lets go to our spring-cloud boot application and create a file called Procfile. Please note, the file is case sensitive and ensure to maintain the same case. Now, add the following to the Procfile:

web: java -Dserver.port=$PORT -jar build/libs/*.jar

This will tell Heroku about how this project needs to be executed.

Step 6

Now, it is time to get busy with deploying the code to cloud. Visit, https://signup.heroku.com/login and register to create a free account if you have not already created one.
Once you create your account and successfully login to your account, create a new app as shown below.
Note : You might have you create a unique name so that you reserve a DNS sub domain.

Step 6a


Install heroku command line (cli) from here.

Step 7

Now, you should be seeing something like this on your screen to be able to deploy the application that you created.
Just follow the steps provide by Heroku.
Here is a run through of the commands provided by Heroku.

$ heroku login
Enter your Heroku credentials:
Email: [email protected]
Password: ******************
Logged in as [email protected]

$ git init
Initialized empty Git repository in /tmp/cloud-project/.git/

$ heroku git:remote -a hero-boot
set git remote heroku to https://git.heroku.com/hero-boot.git

$ git add .

$ git commit -am "make it better"
[master (root-commit) 9fe20b4] make it better
12 files changed, 383 insertions(+)
create mode 100644 .gitignore
create mode 100644 Procfile
create mode 100644 build.gradle
create mode 100644 gradle/wrapper/gradle-wrapper.jar
create mode 100644 gradle/wrapper/gradle-wrapper.properties
create mode 100755 gradlew
create mode 100644 gradlew.bat
create mode 100644 src/main/java/com/example/demo/DemoApplication.java
create mode 100644 src/main/java/com/example/demo/GreetingController.java
create mode 100644 src/main/java/com/example/demo/Greetings.java
create mode 100644 src/main/resources/application.properties
create mode 100644 src/test/java/com/example/demo/DemoApplicationTests.java

$ git push heroku master
Counting objects: 28, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (28/28), 53.50 KiB | 0 bytes/s, done.
Total 28 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 

...

remote: :processResources
remote: :classes
remote: :findMainClass
remote: :jar
remote: :bootRepackage
remote: :assemble
remote: :check
remote: :build
remote: 
remote: BUILD SUCCESSFUL
remote: 
remote: Total time: 17.611 secs
remote: -----&gt; Discovering process types
remote: Procfile declares types ->; web
remote: 
remote: -----&gt; Compressing...
remote: Done: 62.8M
remote: -----&gt; Launching...
remote: Released v3
remote: https://hero-boot.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy.... done.
To https://git.heroku.com/hero-boot.git
* [new branch] master -> master
All I wanted to say is it will take 24-hrs to deploy your application… šŸ˜†.





Lookout for line remote: https://hero-boot.herokuapp.com/ deployed to Heroku.

No, actually you have already deployed the application to the cloud. Look at the logs from your last command where it says “deployed to Heroku”.
In my example, it was deployed to https://hero-boot.herokuapp.com/.

Step 8

Test your application.

$ curl -v https://hero-boot.herokuapp.com/greeting
* Trying 23.23.251.76...
* Connected to hero-boot.herokuapp.com (23.23.251.76) port 443 (#0)
* found 148 certificates in /etc/ssl/certs/ca-certificates.crt
* found 604 certificates in /etc/ssl/certs
* ALPN, offering http/1.1
* SSL connection using TLS1.2 / ECDHE_RSA_AES_128_GCM_SHA256
* server certificate verification OK
* server certificate status verification SKIPPED
* common name: *.herokuapp.com (matched)
* server certificate expiration date OK
* server certificate activation date OK
* certificate public key: RSA
* certificate version: #3
* subject: C=US,ST=California,L=San Francisco,O=Heroku\, Inc.,CN=*.herokuapp.com
* start date: Wed, 19 Apr 2017 00:00:00 GMT
* expire date: Mon, 22 Jun 2020 12:00:00 GMT
* issuer: C=US,O=DigiCert Inc,OU=www.digicert.com,CN=DigiCert SHA2 High Assurance Server CA
* compression: NULL
* ALPN, server did not agree to a protocol
> GET /greeting HTTP/1.1
> Host: hero-boot.herokuapp.com
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 
< Server: Cowboy
< Connection: keep-alive
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Tue, 13 Feb 2018 03:22:53 GMT
< Via: 1.1 vegur
< 
* Connection #0 to host hero-boot.herokuapp.com left intact
{"id":1,"content":"Hello, World!"}

Conclusion

This is the time to celebrate. Your first spring boot application has now been deployed to the cloud. Feel free to comment on this blog and I will get back to you with my next part showing how you can move this application to your custom domain.

Comments

Popular posts from this blog

Java SSL/TLS Testing Tool: Cipher Suite

Developing Spring Boot Application using Docker - Part 1