Skip to content

VehicleRestAPI, a Spring Boot microservices project, empowers CRUD operations and filter-based vehicle data search. Utilizing an API gateway, Eureka Server, and Config Server, it ensures scalability and security with JWT. Resilience4j oversees circuit-breaking, and Test Containers ensure rigorous unit testing for enhanced reliability.

Notifications You must be signed in to change notification settings

Emon-Khan/VehicleRestApi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VehicleRestApi

VehicleRestAPI is a robust microservices-based project developed using Spring Boot, adhering to the principles of microservices architecture. The project provides a comprehensive set of functionalities, including CRUD operations (GET, POST, PUT, DELETE) for vehicle-related data. It incorporates advanced features like filter-based search options, making it a versatile solution for managing vehicle information.

Key Features

  1. Microservices Architecture: The project is designed with a microservices architecture to enhance scalability, flexibility, and maintainability.

  2. API Gateway: An API gateway is employed to manage different services' ports efficiently, ensuring seamless communication between microservices.

  3. Service Registry (Eureka Server): Eureka Server is utilized for service registration and discovery, enabling effective management of microservices.

  4. Configuration Management: Configuration settings are stored in a centralized GitHub file and connected to all services using a Config Server. This ensures consistent configurations across the entire application.

  5. Authentication (JWT): JSON Web Token (JWT) is implemented for secure authentication, providing a robust and scalable method for user verification.

  6. Circuit Breaker (Resilience4j): Resilience4j is employed for circuit-breaking mechanisms, offering features like fault tolerance, retry, and rate limiting to enhance system resilience.

  7. Unit Testing with Test Container: Unit tests are conducted on DAO and service layers using Test Containers, ensuring the reliability and correctness of critical components.And it has used Behavior-Driven Development approach for testing.

  8. DTO (Data Transfer Object) and Object Mapper: To fetch data from the vehicle details service into the vehicle search service, a DTO class is utilized. The Object Mapper concept is employed to efficiently map and transfer data between services.

Demo

In this section i will show you some of my project's features.

Service Registry

So, for runing this project we need to at first start the ServiceRegistry then we will start the other services.But one thing we have to keep in our mind that we have to start service registry first otherwise it will give us some error message.

After start all of the services if we go to the Eureka Server's default port no 8761 alongwith localhost as host.Then we will be able to get some info like this-

EurekaClient

Authentication & Authorization

Now if we want to enter any rest end point into postman and try to run it we will give us some error.Because at first we need to authenticate and based on the role we are given(user/admin) it will check are the user/admin role has the privilege to run this particular endpoint or not.

Now run the authentication using this endPoint http://localhost:9196/api/v1/vehicle-search/auth/authenticate If we give the correct information then it will give us a bearer token.And using that bearer token we can access the other endpoints.And one thing i have forgot to mention if you haven't register here yet then you can also register by using this endpoint http://localhost:9196/api/v1/vehicle-search/auth/register .Although you can see those endpoints by going to the vehicleSearch->auth->authcontroller class.But for making things simple i have mention these endpoints in here.

Authenticate

image

Register

image

Database

image

Crud Operation

In here I will do some of the crud operation inside vehicleDetails service.

GET

This operation will findout all of the vehicle details information available in the database.

1.VehicleDetailsGetMappingCroppedOne.mp4

GETBYID

image

If the id isn't available on the database then it will give a exception.

image

PUT

image

Role Based Operation

In my project i have given user and admin all of the privileges but in delete operation admin can only do the deletion.

image

As I am now logged in as a USER.Not as ADMIN Role. Point To Be Noted - I know this is not the write way to figure out that i can't do the operation.I need to handle the exception but for some of the fields i didn't handle it.But will fix this in future.

Logged in as an ADMIN

image

Now do the deleteion and let's see what happens

image

I can successfully delete that particular manufacturer.

ManyToMany Mapping

I have made an endpoint which actually make relationship between 3 table as it is a manytomany mapping it will create a new table.Here the new Table name is model_trim and inside that table model and it's coresponding trim id has been listed.

image

And new model has been listed to the model table and there is also a reltionship between model and manufacturer.So, inside model table according to each model corresponding manufacturer_id also has been listed.

