Mastering Microservices: Implementation of Sequence Generator

Mastering Microservices: Implementation of Sequence Generator

ยท

3 min read

๐Ÿš€ Welcome to our Blog on Implementing the Sequence Generator Service in a Spring Boot Microservice Banking Application! ๐ŸŒ๐Ÿ“ˆ

Embark on a journey to master the intricacies of sequence generator within our microservices-driven banking system. Whether you're a seasoned developer or a tech enthusiast, join us for hands-on guidance and an in-depth exploration of constructing a robust Account Service.

Development

Now, move to your favorite IDE or to the Spring Initializer and create a Spring boot Application with the following dependencies

Now, what are we gonna use?

Model Layer

Entity

Now, we will craft an entity named Sequence in the model.entity package. This entity encompasses attributes such as accountNumber and a primary key, crucial for the generation of a sequence.

Sequence.java

package org.training.sequence.generator.model.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Sequence {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long sequenceId;

    private long accountNumber;
}

Repository Layer

The repository is primarily an interface that contains high-level methods facilitating interaction with the database. Here, we are creating the SequenceRepository interface in the repository package.

SequenceRepository.java

package org.training.sequence.generator.reporitory;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.training.sequence.generator.model.entity.Sequence;

public interface SequenceRepository extends JpaRepository<Sequence, Long> {
}

Service Layer

We will be creating only one method for creating the sequence.

SequenceService.java

package org.training.sequence.generator.service;

import org.training.sequence.generator.model.entity.Sequence;

public interface SequenceService {

    Sequence create();
}

SequenceServiceImpl.java

package org.training.sequence.generator.service.implementation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.training.sequence.generator.model.entity.Sequence;
import org.training.sequence.generator.reporitory.SequenceRepository;
import org.training.sequence.generator.service.SequenceService;

@Slf4j
@Service
@RequiredArgsConstructor
public class SequenceServiceImpl implements SequenceService {

    private final SequenceRepository sequenceRepository;

    @Override
    public Sequence create() {

        log.info("creating a account number");
        return sequenceRepository.findById(1L)
                .map(sequence -> {
                    sequence.setAccountNumber(sequence.getAccountNumber() + 1);
                    return sequenceRepository.save(sequence);
                }).orElseGet(() -> sequenceRepository.save(Sequence.builder().accountNumber(1L).build()));
    }
}

Controller Layer

Now, let's expose the method as API enpoint.

SequenceController.java

package org.training.sequence.generator.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.training.sequence.generator.model.entity.Sequence;
import org.training.sequence.generator.service.SequenceService;

@RestController
@RequiredArgsConstructor
@RequestMapping("/sequence")
public class SequenceController {

    private final SequenceService sequenceService;

    @PostMapping
    public Sequence generateAccountNumber() {
        return sequenceService.create();
    }
}

Adding the configurations required in the application.yml file:

spring:
  application:
    name: sequence-generator

  datasource:
    url: jdbc:mysql://localhost:3306/sequence_generator
    username: root
    password: root

  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        format_sql: true
server:
  port: 8083

Now, lets the add the route defination in the API Gateway we created in Mastering Microservices: Setting API Gateway with Spring Cloud Gateway so that the service is routed through the API Gateway.

- id: sequence-generator
  uri: lb://sequence-generator
  predicates:
      - Path=/sequence/**

Given that this service doesn't necessitate any exception handling configuration or inter-service configuration, you can execute the application and observe the results. However, ensure to delete the data generated in the database upon execution, enabling the sequence to commence from 1.

Postman Collection

I'm using Postman to test the APIs, and I will be attaching the Postman collection below so that you can go through it.

Run In Postman

Conclusion

Thanks for reading our latest article on Mastering Microservices: Implementation of Sequence Generator with practical usage.

You can get source code for this tutorial from our GitHub repository.

Happy Coding!!!!๐Ÿ˜Š

Did you find this article valuable?

Support dev' Scribblz by becoming a sponsor. Any amount is appreciated!

ย