In order to quickly develop microservices, Spring Boot is a common choice. Why should I be interested in Spring Boot? In this blog post I’ll give you some reasons why looking at Spring Boot is interesting and give some samples on how to get started quickly. I’ll shortly talk about microservices, move on to Spring Boot and end with Application Container Cloud Service which is an ideal platform to run and manage your Spring Boot applications on. This blog touches many subjects but they fit together nicely. You can view the code of my sample Spring Boot project here. Most of the Spring Boot knowledge has been gained by the following free course by Java Brains.
Microservices
Before we go deeper into why Spring Boot for microservices, we of course first need to know what microservices are. An easy question to ask but a little complex to answer in a few lines in this blog. One of the first people describing characteristics of microservices and actually calling them that was Martin Fowler in 2014. What better source to go back to then the articles he has written. For example here.
‘In short, the microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies.’
— James Lewis and Martin Fowler
Of course there are a lot of terms involved in this definition
- It is an architectural style for developing a single application.
- A suite of small services each running in its own process.
- Communicating with lightweight mechanisms, often HTTP.
- Build around business capabilities. Look up bounded context.
- A bare minimum of centralized management of these services. This implies no application server which provides centralized management of the applications running on it.
- May be written in different programming languages or use different storage technologies.
A microservice architectural style also has several characteristics. It is very interesting to look at such an architecture in more detail like for example the OMESA initiative to help you get started. As is of course obvious and true with all architectural styles, you will gain most benefits when doing it right. It is however often not trivial to determine ‘right’.
Spring Boot microservices
Spring Boot features and microservice principles
Spring Boot is based on certain principles which align with microservice architecture. The primary goals of Spring Boot:
- Provide a radically faster and widely accessible getting started experience for all Spring development.
- Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.
- Provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration).
- Absolutely no code generation and no requirement for XML configuration.
The features provided by Spring Boot also make it a good fit to implement microservices in.
- Spring Boot applications can contain an embedded Tomcat server. This is a completely standalone Tomcat container which has its configuration as being part of the application.
- Spring Boot is very well suited to create light weight JSON/REST services.
- Features like health checks are provided. Spring Boot offers Actuator. A set of REST services which allow monitoring and management. Look here. Also externalized configuration can be used. Few centralized management features are required.
- Since different storage techniques can be used, Spring provides Spring Data JPA. JPA is Java Persistence API. This API provides ORM capabilities to make working with relational databases easier (mostly vendor independent, supports EclipseLink, Hibernate and several others).
Easy to implement API design patterns
There are plenty of descriptions online to provide API design guidelines. See for example here. An example API URL can be something like: http://api.yourservice.com/v1/companies/34/employees. Notice the structure of the URL which amongst other things contains a version number. Oracle Mobile Cloud Service documentation also has several design recommendations. See here. These design considerations are of course easily implemented in Spring Boot.
See for example the below code sample:
You can see how the HTTP operations are used and the way method calls are mapped to URLs. Added benefit of this sample is that it also shows how to access the body of the request message.
Integration with backend systems
Spring Boot integrates with JPA. JPA provides an API to easily do ORM. It allows you to work with objects in Java which are backed by database data. For basic CRUD operations, the effort required to implement JPA in Spring Boot is minimal.
You only need three things to do simple CRUD operations when using the embedded Derby database.
- An annotated entity. You only require two annotations inside your POJO. @Entity to annotate the class and @Id to indicate the variable holding primary key.
- A repository interface extending CrudRepository (from org.springframework.data.repository)
- Inside your service, you can use the @Autowired annotation to create a local variable with an instance of the repository.
Connection details for the embedded Derby server are not required. They are for external databases though. If you want to connect to an Oracle database, read the following here.
Pretty comparable to microservices on Node
Node or Spring Boot? This is of course a topic which has many opinions. Many blogs have been written to compare the 2. See for example here.
In several aspects, Spring Boot beats Node.js.
- Performance. Read the following article here. Spring Boot microservices can achieve higher throughput than similar services on Node.js.
- Maturity. Spring has a long history of running Enterprise Applications. Node.js can also be used but is less mature.
- Security. Spring and Spring Boot are clearly better than Node.js. For example, Kerberos support in Node is limited while Spring Boot provides easy abstractions for several security implementations amongst which Kerberos tokens.
- RDBMS. This is more easy to use in Spring Boot because of JPA.
Node.js beats Spring Boot also in several aspects
- Build/package management. People who have experience with Maven and NPM often prefer NPM
- UI. JavaScript is of course the language of choice for front-end applications. The Java based frameworks such as the JSF variants by far do not have the productivity as for example a framework like AngularJS.
- Document databases like MongoDB. When you can work with JSON, JavaScript code running on Node.js makes it very easy to interact with the database.
Spring Boot, being in the Java ecosystem can also be combined with for example Ratpack. See here. Ratpack provides a high throughput, non-blocking web layer. The syntax is similar to how you would code Node.js code. This is of course not so much of an argument for Spring Boot since modules on Node.js provides similar functionality. Both solutions are more alike than you would think on first glance.
It depends probably mainly on the skills you have available and your application landscape if you would choose Node.js or Spring Boot. If you’re from the JavaScript world, you might prefer to write your microservices on Node.js. If you’re from the Java world, you will prefer Spring Boot. It is important to understand there is not an obvious superior choice whether to go for Node.js or Spring Boot.
Getting started with Spring Boot
The easiest way to get started is first watch some online courses. For example this one from Java Brains. I’ll provide some nice to knows below.
Spring Tool Suite (STS)
As for an IDE, every Java IDE will do, however, since Spring Boot is build on top of Spring, you could consider using Spring Tool Suite (STS). This is a distribution of Eclipse with many specific Spring features which make development of Spring applications easier.
Spring Initializr
An alternative way to get your start project is to go to https://start.spring.io/, indicate your dependencies and click the Generate project button. This will generate a Maven or Gradle project for you with the required dependencies already added.
With STS, you can also use the Spring Initializr functionality easily.
Spring Boot CLI
Spring Boot CLI offers features to create and run Groovy Spring Boot applications. Groovy requires less code than Java to do similar things. It is a script language which runs on the JVM and from Groovy you can access regular Java classes/libraries.
You can for example create an application like:
@RestController
class HelloWorldClass {
@RequestMapping(“/”)
String home() {
return “Hello World!”
}
}
Save this as a Groovy script (e.g. app.groovy) and run it with Spring Boot CLI like: spring run app.groovy
Getting actually started
To get started with Spring Boot, you have to add some entries to your pom.xml file and you’re ready to go. Easiest is to use the New Spring Starter project from STS since it will generate a pom, a main and test class for you. That is what I used for my sample project here.
Spring and Oracle
Spring is a very common Java framework. You can find traces of it in several Oracle products and features. Below some examples. If you look in other Oracle products, especially those who are Java based, I expect you will find many more examples.
SOA Suite
For example in Oracle SOA Suite.
- SOA Suite itself under the covers uses Spring
- SOA Suite can use Spring components
Massive Open Online Course
Oracle uses Spring Boot in courses it provides. For example in the Develop RESTful Java Microservices deployable on Oracle Cloud MOOC.
Application Container Cloud Service (ACCS)
ACCS has been around for a while. Together with Spring Boot, they provide an ideal combination to get your microservices developed and running quickly.
Application Container Cloud Service provides the features of The Twelf-Factor App out of the cloudy box so you don’t have to develop these yourself. These of course also align with the microservice principles like executing apps as stateless processes.
If you want to use ACCS with Spring Boot, there are two ways you can deploy your Spring Boot application.
- You can create a WAR file by specifying war in the packaging tag in the pom.xml file. Next you can deploy this WAR file as a Java EE Web Application. This runs WebLogic in the background. Read more here.
- You can create a JAR file by specifying jar in the packaging tag in the pom.xml file. Next you can run this JAR file directly since you’ll get an embedded Tomcat with it and can run it as a Java SE application. The configuration will be part of the application here.
I’ve not compared both options in detail but I can imagine if you want to run a ‘micro’-service, an entire WebLogic Server might make it more of a ‘macro’-service.
I had to double-take this comment and check the date of the article: JANUARY 24, 2018
“Node.js can also be used but is less mature.”
You clearly have no idea what you are talking about with regard to NodeJS. Spring has long been used to “fix” the bloated legacy mess that is Java and its many discordant, often aimless and redundant APIs. You apparently failed to see you are a living example of the “golden hammer” anti-pattern. It’s the “sour grapes” fable on steroids. There is a reason Java has been quietly dying a slow death and rest assured it’s not “excellence” or “efficiency” or “consistency” or “simplicity” but rather the opposite of all of the above. Java is a necessary evil because it is so pervasive, but let’s not make idiotic comments about Node not being mature enough, so you should therefore use Spring Boot.
Spring is great, I learned on this technology a lot, worked on massive scale projects, Spring Security, Integration, Data REST, JPA, AMQP, etc. no question about it.
But I think the days of Spring are simply getting over and anyone sane in thinking about hardware and development costs on large scale projects should be really careful to prevent unexpected damage before the real work starts. I would not recommend Spring technologies for microservices development in these days. First the performance is simply getting worse and worse, but also Spring is not a microservice framework or platform.
If I talk in these days about microservices, I already almost try to abandon whole HTTP REST API style by replacing it with messaging layer at 100%, but it is more to focus on following idioms:
* Event sourcing
* Even store
* Saga pattern
* Audit pattern
* Lightweight workflow
* Event driven
* Fully asynchronous
* Comand Query Responsibility Segregation
Spring is still low-level from this perspective as it gives you only building blocks to build these more advanced blocks, but we have here real microservices frameworks already available:
* Eventuate (http://eventuate.io/) is probably first real microservice platform (correct me if I am wrong) open sourced by I would say legendary Chris Richardson 🙂 at least for me haha. Although it could be right choice for you as well, it is based on Spring, so you can be prepared it will be hungry and slow (but please, speed is only my personal point of view).
* AxonFramework (http://www.axonframework.org/) is another fully featured microservice frameworks, but I don’t have any real experience with.
* A High Throughput, Low Latency and Small Memory Footprint API Platform (https://doc.networknt.com/) is answer to all my thought at time and is also after few months evaluation choice for my next large scale platform project.
NOTE: All 3 frameworks brings in place out of the box all patterns and styles mentioned in the beginning of my answer here. So you don’t focus on implementing Sagas, workflow, event store, but you get it out of the box and focus on real business logic implementation much faster.
Thank you for your valuable feedback! Especially indicating some challenges with Spring and providing suggestions for alternative frameworks which provide out of the box solutions. I’ll look into them.
With kind regards,
Maarten