|
1 | | -# {PN} Common Library |
| 1 | +# Spring Boot Microservice Common Library |
2 | 2 |
|
| 3 | + |
3 | 4 |
|
4 | | -PN - project name or project name abbreviation |
| 5 | +A comprehensive common library built with Spring Boot 3.3 and Java 21, designed to streamline and standardize the development of microservices within your projects. |
| 6 | + |
| 7 | +PN - project name or project name abbreviation |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +### Logging |
| 12 | + |
| 13 | +- **Standardized Request/Response Logging**: Automatically logs incoming requests and outgoing responses based on the base project package. Provides options to exclude request or response bodies from logging when they are excessively large. |
| 14 | + |
| 15 | +- **Event Logging**: Seamlessly logs producer and consumer events, enhancing traceability across microservices. |
| 16 | + |
| 17 | +- **Error Context Logging**: Captures consumer errors by adding a specific MDC (Mapped Diagnostic Context) field with the value `EVENT_ERROR`. Similarly, logs errors related to methods annotated with `@Async` by adding an MDC field with the value `ASYNC_ERROR`. |
| 18 | + |
| 19 | +### Feign Configuration |
| 20 | + |
| 21 | +- **Unified Feign Client Configuration**: Allows all project-specific microservices to utilize a centralized `FeignConfig`, eliminating the need for individual configurations. This approach ensures consistent error decoding and facilitates the propagation of project-specific headers across microservices. |
| 22 | + |
| 23 | +### OpenTelemetry Integration |
| 24 | + |
| 25 | +- **Automatic Trace Exporting**: Automatically exports span and trace information for each microservice to OpenTelemetry, enabling effective tracking and monitoring of distributed systems. |
| 26 | + |
| 27 | +### Swagger Integration |
| 28 | + |
| 29 | +- **Springdoc OpenAPI Integration**: Integrates with Springdoc OpenAPI, allowing each microservice to document its APIs. Minimal configuration changes in `application.yaml` are required to display specific API details; otherwise, common API details are utilized. |
| 30 | + |
| 31 | +### Global Error Handling |
| 32 | + |
| 33 | +- **Comprehensive Error Handling**: The `CommonErrorHandler` addresses a wide range of common exceptions and provides a standardized error response model. It dynamically captures error codes for `MethodArgumentNotValidException` and `ConstraintViolationException`, ensuring uniform logging conventions for all exceptions. Microservices should extend the `CommonErrorHandler` in their respective error handlers to maintain consistency. |
| 34 | + |
| 35 | +### Request Interceptor |
| 36 | + |
| 37 | +- **Header Propagation and Logging**: Extracts all project-specific headers from incoming requests and sets them as Logstash MDC fields before processing the request. These fields are cleared after the request is processed. This mechanism allows for tracking logs using indexed fields; for instance, monitoring all logs associated with a specific customer using the `pn-customer-id` field. |
| 38 | + |
| 39 | +### Messaging |
| 40 | + |
| 41 | +- **Event Models and Retry Logic**: Provides common event-related models and implements retry logic to enhance the reliability of message-driven microservices. |
| 42 | + |
| 43 | +### Utilities |
| 44 | + |
| 45 | +- **Utility Classes**: Offers a collection of utility classes that can be leveraged across microservices to promote code reuse and consistency. |
| 46 | + |
| 47 | +- **Standardized Configuration Files**: Includes common `checkstyle.xml` and `logback.xml` files. Housing these files in the common library ensures that microservices consume them in a read-only manner, preventing unauthorized modifications and maintaining uniformity across projects. |
| 48 | + |
| 49 | +## Usage Configuration |
| 50 | + |
| 51 | +To incorporate the common library into your project, add the following dependency: |
| 52 | + |
| 53 | +### Gradle |
| 54 | + |
| 55 | +```groovy |
| 56 | +implementation "com.company.project:springboot-microservice-common-lib:${pnCommonLibVersion}" |
| 57 | +``` |
| 58 | + |
| 59 | +### Maven |
| 60 | + |
| 61 | +```xml |
| 62 | +<dependency> |
| 63 | +<groupId>com.company.project</groupId> |
| 64 | +<artifactId>springboot-microservice-common-lib</artifactId> |
| 65 | +<version>${pnCommonLibVersion}</version> |
| 66 | +</dependency> |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | +**Note:** This common library is not yet available in the central repository. To use it locally, clone this repository and execute `gradle publishToMavenLocal` to publish it to your local Maven repository. |
| 71 | + |
| 72 | +### Bean Detection Configuration |
| 73 | +To ensure that Spring detects and registers the beans provided by this common library, add the following class to the config package of your microservice: |
| 74 | + |
| 75 | +```java |
| 76 | +import org.springframework.context.annotation.ComponentScan; |
| 77 | +import org.springframework.context.annotation.Configuration; |
| 78 | + |
| 79 | +@Configuration |
| 80 | +@ComponentScan(basePackages = "com.company.project.common") |
| 81 | +public class CommonLibScannerConfig { |
| 82 | +} |
| 83 | +``` |
| 84 | +This will enable automatic scanning and registration of common library components. |
| 85 | + |
| 86 | + |
| 87 | +## Reference Implementation |
| 88 | +For a practical implementation example, refer to the Spring Boot microservice template that utilizes this common library: |
| 89 | + |
| 90 | +[Spring Boot Microservice Template](https://github.com/mmushfiq/springboot-microservice-template) |
0 commit comments