WSO2 Microservices Framework for Java (MSF4J) is a lightweight high-performance framework for developing & running microservices. It is included in the WSO2 Enterprise Integrator.
We are familiar with Service Oriented Architecture (SOA) and the concept of Services like the xml-based SOAP services and RESTFUL services but recently we have seen the advent of so called Microservices. So, lets dive into this new concept.
What are Microservices?
Microservices are narrow-focused services. A microservice does just one thing, and bears complete responsibility for this thing – including the management of its data.
It has a clearly bounded context, which does not overlap the context of any other microservice. It’s a deployable unit, hence each microservice will follow its own lifecycle. If you think about it, this has numerous implications. Microservices do not share a common data model, they are completely self-contained. Hence, inside a microservice architecture, a lot of inter-service communication is going on. This is the price you pay for maximum agility.
The MSF4J framework
Touted by WSO2 as one of the highest performing lightweight Java microservices frameworks. If you want to know more about the performance test, take a look at the github repository found here.
MSF4J is a relatively new product from WSO2. Contrary to existing products like Enterprise Integrator and API Manager, the MSF4J is a product where we actually code the services rather than configure them.
You write Java programs using a special maven archetype and it is typically aimed at connectivity services like REST.
Hello world with MSF4J
It is really easy to define & deploy a Java microservice using WSO2 MSF4J. You simply need to annotate your service and deploy it using a single line of code.
Let’s get started by writing a hello world MSF4J microservice. In subsequent blogs we will extend the example we create in this blog.
You can use the msf4j-microservice Maven archetype to create your first MSF4J project. Make sure you have JDK 1.8 and Maven 3.x installed, & run the following command.
mvn archetype:generate -DarchetypeGroupId=org.wso2.msf4j -DarchetypeArtifactId=msf4j-microservice -DarchetypeVersion=2.5.2 -DgroupId=org.example -DartifactId=Hello-Service -Dversion=0.1-SNAPSHOT -Dpackage=org.example.service -DserviceClass=HelloService
When Maven asks to confirm your choice, just press Enter.
This command tells Maven what to create. The commands in green are commands that you would typically not change since they are needed to create an MSF4J service. The commands in red are used more for naming the Microservices and of course can be changed to fit your purpose and naming conventions.
Copy this command to your terminal or command window and execute it. Maven (mvn) should be present of course in order to create your first MSF4J project. If you do not have Maven installed, you can find it here.
This will generate a project structure for you to quickly get started. Next navigate to the Hello-Service directory. You will find a pom.xml file and also an src directory.
The POM is quite straightforward. We uncommented the msf4j-analytics dependency because we want to have metrics and monitoring.
This pom is filled with the parameters you passed with the mvn command. The POM of course governs the build of the JAR file which we will run using the java -jar [nameofjar]
command.
But first we need to build our Hello World service. The Code placed in the [STARTDIR]Hello-Servicesrcmainjavaorgexampleservice
Is auto generated code and does little.
HelloService.java
Change the org.example.service.HelloService class as follows to echo back the name input parameter.
You can remove the auto generated code and replace it with the following code segment:
package org.example.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@Path("/hello")public class HelloService {
@GET
@Path("/{name}")
public String hello(@PathParam("name") String name) {
System.out.println("This is my response: Hello " + name);
return "This is my response: Hello " + name;
}
}
We include the possibility to put a message on the console (terminal) as well as a regular return in for instance a browser.
Application.java
This is the one-liner to deploy your service using WSO2 MSF4J.
public class Application {
public static void main(String[] args) {
new MicroservicesRunner()
.deploy(new HelloService())
.start();
}
}
You can also pass in the port(s) as an argument to the MicroservicesRunner class constructor. When passing the port(s) as an argument, by default it binds to 0.0.0.0 host. Use “msf4j.host” environment variable to override the host value.
Build the Service
Run the following Maven command: mvn package
This will create the fat jar Hello-Service-0.1-SNAPSHOT.jar
in the target directory. This jar is for the simple HelloService already 6 to 12 MB in size.
This fat jar is a jar file that contains your microservice as well as all its dependencies.
Run the Service
You just have to run the following command to get your service up and running. This needs
java -jar target/Hello-Service-0.1-SNAPSHOT.jar
Test the Service Run the command above and simply go to http://localhost:8080/hello/yenlo ) from your browser and see the response in the terminal (This is my response: Hello yenlo) as well as in the terminal.
This is of course a very simple example. In future blogs we will develop more meaningful and more ‘true’ microservices.
If you have any questions about this blogpost contact us via the comments section below. View also our WSO2 Tutorials, webinars or white papers for more technical information. Need support? We do deliver WSO2 Product Support, WSO2 Development Support, WSO2 Operational Support and WSO2 Training Programs.