Heroku is one of the most popular Cloud PaaS providers in the market, and for that reason I decided to show how you can write microservices/applications using WSO2 Microservices Framework for Java — msf4j, and host them there.

Image for post
Image for post

First off all, I followed the tutorial focused for Java, available in the Heroku Dev Center (https://devcenter.heroku.com/articles/java-support), I recommend you take a look on this, once I will also follow the same recipe in this blog post.

In this step, we will execute the Maven archetype:

mvn archetype:generate -DarchetypeGroupId=org.wso2.msf4j -DarchetypeArtifactId=msf4j-microservice -DarchetypeVersion=1.0.0 -DgroupId=io.edgar -DartifactId=ms4j-app -Dversion=1.0.0-SNAPSHOT -Dpackage=io.edgar.service

Image for post
Image for post
Execution with Successful

See the generated project:

Image for post
Image for post
Genereted Project

You had generated a project that is the skeleton for your future Microservices.

Here you can import your project to your prefered IDE, in my case I am using WSO2 Developer Studio (Eclipse):

Image for post
Image for post
Imported Project in Eclipse (WSO2 Developer Studio)

There is a slight change that you will must to do in the Application class, keep in mind, that as a Microservice, it will be a java -jar style microservice app that will be deployed into Heroku, please keep watching the next steps.

Heroku binds the HTTP port on the fly, so when I tried to deploy default 8080 HTTP port, I got an error from Heroku. In order to avoid that, I did a little change in Application's main method:

In that case, if you pass an int value, it will be passed to the Microservices Server for Java bootstrap the HTTP's services.

Heroku uses that file to determine which is the command that the "container" will run when the VM instance is starting.

So, create a file called Procfile in the project's root dir, and please execute the maven project: mvn package, it will build and generate the fat executable jar that your can execute. In order to test, you can run:

java -jar target/ms4j-app-1.0.0-SNAPSHOT.jar

It will start the msf4j runtime and will expose your services via REST, see the image below:

Image for post
Image for post
Local Application with Services running locally

The content from your Procfile will be:

web: java -jar target/ms4j-app-1.0.0-SNAPSHOT.jar $PORT

Understanding the Procfile:

a) web : It will start a Web Process, and Heroku will try bind a valid HTTP port for this service (Application)

b) java -jar target/ms4j-app-1.0.0-SNAPSHOT.jar $PORT : This is the command that will be executed when the Heroku's Dynos get started, if you are familiar with Docker, it might remind you the same concept when the "container" get started. The last but not least is the $PORT, we don't know which port Heroku will configure for our app, but in that case, either port Heroku configures, we will use the this environment's variable to pass to our application.

You will have to log in in the Heroku Toolbelt Terminal, and create an app into your Heroku’s account, enter in the directory from the application that you generated with the MSF4J Maven Archetype help, and execute this command: heroku create msf4j-app

Image for post
Image for post
See that you you see your application created inside the msf4j created project.

Once you had executed the previous step, you will be able to see your application into your Heroku dashboard account:

Image for post
Image for post
Heroku’s dashboard showing your application

If you type the following command now: git push heroku master , you will see that Heroku will start to publish your application, besides, it will start to invoke the maven execution.

Image for post
Image for post
It will take some time until Maven download and execute everthing.

1 — We created a msf4j project with maven archetype help
2 — We created the Procfile, which is the file that initializes our Dyno (Heroku's instance VM).
3 — We created the heroku’s project inside the folder where maven generated our project.

Anytime you made a change, don't forget to :

a) git add . (if you had added new files)
b) git commit -m "message" for committing your changes
c) And git push heroku master — For send the changes from your machine to Heroku and build it inside your Heroku's dyno (VM instance).

Image for post
Image for post
When you invoke the git push heroku master

Here after the execution:

Image for post
Image for post
Everything worked fine in the Heroku environment

If you execute the command: heroku logs — tail, you will be able to check the execution and the console from your Heroku's dyno/instance vm:

Image for post
Image for post

In that case, you can see that our Procfile is working, and in that case our Microservices Server Runner started the services in the port 21120, that was the HTTP port that Heroku made the bind to offer this application via the default HTTP port (80).

Image for post
Image for post

Please, check the application running here:

https://msf4j-app.herokuapp.com/service

Or if you want to see via CURL or HTTP:

Image for post
Image for post

Microservices Framework for Java — MSF4J is a very lightweight solution in order to build Microservices architectures and applications, IMHO the greatest features in that library/runtime/server compared to others is the fact to bring very great capabilities out-of-the-box, for instance:

  • Metrics Support via JMX and Sophisticated Dashboards to presents analytics
Image for post
Image for post
Command Line Metrics
Image for post
Image for post
WSO2 Data Analytics Server — Metrics Dashboard
  • Security Support
  • Capable to import a Swagger definition to create the services implementation
  • Capacity to use Governance tools, but this is a very long history that I prefer to share in another post.

Written by

Just a dad, husband, and an Old-School Brazilian Jiu-Jitsu Black-belt. Skalena Founder, former WSO2, MuleSoft, Oracle, RedHat, Sun, Borland

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