image

When we are adding a new model if the trim has not been inside the trim table of database.Then it will not added that trim to the model_trim table.Only the existence trim will be added on the model_trim.

And it is happening because of of the cascade = CascadeType.MERGE.Which has been set on the Model entity class.

Trim

image

image

Here i have used trim types and those trim types already exits in the trim table.So, in the model_trim table it will make relationships with the newly created model id. Manufacturer which i have declare in here it is also showing in the manufacturer table too.So, this will also make a relationship with newly created model.

image

Model Table

image

ModelTrim Table

image

Trim Table

image

Manufacturer Table

image

As you can see model and model_trim table has just changed.And the 2 tables remains the same.

But now if i pass a trim type which is not in the trim table then what will happen? Now let's observe this scenario.

image

image

I can't add this model because the trim type isn't available on the trim table.

Now, let's see what will happen if we pass a manufacturer which doesn't exist on the manufacturer table.

image

image

Delete

One question will come into your mind what will happen if we delete a model. So, when doing deletion operation it will delete the model and corresponding model trim relationship from the model_trim table.

Let's do this operation----

Before deletion database tables----

Model image

ModelTrim image

Trim image

Manufacturer image

After Deletion image

Model image

ModelTrim image

Trim image

Manufacturer image

So, we are getting the output as per our expectation.

Fetch values from another Service

In our project we got many services but the values are coming from 2 services one is vehicle details and another is vehicle search.

MicroServiceArchitechure

In vehicleSearch we have to fetch value from vehicleDetails to display some endpoints value.For that we have use restTemplate.And we have use the mapper concept to fetch those values.

image

image

image

Here we are getting values from vehicleDetails service.And also getting some additional value with it.

Circuit Breaker

We have applied circuit breaker in this method.So, if for any reason the vehicle details service is down then it will show the end user some diffult values.

image

As you can see vehicleDetails service is not there in the Eureka server's client list.So, the vehicleDetails service is down.

Now, if we send the request to get the vehicle details value.We will be getting some dummy data.

image

We have also applied retry functionality so that, if the service is little bit slow then it will automatically send the request to the vehicleDetails service and try to connect with it.If according to our given requirement still it can't able to connect with vehicleDetails service then it will show the dummy data.

image

We can also check the health status and other info using actuator.And observing those value we can get an idea of our services present condition.

image

We have also applied rate limiter in this method.Rate limiter actually rate the limit of sending request to a particular endpoint.This thing actually secured our application by defending the DoS attack.And for checking is it working correctly or not.We have used Jmeter.

Search Vehicle By Filter

Fetching value from the vehicleDetails Service By passing value as parameter.

Showing value for the modelYear 2019

image

BusinessLogic

To find out the estimated monthly price we have use Compount interest rule.

image

Testing

For doing the testing we can't store the value inside the databases.Then it will be kind of look like manual process of doing the testing. Instead, we aim to automate testing procedures for efficiency.

For doing the test for controller layer we need to mock the data.That's why we are using Mockito framework.ObjectMapper also has been used here to make a relationship between 2 services.We can fetch value from another service by using the ObjectMapper.And for testing we have used jUnit5.

image

image

Both AAA (Arrange-Act-Assert) and GWT (Given-When-Then) are patterns in Behavioral-Driven Development, serving the same purpose of structuring test cases.In this test cases i have followed the Behavioral Driven Development.

Specially when it's come to writing the unit test cases for the dao/repository layer then we need to store data inside the database temporarily.So, that's why here we can use h2 embedded database.But testContainers spins up actual database instances in Docker containers, mirroring production environments more accurately than H2's in-memory database. This approach mitigates inconsistency between testing and production, enhancing the reliability of unit tests for database-related code.

image

image

Related

I have use an agile project management tool (Jira) for tracking the project.And named the branches of my project based on the task name, along with a generated task number. image image

About

VehicleRestAPI, a Spring Boot microservices project, empowers CRUD operations and filter-based vehicle data search. Utilizing an API gateway, Eureka Server, and Config Server, it ensures scalability and security with JWT. Resilience4j oversees circuit-breaking, and Test Containers ensure rigorous unit testing for enhanced reliability.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages