Microservices jako způsob, jak stavět software, mají oproti monolitické architektuře spoustu výhod:
Jak už to ale bývá, mají také hromadu nevýhod (takže ve výsledku na tom z hlediska námahy budete v podstatě stejně, ale to je zase jiná pohádka)
Přechod na microservices je zkrátka obtížné téma a vyžaduje hodně změn. Architekturou aplikace počínaje, ta je vlastně úplně jiná. A právě způsobem, jak ji navrhnout a jaké vzory čili patterns použít, se kniha zabývá. Ve třinácti kapitolách jde od začátku, tedy proč a jak to dělat, až po samotné implementační detaily a rady pro postupné rozplétání kódu. Obsah:
Zajímavost – každá service může mít vlastní verzi třídy, vlastní doménový model:
A much better approach is to apply DDD and treat each service as a separate subdomain with its own domain model. This means that each of the services in the FTGO application that has anything to do with orders has its own domain model with its version of the Order class. A great example of the benefit of multiple domain models is the Delivery Service. Its view of an Order, shown in figure 2.11, is extremely simple: pickup address, pickup time, delivery address, and delivery time. Moreover, rather than call it an Order, the Delivery Service uses the more appropriate name of Delivery. (Domain Driven Design Str. 59)
Zajímavost – modelový objekt by neměl publikovat události přímo, lepší je nechat ho předávat je publikační službě, nezatěžuje se tak jeho kód infrastrukturním voláním:
Conceptually, domain events are published by aggregates. An aggregate knows when its state changes and hence what event to publish. An aggregate could invoke a messaging API directly. The drawback of this approach is that because aggregates can’t use dependency injection, the messaging API would need to be passed around as a method argument. That would intertwine infrastructure concerns and business logic, which is extremely undesirable.
A better approach is to split responsibility between the aggregate and the service (or equivalent class) that invokes it. Services can use dependency injection to obtain a reference to the messaging API, easily publishing events. The aggregate generates the events whenever its state changes and returns them to the service. There are a couple of different ways an aggregate can return events back to the service. One option is for the return value of an aggregate method to include a list of events. (Str. 164)
Zajímavost – jak si poradit se změnami „schématu“, tedy jak migrovat již existující události:
An event sourcing application can use a similar approach to handle non-backward compatible changes. But instead of migrating events to the new schema version in situ, event sourcing frameworks transform events when they’re loaded from the event store. A component commonly called an upcaster updates individual events from an old version to a newer version. As a result, the application code only ever deals with the current event schema. (Str. 199)
For example, Delayed Delivery Service has a Delivery entity with narrowly focused responsibilities, whereas the FTGO monolith has an Order entity with an excessive number of responsibilities. Because the two domain models are different, you must implement what DDD calls an anti-corruption layer (ACL) in order for the service to communicate with the monolith. The goal of an ACL is to prevent a legacy monolith’s domain model from polluting a service’s domain model. It’s a layer of code that translates between the different domain models. (Str. 446)
Kniha je opravdu doširoka rozkročená a díky tomu poslouží stejně dobře aplikačním architektům, vývojářům i všem, kdo chtějí mikroslužbám porozumět.
Odkazy:
RICHARDSON, Chris. Microservices Patterns. Shelter Island, NY: Manning Publications Co., 2019. ISBN 978-1-61729-454-9
Hodnocení hvězdičkami používá jako prevenci
opakovaného kliknutí anonymní cookie.
Pokud s tím nesouhlasíte, neklikejte.
Další podrobnosti k cookies zde.