In one of my previous posts, we setup a local rest api web application with a spring data jpa, hibernate and a postgreSQL database. That post was Rest Api with Spring, hibernate and Postgres. The aim for setting up that project was to use it to test out REST http integration from Salesforce. However Salesforce cannot integrate with a local service. So we need to deploy this service to an externally exposed server. I decided to expose it on Heroku.
Heroku is a platform as a service cloud provider. You can create your application and deploy it to their platform. They take care of managing the actual server where the application is deployed. Moreover its all on the cloud. You just login to their portal and manage your application. Your source code for the application is maintained in Git. You can made changes in Git and deploy it back to Heroku. Internally it deploys your app to linux servers (they called dynos). If you need to scale your app for heavy traffice, you can increase the number of dynos from the heroku dashboard itself.
Below I will deploy my java application to Heroku. First steps will be -
- Setup a heroku account (it is a free account for one web app and one database)
- From their website, install the heroku toolbelt. This will allow you to work with Heroku from the command prompt.
- If you are interested to learn more about Heroku, use their getting started guides.
- I started with one of their getting started guides for Java, this enabled me to setup a java application with connectivity to Postgres
- They provide a postgres database as part of their free tier. I am going to use that.
- Next, I will update my POM.xml in the java application to include Maven dependency plugin. Also I am including configuration to include jetty-runner.jar as a dependency.
Once I have jetty-runner.jar in my dependencies, it will get downloaded to my target dependency folder. I can use this jar to execute a java command and bring up my web application in the jetty application server.
Note: in previous post, i had included the maven-jetty-plugin in the POM already due to which I was able to execute mvn jetty:run command. Heroku needs you to follow above approach so you can specify the java command to bring up this webapp in a file called Procfile
- In the root directory of my project, i am going to setup a file "Procfile". Be careful to not give an extension to it and give the exact name (I learnt that the hard way :) ). This file should contain the java command to execute and bring up your webapp in the jetty server.
- Now lets talk about the database. In my previous post, we had configured hibernate using a Java configuration class DBConfig.java. It was setup to connect to a local postgres database. Now when we deploy our app to Heroku, we will need a postgres database on Heroku. Heroku allows you to create a free heroku database. Lets run below commands to do that.
- In my project's root directory, to create the heroku app - use the command heroku create
- this will create the heroku app and give back the details of the blank app that was created for you. It will also add a git remote repository with name "heroku"
- Lets add a database in this blank heroku app by doing the following - heroku addons: create heroku-postgresql
- This will add a database for your app on the heroku cloud. It also adds an environment variable "DATABASE_URL" in your environment on the cloud which will have the uri to your database. We will use this to update our DBConfig.java to connect to the new postgres database.
- Change the DBConfig.java code to use the system environment variable to get the database path, port, user name and password -
Note: How you are using the System.getenv to get the DATABASE_URL value and then parsing it to create the datasource details.
- On heroku cloud, you may want to create the database table and put some data there. To connect to your postgres db on the cloud, just use the command - heroku pg:psql . It will connect you to the app's database. Use standard sql commands to build the users table and put any data you want there.
- Now you are done with all changes. Lets commit all the changes to your git repository first. Post that push all the changes to the remote Heroku repository with the command - git push heroku master
- When you push it to the heroku repository, you will see that Heroku automatically scans your code, detects the java code, builds it and deploys your app -
- One last step remains, you need to scale your app to have atleast one process running. This is done by using the command - heroku ps:scale web=1
- Thats it, your app should be running now. To access your app, you can use the command - heroku open
Remember as you are using a free tier application, it will stay up for only 30 mnts and go down due to inactivity. I am sure there are other limits that apply in the free tier. If you are interested in having a real application on the cloud, you can always upgrade to use their services in a better way.
Comments
Post a Comment