spring boot microservices maven architecture - spring

I am currently building a spring boot application with a micro-service architecture.
I am looking for clean ways to reuse code.
My idea is to pull out common code in a shared module.
(e.g. base classes which model classes in microservices inherit from, interfaces which are reused in any mvc controller, domain code which is the same for each bound context).
Concrete implementations and service-only model classes etc. are on the submodule (microservice) level.
I am building stuff with maven and also managing dependencies. My question is about how to structure maven modules and dependencies in such a setup.
Where do shared libraries go? How and when are they built and referenced via dependency?
Is it good to use a BOM for managing dependencies and only override specific versions in microservices when needed?
How is the bom included? How and when is it built?
I can think about one way to handle it which I don't really like:
Have a (super-parent) maven pom which handles dependency management and can be included in submodules (micro-services) with import scope. (BOM)
microservices poms build all their dependencies (including BOM and shared module)
Problems:
when declaring submodules for building dependencies the pom must be of type pom aswell, so for every microservice module I would also need a parent pom aswell
If the super-parent or BOM includes a module section which declares all submodules (microservices) I cannot build it from the ms-level, since there would be circular dependencies.
Why would I even declare submodules in the bom? because in some IDEs like Intellij the maven plugin/support includes the submodules in the structure overview.
Can you please point me to some good advice for handling maven architecture for microservices in a solid and clean way? (less code/configuration duplication as possible, performant build etc.)
Thanks

The first thing you should concern about with the microservices architecture is the isolation of the services from each other. That implies that you should share as little as possible.
As I tried different approaches over past 6-7 years I can confidently say
Don't ever use a parent pom for different microservices. Each microservice can use different version of the same libraries depending our their own needs. Having parent pom may sound convenient initially but it creates significant issues in the long run for isolation.
Don't ever try to focus on code reuse between the microservices. That doesn't mean that you can't use utility libraries etc. But sharing model classes etc. will bind the microservices to each other which will damage the freedom/isolation substantially. Instead you should rely on proper contract between the microservices.
I see that your approach are mostly for implementation convenience but there are other things you should concern more over time like deployment/release, maintenance, testing of the components which could be harmed by lack of proper isolation.

Related

Shared data classes in microservice architecture with Spring and Kotlin

Suppose you have two microservices talking to each other, both share data classes as contracts. While you dont want to duplicate all contracts in every microservices project, whats the best way to share them?
As is see it, there are only two options:
Duplicate shared contracts, while also duplicating code
Compile a first party library and import it
My team had the same scenario some time ago and we decided for a variant of your second point. We used Maven Multi-Module Project, so that one Microservice produced the library, which was then imported by the 2nd Microservice.
Maven Multi-Module Project is explained here:
https://www.baeldung.com/maven-multi-module
It is not a good idea to do project1 > project2 dependency because it might change in the future and you'll end up having to refactor the whole thing.
What I'd do is to create a shared library that's published to an internal Nexus or artifactory, then you can just add this as a dependency to your project so you'll have something like this:
library > project1
library > project2
Another idea to consider is to use something like Protocol Buffers if you are only sharing the data transfer objects. This has the additional advantage of versioning and forward compatibility.

Spring Boot Multi Module and Fat jar with Shared Features

