Spring Boot Microservice Best Practices and Integrations

Feb 18, 2022 | Programming

In today’s fast-paced technological landscape, building robust microservices is crucial for effective software solutions. This guide will consolidate best practices and integrations for Spring Boot-based microservices into a user-friendly format.

Table of Contents

Introduction

This project is intended to bring arguably best practices and integrations available for Spring Boot-based Microservices in a single repository. Developers can use this repository as a template to build their own Microservice by adding or removing dependencies as per requirement.

Prerequisites

  • Java 21 Installed.
  • To run the application against the actual Companies House API, you will need to create a free account here. Once created, replace the authUserName in the application.yaml under the main directory.

Installation and Getting Started

Let us get started by cloning or downloading the repository to your workstation. Once cloned or downloaded, import the project in your favorite IDE (IntelliJ, Eclipse, etc.).

We use Gradle Wrapper for dependency management, so you do not need to explicitly configure Gradle or Maven.

./gradlew clean build

As the codebase grows, visualization becomes vital. Visualization provides enough of a “fingerprint” that viewers can glance at it and see the structure. Thanks to repo-visualization for helping achieve this.

Microservice Structure

We are following the Classic Microservice Separation of Concerns pattern having Controller, Service, and Connector layers. Each layer has its responsibilities:

  • Controller: Handles incoming requests and invokes business logic to respond.
  • Service: Implements business logic, keeping it separate from controller responsibilities.
  • Connector/Repository: Fetches data needed by the Service layer; interacts with external services or databases.

Development Practice

In our software development process, we embrace Behavior Driven Development (BDD) and Test Driven Development (TDD), thus ensuring high-quality code output.

A feature is not considered developed until all the Unit Tests (TDD) and features (BDD) are passed!

Integrations

1. Testing

1.1 Unit Test

We utilize JUnit 5 for running unit test cases.

./gradlew test

After execution, a report will be generated at build/reports/tests/test/index.html.

1.2 Cucumber End to End Test

We use Cucumber for BDD testing.

./gradlew cucumber

Test reports will be available at build/reports/cucumber/index.html.

1.3 Mutation Testing

Use Pitest for mutation testing.

./gradlew pitest

Reports can be found at build/reports/pitest/[TIMESTAMP]/index.html.

2. Development Accelerators

2.1 Mapstruct

A great library for converting VO to DAO objects and vice versa.

2.2 Lombok

Provides annotations for auto-generation of methods, logging, builders, etc.

2.3 WireMock

Great for simulating HTTP APIs.

3. Analysis and Quality Checks

3.1 Checkstyle

A static code analysis tool for checking Java source code compliance with coding rules.

./gradlew check

Checkstyle reports will be generated at build/reports/checkstyle/main.html

3.2 Jacoco

For code coverage, Jacoco is a must!

./gradlew test -Pexcludee2e=true
./gradlew jacocoTestReport
./gradlew jacocoTestCoverageVerification

Jacoco reports are located at build/reports/jacocoHtml/index.html.

3.3 Hadolint

A smart Dockerfile linter that helps you build best practice images.

hadolint Dockerfile

4. API Documentation

Including SpringDoc-API enables clean documentation for APIs.

5. DevSecOps

DevSecOps integrates security practices into the DevOps lifecycle for a holistic approach.

5.1 Dependency Vulnerability Check

Use OWASP Dependency-Check for identifying vulnerabilities within dependencies.

5.2 Docker Image Vulnerability Check

Utilize Trivy for scanning Docker images.

5.3 Infrastructure as Code Static Analysis

Static analysis can be conducted on the IAC.

5.4 Penetration Test

Explore OWASP ZAP for performing penetration tests.

6. Continuous Integration, Delivery and Deployment

Continuous Integration helps teams integrate work frequently, with automated builds verifying changes.

6.1 Docker Containerization

Docker is a widely adapted platform for containerization, simplifying deployment across environments.


FROM openjdk:21-slim
WORKDIR /opt
COPY build/libs/companieshouse-0.0.1-SNAPSHOT.jar companieshouse-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar", "companieshouse-0.0.1-SNAPSHOT.jar"]

This Dockerfile serves as an example for building a Spring Boot application into a Docker image.

6.2 CI and CD Pipeline Tools

CircleCI

CircleCI is a SaaS tool that makes CI/CD processes seamless and efficient.

Concourse

An open-source CI/CD tool focused on simplicity and scalability.

Jenkins

A widely used tool that integrates well with various CI/CD pipelines.

Google Cloud Build

Executes your builds on Google Cloud Platform infrastructure, allowing you to build Docker containers or Java archives.

7. Platforms

7.1 Kubernetes

Kubernetes is essential for managing containerized applications.

7.2 Google Cloud Run

A fully managed service that automatically scales your stateless containers.

What to expect Next!

Future enhancements to the repository may include:

  • GitHub Actions
  • Chaos Monkey
  • Hystrix
  • CORS (Cross-Origin Resource Sharing)

Versioning

We adhere to SemVer for versioning our project.

Author

Abhishek Singh Rajput – Initial work – @abhisheksr01

Contributors

Everyone is welcome to enhance this project with new features or improvements.

License

This project is licensed under the MIT License.

Acknowledgments

Special thanks to Eugen Paraschiv for his invaluable tutorials.

Troubleshooting

If you encounter issues during implementation, consider the following troubleshooting tips:

  • Ensure you have the necessary dependencies configured in your build file.
  • Double-check API credentials and permissions.
  • Inspect logs for error messages that can guide fixes.

For further assistance or collaboration opportunities, feel free to connect with us at **fxis.ai**. For more insights, updates, or to collaborate on AI development projects, stay connected with **fxis.ai**.

At **fxis.ai**, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox