This article is a continuation of REST Part 1 article, where I developed a small Spring REST application using Spring Boot and searched Twitter for trends found in a location, using Twitter4J API.
Please go through the application here : Spring Boot REST
In this article, I would deploy the same application in cloud. I have chosen Heroku as my cloud-of-choice.
I would follow the steps given below :
- Create a free Heroku account
- Download and install Heroku Toolbelt for Windows
- Prepare and push the Spring Boot App for Heroku
- Test the application
Step – 1
This is simple. Just go to https:www.heroku.com and signup for a free account. A free account is pretty basic, but, sufficient for this article. Heroku provides various pricing options like Free, Hobby, Standard and Performance:
You would notice that, the compute power is specified in something called a “DYNO”. A Heroku dyno is a lightweight Linux container that runs a single user-specified command. E.g, for my free account, I get 1 dyno with the following limitations : 512 MB RAM, 1 Web Dyno, 1 Worker Dyno, the container would sleep after 30 minutes of inactivity and it must sleep for 6 hours within a 24 hours cycle.
A Web Dyno is a process type that is defined in a file called “Procfile”. For our application, we would shortly built a Procfile.Only web dynos receive HTTP traffic from Heroku’s routers.
A Worker Dyno can be of any process type declared in theProcfile, other than “web”. Worker dynos are typically used for background jobs, queueing systems, and timed jobs. You can have multiple kinds of worker dynos in anapplication.
Please note that, the beauty of Heroku is that, it provides , what is known as a PaaS (Platform-As-A-Service). So, we end up having a Java HTTP container like tomcat/jetty which is capable of taking part in HTTP request-response cycles and even databases like PostGRESQL as a backend. So, all we need to do is build the application and then push the codebase from GIT / Maven to Heroku and we would end up having our application in cloud.
The other option would have been AWS EC2 (Amazon Web Service – Elastic Compute Cloud). Unlike, PaaS, EC2 provides, what is known as IaaS (Infrastructure-As-A-Service).
Simply put, the difference is that EC2 would provide the components necessary to build the environment of our choice, while Heroku(PaaS) would have the environment ready-made – But, at the cost of power and flexibility. For an IaaS like EC2, we would need to have a load-balancer sitting on top of a HTTP container like nginx and then configure and deploy DBs etc … But, it is worth mentioning that AWS does have a PaaS offering called Elastic Beanstalk, but, we would keep this debate out of our head for a while and come back to this article !
To summarize, PaaS, sort of, sits in the middle of a cloud stack, between IaaS and SaaS(Software-As-A-Service).With IaaS, we get robust provisioning, storage options, traffic routers and load balancers. With PaaS, we depend on the provider for such configuration and get a robust container with multi-language support (Java, Python, PERL, SCala etc ..), database (PostGRESQL etc ..) and with SaaS (like SalesForce) we only need to worry about the number of users and the entire application is already pre-built in the cloud.
To be honest, the debate whether one should select an IaaS(EC2) vs PaaS(Heroku) is ever raging, and I request you to take google’s help in finding out more about the differences and enrich your knowledge.
So, we end step – 1 with a free heroku account created. Let’s move on.
Install Heroku Toolbelt for Windows 64 bit, which is akin to a Heroku Command Line Interface (CLI).
The link is : Heroku Toolbelt For Windows
I just made sure that I did not install it in the default “C:\Program Files …”, instead I installed it in a folder name with no space in it. It would install Heroku CLI, Ruby and GIT.
For this article, I would push the same Spring Boot Twitter application that was built in Part 1.
Copying the source from GitHub repo :
Then imported this project in my Eclipse workspace. Changed folder to the root of this project.
Then typed “heroku login” and the Heroku CLI fired up and asked for my heroku account credentials, as below.
Step – 3
Before the Spring Boot twitter application can be deployed in Heroku, a “Procfile” needs to be created. The procfile tell heroku if the deployed application is a web application or a java process that needs to run etc …
So, in a text editor just enter the following line and save the file as “Procfile” at the root directory of the project.
web: java -Dserver.port=$PORT -jar target/twitter-boot-0.0.1-SNAPSHOT.jar
Notice, how we take advantage of the Spring Boot fat jar created as a result of a “mvn clean install”. Spring Boot, this way, has become an ideal fit for deploying on cloud.
In order to deploy to Heroku, we need to create a new Heroku app, by running the command :
“heroku create” in the root project directory.
Now, we can deploy the code by issuing a “git push heroku master” command. There would be a flurry of activities in the command promt. But, let’s just focus on some of the important steps of what’s really happening, under the hood.
The “heroku create” command essentially creates a remote repository in the local GIT repo. A random name is generated (for my case it is called : https://ancient-cove-46720.herokuapp.com/). Can’t believe that’s the name of my app ! ! No worries, we would change it shortly with the “heroku apps:rename” command ! !
So, with the “git push heroku master” the code gets deployed to heroku, heroku understands that it is a Java application from the Maven POM.xml file and then starts downloading the dependencies in the remote deployment package :
D:\Diptiman\ECLIPSE_TEST_WORKSPACE\twitterspringboot>git push heroku master Counting objects: 36, done. Delta compression using up to 4 threads. Compressing objects: 100% (24/24), done. Writing objects: 100% (36/36), 5.69 KiB | 0 bytes/s, done. Total 36 (delta 1), reused 0 (delta 0) remote: Compressing source files... done. remote: Building source: remote: remote: -----> Java app detected remote: -----> Installing OpenJDK 1.8... done remote: -----> Installing Maven 3.3.9... done remote: -----> Executing: mvn -B -DskipTests clean dependency:list install remote: [INFO] Scanning for projects... remote: [INFO] Downloading: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/1.3.1 remote: [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/1.3.1. remote: [INFO] Downloading: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/1.3.1.R remote: [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/1.3.1.RE remote: [INFO] Downloading: https://repo.maven.apache.org/maven2/org/springframework/spring-framework-bom/4.2.4.RELEASE/sp remote: [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/springframework/spring-framework-bom/4.2.4.RELEASE/spr
Once, the build is complete, it checks the procfile and runs the jar file, as below :
remote: [INFO] ------------------------------------------------------------------------ remote: [INFO] BUILD SUCCESS remote: [INFO] ------------------------------------------------------------------------ remote: [INFO] Total time: 21.377 s remote: [INFO] Finished at: 2016-01-27T21:49:34+00:00 remote: [INFO] Final Memory: 34M/264M remote: [INFO] ------------------------------------------------------------------------ remote: -----> Discovering process types remote: Procfile declares types -> (none) remote: remote: -----> Compressing... done, 61.2MB remote: -----> Launching... remote: Released v4 remote: https://ancient-cove-46720.herokuapp.com/ deployed to Heroku remote: remote: Verifying deploy.... done. To https://git.heroku.com/ancient-cove-46720.git * [new branch] master -> master D:\Diptiman\ECLIPSE_TEST_WORKSPACE\twitterspringboot>
Step – 4
Now, the twitter app is deployed in heroku cloud. To test it let’s hit the following URL :
You would be able to see the top twitter trends for the location such as “paris”, “tokyo” etc …
This concludes , part 2 of the Spring Boot REST, deployed in cloud.
In, part 3 and the last part, I would develop and android app to consume this REST ful twitter trend search for locations JSON.
So long …