Java microservices Framework at a glance

Source: Internet
Author: User
Tags spring boot tutorial java se

Introduction: This article first briefly introduces the concept of microservices and the advantages of using microservices, and then introduces several common Java microservices frameworks with examples.

MicroServices are increasingly used in development because developers are committed to creating larger, more complex applications that are better developed and managed as a combination of small services. These tiny services can be combined to work together and achieve larger, more versatile functions. Now there are a lot of tools to meet all the requirements of using piecemeal rather than one-time design and build applications. Today, let's look at what is microservices, the benefits of using microservices, and a few code examples.

What is micro-service?

MicroServices are a service-oriented architecture style (one of the most important skills of Java developers), where applications are built into collections of different small services rather than individual applications. Unlike a single program, microservices allow you to run multiple standalone applications at the same time, and these standalone applications can be created using a different encoding or programming language. A large and complex application can consist of several simple and independent programs that can be executed on their own. These smaller programs are combined to provide all the capabilities of a large single program.

MicroServices Capture your business scenario and answer the question "What do you want to try to solve?" The MicroServices development team has a smaller number of members and can be developed in any language, any framework. Each related program is versioned, executed, and extended independently. These microservices can interact with other microservices and have unique URLs or names, while maintaining availability and consistency even in the face of failures.

What benefits does microservices bring?

One of the benefits of using microservices is that because these smaller applications do not need to use the same programming language, developers can use the language they are most familiar with. This helps developers develop programs with lower costs and fewer errors. The two features of flexibility and low cost also show that these smaller programs can be reused in other projects to make them more efficient.

Examples of several Java microservices frameworks

Here are a few microservices frameworks that you can use for Java development:

    • Spring Boot: This is probably the best Java microservices Framework, which is suitable for control inversion, aspect-oriented programming, and so on.
    • Jersey: This open source framework supports Java's Jax-rs API, which is very easy to use.
    • Swagger: While providing you with the Development Portal Web page, it can help you generate API documentation to allow users to test your API.

You might also consider using some other frameworks, including: Dropwizard, Ninja Web framework, play frame, restexpress, Restlet, RESTX, and the spark framework.

How to use Dropwizard to create microservices

Dropwizard integrates a mature and stable Java library into a lightweight package that you can use in your own applications. It uses jetty for HTTP, Jersey for rest and Jackson for JSON, as well as metrics, guava, logback, Hibernate Validator, Apache HttpClient, Liquibase, mustache, Joda time and Freemarker.

You can use MAVEN to set up the Dropwizard application. How do you do it?

In your pom file, add a Dropwizard.version property with a value of the latest version of Dropwizard.

<Properties><Dropwizard.version>latest version</Dropwizard.version></Properties><!--then list the Dropwizard-core library:--><dependencies> <dependency> <groupid>io.dropwizard</ groupid> <artifactId> Dropwizard-core</artifactid> << Span class= "Hljs-name" >version>${version}</version> </ Dependency></DEPENDENCIES>      

This will set up a MAVEN project for you. Here, you can create configuration classes, application classes, presentation classes, resource classes, or a health check, build Fat JARS, and then run the application.

You can view the Dropwizard user manual in this link, which is the GitHub library.

Example code:

Package com. Example. helloworld;import com. Yammer. Dropwizard. config. Configuration;import com. fasterxml. Jackson. annotation. jsonproperty;import org. Hibernate. Validator.constraintspublic class  Helloworldconfiguration extends Configuration { Span class= "hljs-variable" > @NotEmpty  @JsonProperty private String template;  @NotEmpty  @JsonProperty private String DefaultName = " stranger "; public string  GetTemplate () {return template;} public string  Getdefaultname () {return defaultname;}}            

Spring Boot Micro Service

Spring boot allows you to use the Java application provided by the embedded server with your own application. It uses Tomcat, so you don't have to use other Java EE containers. The Spring boot Tutorial sample can be accessed here.

You can find all the spring boot projects here, and you'll find that spring boot has all the infrastructure you need for your application. Whether you're writing a security class, a configuration class, or a big data class application, you'll always find the corresponding spring boot project.

The Spring Boot project includes:

-Spring IO Platform: Enterprise-level distribution for versioned applications.

-Spring Framework: For transaction management, dependency injection, data access, messaging, and Web applications.

-Spring Cloud: For distributed systems, to build or deploy your microservices.

-Spring data: For microservices related to data access, whether mapping or attribution, relational or non-relational.

-Spring batch: For high-level bulk operations.

-Spring Security: For authorization and certification support.

-Spring Rest Documentation: for RESTful service documentation.

-Spring Social: Used to connect to social media APIs.

-Spring Mobile: for mobile Web applications.

Example code:

import org. springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.annotation.*; @RestController  @EnableAutoConfigurationpublic class Example {  @RequestMapping ( "/") string Home () {return "Hello world!";} public static void Main (string[] args) throws Exception { ( Example.class, args); }}


The Jersey restful framework is open source, based on the JAX-RS specification. Jersey applications can extend existing JAX-RS implementations and add functionality and utilities to make restful services easier and client-side development easier.

The best thing about jersey is that its documentation is very detailed and there are many examples. It's fast and the routing is very simple.

The documentation on how to get started with jersey is here, while more documents can be found here.

You can try running the following sample code:

package Org. GlassFish.jersey.examples .helloworld;import @Path ( "HelloWorld") public class Helloworldresource {public Static final String cliched_message = "Hello world!";  @GET  @Produces ( "Text/plain") Public String Gethello () {return cliched_message;}}          

Jersey can be easily used with other libraries, such as Netty or Grizzly, which supports asynchronous connections. It does not require a servlet container. However, it is really rude to rely on injection implementations.

Play frame

The play framework makes it easy to use Scala and Java to build, create, and deploy Web applications. The play framework is the ideal choice for restful applications that require parallel processing of remote calls. It is modular and supports asynchrony. The play framework community is also one of the largest communities in all microservices frameworks.

You can try running the following sample code:

Package controllers; import play.mvc.*; indexSayHello(String myName) {render (MyName);}}   


Restlet can help developers create fast and extensible Web APIs that follow the restful architectural pattern. It has good routing and filtering capabilities for Java Se/ee, OSGi, Google AppEngine (part of Google Compute), Android, and other major platforms.

Because its community is closed, Restlet's learning curve is steep, but you can get help from StackOverflow.

Example code:

Package firststeps; import Org.restlet.resource. Get; import Org.restlet.resource. Serverresource; "Hello, World";}}    

Java microservices Framework at a glance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.