SpringBoot for Microservices

Spring Boot is a framework module which provides RAD (Rapid Application Development) feature to the Spring framework. It is highly dependent on the starter templates feature which is very powerful and works flawlessly.

Spring Boot
Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Features :

  • Create stand-alone Spring applications
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  • Provide opinionated ‘starter’ dependencies to simplify your build configuration
  • Automatically configure Spring and 3rd party libraries whenever possible
  • Provide production-ready features such as metrics, health checks and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration

Important Annotations :

  • @SpringBootApplication
  • @RestController
  • @Configuration
  • @EnableAutoConfiguration
  • @RequestMapping(value=”/”)
  • @ResponseBody
  • Etc…

Why we need SpringBoot ?

  • In microservice architecture, most of the companies are using Nodejs. Because it is a lightweight, open source, Highly scalable. But Node Js is single threaded. So, In that case, you can choose for your microservice.
  • Java & Spring framework both are Open-source.
  • Java is a multithreaded language.

Sample Spring Boot Project :

SkeletonApplication.java

package Skeleton.demo;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@Configuration
@EnableAutoConfiguration
public class SkeletonApplication {
        
    @RequestMapping(value="/")
    public String status(){
        return "My First SpringBoot ;)";
    }
    public static void main(String[] args) {
        SpringApplication.run(SkeletonApplication.class, args);
    }
}

Status.java

package Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Vivek Java
 */
@Controller
public class Status {
   @RequestMapping("/")
   @ResponseBody
   public String hello() {
      return "Hello Spring Boot Learners";
   }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>Skeleton</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Skeleton</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
                 <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.kafka</groupId>
			<artifactId>spring-kafka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.kafka</groupId>
			<artifactId>spring-kafka-test</artifactId>
			<scope>test</scope>
		</dependency>
	 <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-web</artifactId>
	  <version>5.1.3.RELEASE</version>
	  <type>jar</type>
	 </dependency>
	 <dependency>
	  <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-configuration-processor</artifactId>
	  <version>2.0.3.RELEASE</version>
	  <type>jar</type>
	 </dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

application.properties

server.port = 8000

In the above file, you can configure your port number based on your wish.

To Run :

  • Step 1: Install maven click here to see how
  • Go to the location where your pom.xml is present.
  • set the location in cmd
  • Run the command $ mvn clean install
  • After downloading all your dependencies build your spring-boot project. (Ex : C:\Users\Gopal.m2\repository\Skeleton\demo\0.0.1-SNAPSHOT)
  • In the above location, my demo-0.0.1-SNAPSHOT.jar was build successfully.
  • Now using the command you can start your spring boot project $java -jar demo-0.0.1-SNAPSHOT.jar.
  • Now Your SpringBoot Microservice is running on port 8000

Download Source Code :




Sample project




Common Issues :

Why does my Spring Boot App always shutdown immediately after starting?

Solution : add the below code in your pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>


****** BEST OF LUCK *******

Try Another program: Nightwatch.js

You may also like...