Implementing MicroServices Using Spring Boot

sambsv
5 min readOct 12, 2022

--

Features

Quote from SpringBoot

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

Automatically configure Spring and 3rd party libraries whenever possible

Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

Provide opinionated ‘starter’ dependencies to simplify your build configuration

Decouple Web and Servlet Dispatcher and related configuration files

Come ready with Maven archetype , dependency so that we can run the SpringBoot API and release API

The blog demonstrates some advanced features of building and running Spring Boot microservices using database

  1. Create Project with SpringBoot Starter

Use https://start.spring.io and create project. Download the archive and extract to your local. The steps are well documented .Also opening the project using Intellij or Eclipse is also well documented on medium sites as well on these IDE homepages. We will cover the code to get our Microservices development using Database MySql

I might have forgotten to include some of the software that will be used throughout this article:

  1. IntelliJ — my IDE for Java. Others can be used, but the process I will be showing is mainly for IntelliJ.I am veteran user of Eclipse/STS that works great. You can use that also
  2. I am veteran user of Eclipse/STS that works great. You can use that also
  3. Postman — used for testing API’s

Here comes IntelliJ, the IDE we’ll be using for our development. We’ll open up our previously extracted project in IntelliJ.

Java has multiple package managers with prominent , Maven and Gradle.

  • Maven — This is the industry-standard package manager for Java.
  • Gradle — Gradle is newer than Maven and also has a faster build time. This is very great and you must use to see this. I will use this for Spring reactive microservices in that post.

For the project, we’ll go with Maven. The Starter Project was created using Maven. It has all basic dependencies. Lets ensure following dependencies or just add them if they are not there. Note Spring Boot Starter Wizard gives options to to add these and is ver beautiful. However many developers have lived era before SpringBoot and use maven to configure the pom.xml.

  1. Configure Dependencies

Creating Rest Controller For Post

Add annotations RestController, RequestMapping

CrossOrigin — Add so that yur UI Developers can make call and proceed with developments. Security can be addressed in non -dev and its matter of configurations and some annotations. We will focus on business use case to create Rest API that our team/ UI and backend needs. Please check out my other posts on Security and Redis

Ensure that add methods

to retrieve all posts, one post and to delete post as shown below. You can name the methods as you want . Used your IDE-Refractor and you see this is piece of cake. We want to have the crux of code and simplified understanding for our audience.

RestController Class And annotations

Creating Repository

Our controller class has methods and need data . This is delegated to Repository. Create different package and have interface created PostRepository that has list of all business methods aligned with Spring conventions. We will not use CRUDRepository feature to create all CRUD as we want to dig into API and have the power to customize and add business logics . Create class JdbcRepository that implements our interface PostRepository

Spring is DI , so lets inject JDBCTemplate which provided api needed for CRUD operations

Run the Project

here lies the beauty. SpringBoot Provide the call that is annotated with @SpringBootApplication . It has main method. Just tun this main Class and we see the SpringBoot running on default port 8080. We can customize these using Spring Configuration exposed to us and we will do in other post or refer to SpringBoot Docs.

Invoke your REST API with PostMan

I am using Postman. There are lot of tools and app to run/test/invoke REST API.

List all posts. If you donot like the prefixed svposts just got to controller and change the Requestmapping of Controller and you are there . No Code changed just configuration of Annotation — power delivered by Spring RequestMapping

Retrieve details of specific post.

Conclusions

And there you have it. We got REST API for Spring Boot that connects to our database. The Microservice can be deployed by configuring yaml /SpringBoot properties and adjusting configurations . Add Actuator , Hysterix , Caching and creating multi pom module to separate controller and services and dao .These are some of the points on how to create a REST API. Next we will learn to create a Spring reactive application the connects to a no sql database and is powered by the netty — NIO (that supports reactive streams).

--

--

sambsv

Skiing on snow and developing software from a specification are easy if both are frozen. Software Engineer