Home United States USA — software Java Microservices: Code Examples, Tutorials, and More Java Microservices: Code Examples, Tutorials,...

Java Microservices: Code Examples, Tutorials, and More Java Microservices: Code Examples, Tutorials, and More

313
0
SHARE

Here we explore microservices and their benefits in the context of various Java-friendly frameworks, including Spring Boot, Jersey, Play, and Restlet.
Microservices are increasingly used in the development world as developers work to create larger, more complex applications that are better developed and managed as a combination of smaller services that work cohesively together for larger, application-wide functionality. Tools are rising to meet the need to think about and build apps using a piece-by-piece methodology that is, frankly, less mind-boggling than considering the whole of the application at once. Today, we’ ll take a look at microservices, the benefits of using this capability, and a few code examples.
Microservices are a form of service-oriented architecture style (one of the most important skills for Java developers) wherein applications are built as a collection of different smaller services rather than one whole app. Instead of a monolithic app, you have several independent applications that can run on their own and may be created using different coding or programming languages. Big and complicated applications can be made up of simpler and independent programs that are executable by themselves. These smaller programs are grouped together to deliver all the functionalities of the big, monolithic app.
There are several benefits to using microservices. For one, because these smaller applications are not dependent on the same coding language, the developers can use the programming language that they are most familiar with. That helps developers come up with a program faster with lower costs and fewer bugs. The agility and low costs can also come from being able to reuse these smaller programs on other projects, making it more efficient.
There are several microservices frameworks that you can use for developing for Java. Some of these are:
Others that you can consider include: Dropwizard, Ninja Web Framework, Play Framework, RestExpress, Restlet, Restx, and Spark Framework.
DropWizard pulls together mature and stable Java libraries in lightweight packages that you can use for your own applications. It uses Jetty for HTTP, Jersey for REST, and Jackson for JSON, along with Metrics, Guava, Logback, Hibernate Validator, Apache HttpClient, Liquibase, Mustache, Joda Time, and Freemarker.
You can setup Dropwizard application using Maven. How?
In your POM, add in a dropwizard.version property using the latest version of DropWizard.
This will set up a Maven project for you. From here, you can create a configuration class, an application class, a representation class, a resource class, or a health check, and you can also build Fat JARS, then run your application.
Check out the Dropwizard user manual at this link. The GitHub library is here.
Sample code:
Spring Boot gives you Java application to use with your own apps via an embedded server. It uses Tomcat, so you do not have to use Java EE containers. A sample Spring Boot tutorial is at this link.
You can find all Spring Boot projects here, and you will realize that Spring Boot has all the infrastructures that your applications need. It does not matter if you are writing apps for security, configuration, or big data; there is a Spring Boot project for it.
Spring Boot projects include:
Sample code:
Jersey RESTful framework is open source, and it is based on JAX-RS specification. Jersey’s applications can extend existing JAX-RS implementations and add features and utilities that would make RESTful services simpler, as well as making client development easier.
The best thing about Jersey is that it has great documentation that is filled with examples. It is also fast and has extremely easy routing.
The documentation on how to get started with Jersey is at this link, while more documentation can be found here .
A sample code that you can try:
Jersey is very easy to use with other libraries, such as Netty or Grizzly, and it supports asynchronous connections. It does not need servlet containers. It does, however, have an unpolished dependency injection implementation.
Play Framework gives you an easier way to build, create and deploy Web applications using Scala and Java. Play Framework is ideal for RESTful application that requires you to handle remote calls in parallel. It is also very modular and supports async. Play Framework also has one of the biggest communities out of all microservices frameworks.
Sample code you can try:
Restlet helps developers create fast and scalable Web APIs that adheres to the RESTful architecture pattern. It has good routing and filtering, and available for Java SE/EE, OSGi, Google AppEngine (part of Google Compute) , Android, and other major platforms.
Restlet comes with a steep learning curve that is made worse by a closed community, but you can probably get help from people at StackOverflow.
Sample code:
For further reading and information on microservices, including some helpful tutorials, visit the following resources:

Continue reading...