Experts,
I need some expert advice on how to approach the below use case in spring boot.
I need to have a maven multi-module approach to my project.
I need to have a single jar as output of the final build process.
There are to be common modules for controllers, data access and other functionality
Other modules are to be created based on functionality domain for eg a module for Payroll, a module for Admin etc etc.
Each domain functional module will then have their own controllers extending the common controller, exception handler and so on.
Each module will also have its own set of thyme leaf pages.
The reason for following such an approach is we have development in phases and we will be rolling out based on functional modules.
Here are the issues that I can sense using this approach.
Where do I add the spring web dependency? If I add to the parent pom - it gets replicated across the children and there will be port conflict issues as each module loads. the same issue will also be there the moment I add it to two child modules.
How do I build the fat jar which has all the jars from all modules and works as the final deployment?
All the text that I read i can't see anything even close to what I am trying to achieve.
AD1. They will not unless you are trying to setup independent application context in each module. Of course you can do that(it might be complicated but I believe it's achievable), but for me it's an overkill. Personally I think it's better to have one application context and rely on scanning components that are present in classpath.
AD2. The structure in maven might be a little bit complicated and overwhelming at first glance but it makes sense. Here's how I see it:
Create a parent module that will aggregate each module in project and will declare library/plugin dependencies for submodules.
Create 1-N shared submodules that will be used in other modules. With come common logic, utils, etc.
Create 1-N submodules that will be handling your business logic
Create an application submodule that creates application context and loads configuration and components from classpath
Create a submodule that will be responsible for packaging process, either to war, jar, uber-jar or whatever else you desire. Maven jar plugin should do that for you. For executable uber-jar, you have dedicated tool from spring.
Now you can choose three ways(these ways I know) of loading your modules.
1. Include some modules in maven build based on the build configuration via maven profiles and let spring IoC container load all the components he finds in the classpath
2. Include all of the modules in maven build and load them depending on spring active profiles - you can think about it as of feature flag. You annotate your components or configuration class with #Profile("XYZ") telling spring IoC container whether to instantiate component or not. You will need (most flexible solution) to provide a property file which tells spring which profiles are active and thus which modules should be loaded
3. Mix of these two above.
Solution 1 pros:
build is faster (modules that are not included will be skipped during build)
final build file is light (modules that are not included are... not included ;))
nobody can run module that is not present
Solution 1 contras:
project descriptor in maven may explode as you might have many different profiles
Solution 2 pros:
it's fairly easy and fun to maintain modules from code
less mess in project descriptor
Solution 2 contras:
somebody can run module that is not intended to be run as it's present in classpath, but just excluded during runtime via spring active profiles
final build file might be overweight - unused code is still present in code
build might take longer - unused code will be compiled
Summary:
It's not easy to build well structured project from scratch. It's much more easier to create a monolith and then split it into modules. It's because if you already created a project, you've probably already identified all the domains and relations between them.
Over past 8 years of using maven, I honestly and strongly recommend using gradle as it's far more flexible than maven. Maven is really great tool, but when it comes to weird customization it often fails as it's build capabilities rely on plugins. You can't write a piece of code on the fly to perform some custom build behaviour while buidling your project, you must have a dedicated plugin for doing that. If such plugin exists it's fine, if it's not you will probably end up writing your own and handling its shipment, so anyone in your company can easily perform project build.
I hope it helps. Have fun ;)

How should be project structure using microservices with gradle or maven?

I want to be sure what is the best practice for project structure while using microservice architecture.
All microservices can be created as a new maven/gradle project or as a subproject/module.
I think dependency inheritance, project repository should be taken into account.
Due to the nature of the microservices, any service can has a different technology but still most of the services can have same dependencies(e.g. spring-boot)).
Another issue is that should team fetch all services or just a service which will be worked on? so repository structure also will be affected by the structure.
The main reasons to have a multi-module project is to manage dependencies (in the maven sense) between the different modules (and build them together in the right order). Your microservices may call each other, but are not dependent on each other in the Maven sense. So I would not use multi-module projects for them.
Nevertheless, you can define a parent pom for your microservices that defines common configuration and dependencies.
I don't recommend you to have a single project with multiple microservices of different technologies.
If you use Java as a programming language you can have a master project and use it as a parent in your microservices projects. Also, you can have common libraries as dependencies of your microservices projects.
If you want to do a microservices with different technology I recommend you to have a repository for each microservice.
Choosing this option you can deploy and versioning each microservice when you do changes in its code and not when you have changes in another microservice.

Spring boot parent pom with custom parent

I read a lot of posts regarding the ways to use spring-boot-starter-parent in a spring boot project.
Essentially, I read posts (Spring documentation also talks about this) describing two ways to do this
To use spring-boot-starter-parent as the project parent directly. It gives us the benefits of having the dependency management as well as the plugin management.
The other way is to import the spring-boot-starter parent in the project pom (we may need this in case we already have a parent pom for the project).
It allows us to get the benefits of dependency management but not the plugin management)
I am creating a new Maven multi module project. Ideally I would like to have my own custom parent and also get all the benefits of using the Spring-boot-starter-parent.
I was wondering if it made sense to create a custom parent for my maven projects. This parent would in turn be a child of the spring-boot-starter-parent.
If I am not missing anything, this way I could get the benefits of having the dependency management and plugin management from spring-boot-starter-parent and at the
same time have a custom parent for all my projects where I could define some other common dependencies or if needed override the dependencies defined in the
spring-boot-starter-parent which would then be inherited by all my projects.
Does this design make sense or am I missing something.
What are the drawbacks of this approach?
There are no drawbacks -- this is exactly what you're meant to do if you want a multimodule spring-boot project. However, consider this: typically multi-module projects have all modules versioned together, released together, and dependant on each other. This rarely makes sense in a group of spring-boot modules, which are typically of the micro-service style and which require independent evolution. So, you should question your need for a multi-module project at all.

