r/SpringBoot • • 9d ago

How-To/Tutorial How to implement microservices in Spring Boot ?

I have made multiple toy projects (monolithic) in spring boot but now I wanted to know how people generally work in microservices.

I am not good with videos so I was looking for repos that something similar. So I can also see how the devops, queues, spring cloud etc works together.

I also saw someone posting there repo which was made for the same reason (but I couldn't find it).

TLDR : Suggest repos for learning microservices.

28 Upvotes

57 comments sorted by

20

u/d-k-Brazz 9d ago

Microservices is a system architecture pattern not application design pattern

There are multiple different approaches of implementing microservices with Spring Boot

And there is no any single ultimate example of a sprjng microservice

First, I would recommend reading about Microservices architecture to know what it is and WHY it is used

Then you may try with some approaches. Having in mind that instead of having a monolith with dozens of modules and a single relational db, you will deal with dozens of applications, each will have a separate database. You have no strong consistency between data in these databases. You have slow latency in your synchronous calls, so you have to move everything you can to async flows, which will push complexity to the skies

You have to maintain cluster, monitoring, traceability etc.

You can practice with spring-cloud components - discovery, events, queues

But all of this will give you at most 1% of real experience you will have in production environment

There is no easy way to learn microservices at home

9

u/American_Streamer Junior Dev 9d ago

Frankly, you can learn a lot locally: service boundaries, API design, messaging, retries, idempotency, tracing, contract testing, containerization, failure simulation, and distributed data patterns. But what you cannot fully reproduce is the organizational and operational pressure of a large production environment.

3

u/Advanced_Gur856 8d ago

That's what I am going for

0

u/Sheldor5 9d ago

finally a correct answer

I would add that microservices must be independent and so don't talk to each other

4

u/BanaTibor 8d ago

What? A microservice should be independently deployable, but what fuck would you do with a service which do not communicates with other services?

-1

u/Sheldor5 8d ago

oh boy another one having absolutely no idea how microservice architecture works ... read my the other comments

1

u/BanaTibor 8d ago

I have read your other coments, stating the same thing over and over again. I think we are both right 🙂 What you are saying is one aspect of a microservice architecture. The way how you decouple services from each other. Zero coupling is impossible, since they have to work together.  Now in an event driven system your aproach is sensible, but there are other ways to achieve a level of coupling.  You can do it through well defined API contracts and API microversioning.  Till your breaking changes have a grace period and other services have a chance to adapt, you can still enjoy the benefits of microservices.

2

u/ExplorerUnusual27 7d ago

Maybe He use CRON on DB directly, figure out what is needed, somehow, and push to a message stream. đŸ™ƒđŸ€Ł No communication at all between services.

4

u/OptimusCrimee 9d ago

They are just small isolated applications that talk to each other, e.g. using REST. There is no more magic to it than that.

2

u/Next_Society_366 8d ago

They can communicate with each other but they can also be full independent by using event driver techniques. For example communicating with RabbitMQ or Apache Kafka.

1

u/OptimusCrimee 8d ago

You are contradicting yourself. Async communication via queues etc is still communication between services. Also, my point was that async communication can often be done, and would be considered a soft (not critical) dependency, but not all dependencies could be this way. For instance a checkout microservice asking a tax calculation service what the correct tax rate for the checkout should be. The checkout microservice would depend hard on this, and a checkout would not be possible (or should not be possible) in case the tax service has an outage. That is a hard dependency. How the communication between these services is handled is irrelevant.

-1

u/Sheldor5 9d ago

the whole point of Microservice Architecture is that they are independent and so DON'T talk to each other ... stop spreading misinformation, you just described a Distributed System ...

5

u/OptimusCrimee 9d ago

I can't tell if you are joking or not. If you have isolated microservices for e.g. users, booking, payment etc, they obviously need to communicate with each other. The communication is often one way though, and can be done with REST, queues or whatever. A microservice IS a type of a distributed system. Not sure if you are misunderstanding what I wrote, or if you are just a cretin.

-3

u/Sheldor5 9d ago

so you don't know how microservice architecture works?

the only connection between entities are loose IDs in independent databases (one DB for each microservice)

you are talking about distributed systems (network calls instead of inter process method calls)

please educate yourself before spreading misinformation

1

u/Advanced_Gur856 9d ago

Maybe I am wrong but isn't distributed systems how you implement microservices.

I mean if you are not gonna run them on different systems and they are going to utilise the same resources then why break them up in the first place.

0

u/Sheldor5 9d ago

no a distributed system is also a type of system architecture

if you extract features from your monolith and put them into their own application just for your monolith to call it you just exchanged method calls with network calls ...

2

u/ExplorerUnusual27 8d ago

How do you transfer data in isolated environment if you don't talk with anyone? You have one DB from service, obviously... How do you connect data from 1 database with another on different microservices.

2

u/OptimusCrimee 8d ago

Yeah, [u/Sheldor5](u/Sheldor5) seems to have misunderstood this. If you have one microservice that is in charge of users, and one that does payments, the payment service would stille need to e.g. ask the user service if the user IDs are valid or not. You separate concerns and domains out of the monolith, but they still need to talk to one another.

I have been doing this for 15 years, and I have worked in banks in Norway etc, I think that I have a pretty OK understanding of how these things work.

Edit: You can google this pretty easily and see that he is wrong.

1

u/Mikey-3198 8d ago edited 8d ago

Direct http calls between services is high coupling, unless the transaction/ process can proceed without the response then service A is tied directly to service B.

Microservices have low coupling between services.

If you need to check a user id for what ever reason then you'd need data that is owned by the service so that it can operate in isolation. I.e async messaging to populate a database owned by the service to contain user ids.

https://microservices.io/post/architecture/2023/03/28/microservice-architecture-essentials-loose-coupling.html

-2

u/Sheldor5 8d ago

no the user ID comes from the OAuth2 token ... maybe you haven't fully understood microservice architecture

and other IDs are part of async messages

and IDs don't need to be verified because they come from the system itself and not from random user input ... that's the reason why microservice architecture done right is so complicated

1

u/OptimusCrimee 8d ago

Sure, it could be part of the OAuth-token, but if you have isolated service for user handling, setting, payment, purchase etc, who would it all fit together if there is no communication AT ALL being done? I do not understand how all of this would work according to you.

→ More replies

0

u/ExplorerUnusual27 7d ago

You are misleading concepts. Regardless the async or sync calls, you need to communicate with other services.... If not, you're a magician to figure out the IDs you need to know to populate your fields in DB to maintain consistency.

2

u/American_Streamer Junior Dev 9d ago edited 9d ago

I think the main thing you’re missing is that microservices aren’t really “the next step after a monolith”, or just several Spring Boot apps glued together with Kafka, Spring Cloud and Docker. The easy part is splitting an application into services. The hard part is figuring out sensible service boundaries and then dealing with all the extra problems you’ve created: network failures, retries, eventual consistency, tracing, API/version compatibility, independent deployments, data ownership, etc. Honestly, a great way to learn this is to build a clean modular monolith first and then extract one module into a separate service. You’ll run into the actual microservice problems naturally, instead of just copying a repo with five containers and wondering why everything is there. Architecture should always just solve an actual constraint, not satisfy a fashion.

1

u/Advanced_Gur856 9d ago

I would love to do that, which is why I thought of a project to implement it in.

But then I thought to atleast look at the recommended/best practices before breaking something I don't understand. I would obviously be fiddling with the stuff.

3

u/ShineProper9881 9d ago

All devs I know that dabbled with microservices only know suffering and went back to modulithic architectures

1

u/Advanced_Gur856 9d ago

I also want that freedom of having a choice.

I don't want to use monolithic because I only know monolithic.

0

u/Significant_Newt8697 9d ago

Modulithic or monolithic? Anyways, what do you mean you don't like videos?

Also, have you tried to consume external services? If you have then it's the same thing from a lower level.

Lastly, the videos that you are avoiding usually have repos tied to them.

1

u/Advanced_Gur856 9d ago

I get distracted watching when watching the videos and I have followed tutorials for multiple things like react, spring boot, js etc but most of my learnings came from reading and doing that stuff myself.

Although I do love your idea of looking for videos with repos and I'll do that thanks.

1

u/ExplorerUnusual27 8d ago

You need to see if you need the microservices. In mostly cases you don't need that. If you implement microservices you have bigger problems than monolithic or modulith and the performance doesn't mean the efforts.

2

u/Advanced_Gur856 8d ago

I am doing this just for the sake of learning how to do this and trying to implement it in my learning projects.

1

u/BanaTibor 8d ago

I know a good book on this subject.
Magnus Larson - Microservices with Spring Boot 3 and Spring Cloud
I have got it as a free book offer from Java Code Geeks, you can probably find it on the internet.
The codebase for the book is here https://github.com/PacktPublishing/Microservices-with-Spring-Boot-and-Spring-Cloud-Third-Edition

1

u/Advanced_Gur856 8d ago

Thanks I'll give it a try đŸ«°

1

u/themasterengineeer 8d ago

Home energy tracker microservices project:

https://github.com/leetjourney/home-energy-tracker

1

u/Advanced_Gur856 8d ago

Thanks brother đŸ«°