Java ProgrammingLearn How to Create Microservices with the Spring Framework

Learn How to Create Microservices with the Spring Framework

The concept of Microservices is being widely put into practice across enterprises. Compared to the monolithic IT systems, Microservices offer more benefits and helps overcome the limitations of the monolithic systems, especially in context of modern-day needs of the enterprises. Modern enterprises want lesser downtime, agile manageability, and flexibility from their systems. Tight service coupling and high interdependence across services in monolithic systems prove to be a barrier in achieving what modern enterprises want.

Microservices allow services in a greater system to be systemically independent and loosely coupled so that enterprises have greater flexibility and agility. As enterprises take note of the benefits of Microservices, more effort and resources are being expended to develop new supporting systems or upgrade existing products. The Spring Boot has been one such system which helps enterprises to implement the Microservices. The Spring Boot harnesses all good things about the Spring Framework that are necessary and customizes the Spring framework for Microservice development. You can say that the Spring Boot is a micro framework that makes building Microservices easier because it packages necessary modules from the superset so that developers can choose necessary modules without getting overwhelmed.

Microservices – What is it?

Microservices, also known as the Microservices Architecture, are a group of loosely coupled software services that constitute a software suite. The services are well known as modules and they can manage it independently. The services can are different from one another. The services need to communicate with one another through various ways and it depends on the software requirements. Still, some of the standard protocols used by the developers are HTTP/REST with JSON or Protobuf.  DevOps professionals may use any protocol they think is suitable. Generally, the Representational State Transfer (REST) protocol is considered useful because of the comparatively lower complexity Probably the best way to understand Microservices and its benefits to compare with monolithic software systems.

Microservices address certain limitations of monolithic systems. Services in a monolithic system are tightly coupled. If one or more services are updated or fixed, the other services also gets affected because all services must be recompiled and redeployed. Monolithic systems also have a single run time system which impacts all services, required or not. So, monolithic systems’ disadvantages are unnecessary redeployments; tightly coupled services offer minimal or no flexibility; inefficiency and high expenses. Compare monolithic systems with the Microservice Architecture. One, if you need to update or fix a service, you do so without impacting the other services. You may, however, need to review the communication protocol post the update or fix. Two, since the services are loosely coupled, replacement is easy without impacting the system. Three, you always can use services based on requirements regardless of how they communicate or depend on one another. Four, when you redeploy, only the impacted service is redeployed.

What is spring boot?

The Spring Boot is a new framework that helps you build Spring applications. While that sounds nothing unique about Spring Boot, how it helps you build Spring applications. It offers certain unique ways that help you build applications easily. First, it helps you avoid writing verbose code, also referred to as Boilerplate software development by simplifying code writing. Second, it aims to free software developers from XML dependency, something that was quite intrinsic with the Spring framework. The Spring framework, faced heavy criticism because of its bulky XML configuration. Spring Boot not only reduces the use of XML while building software applications but even makes the bold claim of freeing developers from writing import statements.

Microservices & Spring boot – What is the relation?

Spring Boot makes developing Microservices easier because software developers can choose, and use required components from the framework without needing to spend too much time on details of the components. Spring Boot rests on top of the Spring Framework and it gives the developers what they need so developers do not need to scour the framework and spend time on building components for the Spring application. It is a kind of mini framework. Spring Boot facilitates greater productivity by making concepts such as RESTful HTTP and embedded web application easy to use and deploy. Spring Boot provides a convenient mechanism for building applications, known as the Spring Initializer. It makes development easy by allowing developers to choose exactly the libraries they need, providing metadata and just generating the project. Overall, development of Microservices can be expedited using Spring Boot.

Sample application & Configuration

For creating microservices (by using Spring framework), we need to follow certain steps and they are as follows:-

Registration of service: –

Microservices are actually different processes working together. It is a service based communication instead of a component based architecture. Now, to find those Microservices, some registry has to be maintained. And, all the microservices will be registered there. There are couple of registration servers (open source) available in the market. One is called ‘Eureka’ which is incorporated by Spring cloud. Spring cloud also supports ‘Consul’, which is an alternative to ‘Eureka’.

Listing 1: – Sample registration server

@SpringBootApplication
@EnableEurekaServer
public class ServiceRegServer {

  public static void main(String[] args) {
    // Spring Boot will look for registration-server.yml
    System.setProperty("spring.config.name", "registration-server");
    SpringApplication.run(ServiceRegServer.class, args);
  }
}

Configuration (pom.xml):

In this section, we will check the main components in pom.xml configuration file.

Listing 2: – Sample pom.xml configuration file

<parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>_Brixton_.RELEASE</version> 
</parent>
<dependencies>
        <dependency>
            <!-- Setup Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <!-- Setup Spring MVC and REST, using embedded Tomcat server -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <!-- Spring Cloud starter -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
        <dependency>
            <!-- Take Eureka for service registration -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
 </dependencies>

Properties file:
Spring boot application always check application.properties or application.yml file for configuration related information. In this application, we will have application.yml (reg-srever.yml) file for storing configuration related information.

Listing 3: – Sample registration server

# Discovery server configuration
eureka:
  instance:
    hostname: localhost
  client: 
    registerWithEureka: false
    fetchRegistry: false
server:
  port: 8761 # HTTP (Tomcat) port

Now, if we open the Eureka dashboard at http://localhost:8761, applications section will be empty. So, we need to register the microservice with the discovery server. Now, if we refresh the dashboard, it will show the microservice there.

Spring boot start-up class configuration:
This is the Spring boot start-up class. This is the main entry point to the application.

Listing 4: – Sample Spring boot starter class.

@EnableAutoConfiguration
@EnableDiscoveryClient
@Import(SampleWebApplication.class)
public class AccServer {
    @Autowired
    AccRepository accRepository;
    public static void main(String[] args) {
        // Configure using acc-server.yml
        System.setProperty("spring.config.name", "acc-server");
        SpringApplication.run(AccServer.class, args);
    }
}

Registration with discovery server
Here, we will register the class with the discovery server.

Listing 5: – Register with the discovery server

# Spring properties details
spring:
  application:
     name: acc-service
# Discovery Server Access Details
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
# HTTP Server details
server:
  port: 8080 # HTTP (Tomcat) port

Now, refresh the dashboard, and it will show the microservice listed under the applications sections.

Conclusion: –
It seems improving developer productivity and saving time are two of the most important features of the Spring Boot and Microservices combination. However, this also needs to be noted that it is still a relatively new concept and the jury is out on its universal applicability and the tangible benefits are yet to accrue.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -