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.
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.
Creating the Project with MSF4J Archetype
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
See the generated project:
You had generated a project that is the skeleton for your future Microservices.
Importing Project into your IDE
Here you can import your project to your prefered IDE, in my case I am using WSO2 Developer Studio (Eclipse):
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.
Passing HTTP Port via Java Command Line
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.
Adding the Procfile
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:
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.
Using Heroku Toolbelt Tool
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
Once you had executed the previous step, you will be able to see your application into your Heroku dashboard account:
Synchronizing the 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.
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.
About to get it done!
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).
Here after the execution:
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:
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).
Please, check the application running here:
Or if you want to see via CURL or HTTP:
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
- 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.