Maven Multi Module benefits over simple dependency

I have some years of experience with maven projects, even with multi modules ones (which has made me hate the multi modules feature of maven (so the disclaimer is now done)) and even if I really like maven there is something I cannot get a clear answer about :
What is a typical usecase of a multi module maven project ? What is the added value of such a structure compared to simple dependencies and parent pom ?
I have seen a lot of configuration of multi module projects but all of them could have clearly been addressed by creating a simple structure of dependency library living their own life as deliverables (even with a parent pom, as a separate deliverable : factorising depedencies and configuration) and I haven't found any usecase where I can clearly see an added value of the multi module structure.
I have always found that this kind of structure brings an overkilling complexity with no real benefit : where am I missing something ? (to be truly honest, I can get that some ear can benefit from this kind of structure but except from that particular usecase, any other real use and benefit ?)
Here's a real life case.
I have a multi-module project (and to your rant... I haven't seen any complications with it.) The end result is a webapp but I have different modules for api, impl, and webapp.
12 months after creating the project I find that I have to integrate with Amazon S3 using a stand-alone process run from a jar. I add a new module which depends on api/impl and write my code for the integration in the new module. I use the assembly plugin (or something like it) to create a runnable jar and now I have a war I can deploy in tomcat and a process I can deploy on another server. I have no web classes in my S3 integration process and I have no Amazon dependencies in my webapp but I can share all the stuff in api and impl.
3 months after that we decide to create a REST webapp. We want to do it as a separate app instead of just new URL mappings in the existing webapp. Simple. One more module, another webapp created as the result of the maven build with no special tinkering. Business logic is shared easily between webapp and rest-webapp and I can deploy them as needed.
The major benefit of multi modules are
one single maven command to build all your modules at once.
and the most important : maven take care of the build order for you.
configuring your CI-server is also very easy: one single jenkins job to build everything.
I already worked in a project with about 30 submodules. Sometimes, you need to change something in more than module, and running one single command and being sure that everything that need to be compiled is compiled in the correct order is a must.
EDIT
Why 30 submodules ?
Huge framework with lot's a features, lot's of developers, separation of features on a module base. It's a real life use case and the separation of the code into module was really meaningful.
I think you are correct in that most project that use multi modules, actually don't need them.
At where I work we use multimodule projects (and I think that for a good reason). We have something similar to a service oriented architecture, so each application
A client module
An interface module (which has shared objects between the client and implementation)
an implementation module
a war module
I agree that putting that implementation and war module in the same actual module would be ok, but the (arguably) benefit of this is that is very clear division between the classes that solve the problem and how the application communicates with the external world.
In previous projects that involved just a web application, I've tried to put everything in the same module, as it made testing easier, given the modules I was using.
Multi modules can help you with re-use your code.
It's one of the best benefits you'll feel in work.
Imagine if you have 3 web projects with a security layer, You'll have to copy paste your code 3 times and trying connect it with each project.
But what if you create a security module a project with a specific job.
It'll be easy to use it by injecting it to your app and then boom it works.
Also as mentioned in #ben75's answer the one maven build command and the correct order of building all your used jars. You'll think no more about which depends on another.
I find maven modules extremely useful for the following reasons:
Architecture layering and boundaries
For example, I make a maven module application-contract which contains the interfaces my presentation layer sees. So I have UI->Presenter-> application-contract <-application-impl <- infrastructure -> domain. This way, I know that my presentation/UI layer will not have access to classes from my Domain/application layers. If domain classes are not in classpath when I code in UI, I cant use them. And I like it this way (utilizing the class path restrictions). Perhaps Java 9 modules can solve this problem too, but (unfortunately) I have work with Java 8.
Running tests in one module each time
When I change code to a layer which is a module (as mentioned previously) I can run its tests only, without re-runing tests from code I did not change. This gives me speed. My presentation layer tests need ~3 seconds (for 300 tests). Every time I change code to a Presenter or whatever below application layer, I don't want my database H2 integration tests to run. Or My Image processing tests to run. Because these do IO and they are slow.
Building
Pretty much the same thing. When I change code to UI, i have only to build and deploy UI stuff (my UI is in Java).

Resources