Você está na página 1de 36

#decoderweek

Arquitetura completa do Food4You


Arquitetura de Microservices do Food4You

A A A
Auth User P
Order P
Production
P
I Service I Service I Service

Logs
DB DB DB
API
Gateway
client

IDs
A
P
Payment A
Delivery
P
I Service I Service

DB DB
Service Config
Registry Server

youtube.com/michellibrito @brito_michelli
#decoderweek

Microservices Patterns são um conjunto


de ferramentas para não reinventar a
roda em soluções já conhecidas.

youtube.com/michellibrito @brito_michelli
#decoderweek

Boas práticas e padrões em Microservices

Broker Pattern e Mediator Pattern API Composition Pattern

Event Notification Pattern Registry Discovery Pattern

Event-Carried State Transfer Pattern Distributed Tracing Pattern

Saga Pattern com Coreografia Log Aggregation Pattern

Saga Pattern com Orquestração Ports and Adapters Pattern

youtube.com/michellibrito @brito_michelli
#decoderweek

Broker Pattern

Message via command

message via message via


Order command command Production
Service Service

DB DB
broker

Utilizado nas
comunicações
assíncronas via
mensageria.
youtube.com/michellibrito @brito_michelli
#decoderweek

Broker Pattern

Message via events


Production
Service

message
DB
Order via event
Service

Vantagens Payment
DB Service
- Menor acoplamento broker
- Maior disponibilidade
DB

Pontos de Atenção

Cuidado com os
desdobramentos de eventos

youtube.com/michellibrito @brito_michelli
#decoderweek

Mediator Pattern

Production
Service

DB

Order message message Delivery


Service Mediator
Service

DB DB
broker

Utilizado principalmente para: message Payment


- Regras de negócio de forma dinâmica Service
e avançadas;
- Enriquecimento de dados;
- Validações; broker DB
- Controle de estado dos recursos...

youtube.com/michellibrito @brito_michelli
#decoderweek

Event Driven – Event Notification Pattern


GET user
Payment
Service

POST event DB makePayment()


new user A
Auth User notification:
P
I Service saved user

saveUser()
Vantagens DB broker
Delivery
Menor acoplamento de Service
responsabilidades

DB Notificação através de um
Pontos de Atenção evento, onde os serviços que
Pode gerar uma sobrecarga de precisam processar esse
evento devem recorrer ao
sub-consultas na arquitetura serviço primário para obter o
estado atual do recurso.
youtube.com/michellibrito @brito_michelli
#decoderweek

Event Driven – Event Carried State Transfer Pattern


New User
GET user
Payment savePartialUser()
Service makePayment()

POST User Table


event state DB
new user A - userId
Auth User transfer: - name
P
I Service new user - cpf
- cardNumber
saveUser() User Table
Vantagens DB - userId broker
- name Delivery
- Maior disponibilidade
- cpf Service
- Menor acoplamento - email savePartialUser()
- birthDate
- Melhor performance - address User Document
DB - userId
- phoneNumber
Pontos de Atenção - name Transferência de
- cardNumber
- status - address estado entre
- Replicação dos dados ... - phoneNumber serviços através de
- status eventos para
- Consistência eventual
replicação de
- Desdobramento de eventos em cascata dados.
#decoderweek

Event Driven – Event Carried State Transfer Pattern


Update User
Payment updatePartialUser()
Service

UPDATE event state User Table


DB
user cardNumber A - userId
Auth User transfer: - name
P
I Service new cardNumber - cpf
- cardNumber
updateUser() User Table
DB - userId broker
- name Delivery
- cpf Service
- email
- birthDate
- address User Document
DB - userId
- phoneNumber
- cardNumber - name
- status - address
... - phoneNumber
- status

youtube.com/michellibrito @brito_michelli
#decoderweek

Saga Pattern

Sequencia de transações distribuídas

Service Service Service


A B C

transação DB DB transação DB transação


local local local

Sequencia de transações
locais que envolvem
múltiplos Microservices
quando temos dados
distribuídos na arquitetura.
youtube.com/michellibrito @brito_michelli
#decoderweek

Saga Pattern

Sequencia de transações distribuídas com compensação de dados

Service Service Service


A B C

transação DB DB transação DB transação


local local local

Compensação de
dados do estado
atual dos recursos.
youtube.com/michellibrito @brito_michelli
#decoderweek

Saga Pattern com Coreografia


Coreografia

Production
order
Service
event
channel
POST createTicket()
DB
new order A
Order confirmTicket()
P
I Service production completeTicket()
event
channel

DB createOrder()
Payment
confirmOrder() Service
payment
event
completeOrder() channel
Temos uma DB makePayment()
sequencia de broker
eventos em Saga
com Coreografia.

youtube.com/michellibrito @brito_michelli
#decoderweek

Saga Pattern com Coreografia


Coreografia com compensação de dados

Production
order
Service
event
channel
POST createTicket()
DB
new order A
Order cancelTicket()
P
I Service production
Vantagens event
channel

- Menor acoplamento DB createOrder()


Payment
cancelOrder() Service
payment
Pontos de Atenção event
channel
- Maior dificuldade no entendimento da arquitetura DB makePayment()
broker
como um todo (dependências cíclicas)
- Cuidado com os desdobramentos dos eventos

youtube.com/michellibrito @brito_michelli
#decoderweek

Saga Pattern com Orquestração


Orquestração

Production
production
Service
command
channel
POST createTicket()
DB
new order A
Order confirmTicket()
P
I Service order completeTicket()
command
channel

Payment
orquestrador payment
Service
command
channel
Temos uma DB makePayment()
createOrder() broker
sequencia de DB
comandos em Saga confirmOrder()
com Orquestração. completeOrder()
youtube.com/michellibrito @brito_michelli
#decoderweek

Saga Pattern com Orquestração


Orquestração com compensação de dados

Production
production
Service
command
channel
POST createTicket()
DB
new order A
Order cancelTicket()
P
Vantagens I Service order
command
channel
- Melhor divisão responsabilidades
- Melhor entendimento da arquitetura Payment
payment
Service
Pontos de Atenção command
channel

- Maior acoplamento DB makePayment()


DB createOrder() broker
(principalmente em torno
cancelOrder()
do orquestrador)

youtube.com/michellibrito @brito_michelli
#decoderweek

API Composition Pattern


Arquitetura do Food4You com API Composer

A A A
Auth User P
Order P
Production
P
I Service I Service I Service

request
/reports DB DB DB
A
API P
API
Gateway I Composer
client response

A
P
Payment A
Delivery
P
I Service I Service

DB DB
Service Config
Registry Server
API Composition pode ser
implementado no client, gateway
ou em um Composer Microservice.
youtube.com/michellibrito @brito_michelli
#decoderweek

Registry Discovery Pattern


Arquitetura do Food4You com Service Registry / Discovery

A A A
Auth User P
Order P
Production
P
I Service I Service I Service

Logs
DB DB DB
API
Gateway
client

IDs
A
P
Payment A
Delivery
P
I Service I Service

DB DB
Service Config
Registry Server

youtube.com/michellibrito @brito_michelli
#decoderweek

Registry Discovery Pattern

Registry Passo 1

A Payment A Auth User


P Client P Client
I Service I Service

DB DB

Service -> Port


Registry -> IP
-> Domain
-> Instance...

youtube.com/michellibrito @brito_michelli
#decoderweek

Registry Discovery Pattern

Registry Passo 1

A Auth User
A Payment A PAuth User
P P I Service
I Service I Service

DB DB

Service De tempo em
Registry tempo os clients
enviam as suas
atualizações para
o Service Registry.

youtube.com/michellibrito @brito_michelli
#decoderweek

Registry Discovery Pattern

Discovery Passo 2

request A Auth User


A Payment A P Service
P P I Service
I Service I B
response

DB DB

Service Service Registry


Registry envolvem as
preocupações
transversais

youtube.com/michellibrito @brito_michelli
#decoderweek

Distributed Tracing Pattern


Arquitetura do Food4You com Distributed Tracing Pattern

A A A
Auth User P
Order P
Production
P
I Service I Service I Service

Logs
DB DB DB
API
Gateway
client

IDs
A
P
Payment A
Delivery
P
I Service I Service

DB DB
Service Config
Registry Server

youtube.com/michellibrito @brito_michelli
#decoderweek

Distributed Tracing Pattern

Fluxo completo da requisição

request request
A Payment A Auth User
P P
I Service I Service
client response response

DB DB

Tempo total da request = Payment Service + Auth User Service

youtube.com/michellibrito @brito_michelli
#decoderweek

Distributed Tracing Pattern

Identificadores Trace IDs

request request
A Payment A Auth User
P P
I Service I Service
client response response

DB DB

Tempo total da request = Payment Service + Auth User Service

youtube.com/michellibrito @brito_michelli
#decoderweek

Distributed Tracing Pattern

Identificadores Span IDs

request request
A Payment A Auth User
P P
I Service I Service
client response response

DB DB

Tempo total da request = Payment Service + Auth User Service

youtube.com/michellibrito @brito_michelli
#decoderweek

Distributed Tracing Pattern

A Payment A Auth User


P P
I Service I Service

DB DB

IDs

youtube.com/michellibrito @brito_michelli
#decoderweek

Log Aggregation Pattern


Arquitetura do Food4You com Log Aggregation

A A A
Auth User P
Order P
Production
P
I Service I Service I Service

Logs
DB DB DB
API
Gateway
client

IDs
A
P
Payment A
Delivery
P
I Service I Service

DB DB
Service Config
Registry Server

youtube.com/michellibrito @brito_michelli
#decoderweek

Log Aggregation Pattern

Log INFO Log INFO


Log INFO A Payment A Auth User Log INFO
P P
Log INFO Service Service Log INFO
I I
Log WARN Log WARN
Log INFO Log INFO
Log INFO Log INFO
DB DB Log INFO
Log INFO
Log INFO
Log INFO
Log INFO
Log WARN
Log ERROR

Logs

youtube.com/michellibrito @brito_michelli
#decoderweek

Log Aggregation Pattern

A Payment A Auth User


P P
I Service I Service

DB DB

Logs

youtube.com/michellibrito @brito_michelli
#decoderweek

Ports and Adapters Pattern

Port

Adapter Adapter
Port

Application Core

Port

Adapter Adapter Design de Negócio


Port Arquitetura
Hexagonal

youtube.com/michellibrito @brito_michelli
#decoderweek

Ports and Adapters Pattern


Browser
Inbound adapters
Controller

Interface
Service
Queue Consumer

Services

Domain
Interface
Service

Queue Producer Configuration


Interface
Repository

Database
Outbound adapters Persistence

youtube.com/michellibrito @brito_michelli
#decoderweek

Arquitetura Hexagonal
trata-se da dissociação da lógica de negócio
das tecnologias e frameworks, preocupando-se
principalmente com a SUSTENTABILIDADE.

Ou seja, a capacidade e a facilidade de modificar, testar e modernizar softwares ao longo do


tempo.

youtube.com/michellibrito @brito_michelli
#decoderweek

Ports and Adapters Pattern


Browser
Inbound adapters
Controller

Interface
Service
Queue Consumer Elastic Email Elastic
Interface
Smtp
Services

Domain
Interface
Service

Queue Producer Configuration


Interface
Repository

Database
Outbound adapters Persistence

youtube.com/michellibrito @brito_michelli
#decoderweek

Ports and Adapters Pattern


Browser
Inbound adapters
Controller

Interface
Service
Queue Consumer Elastic Email Elastic
Interface
Smtp
Services SES Amazon AWS

Domain
Interface
Service

Queue Producer Configuration


Interface
Repository

Database
Outbound adapters Persistence

youtube.com/michellibrito @brito_michelli
#decoderweek

Boas práticas e padrões em Microservices

Broker Pattern e Mediator Pattern API Composition Pattern

Event Notification Pattern Registry Discovery Pattern

Event-Carried State Transfer Pattern Distributed Tracing Pattern

Saga Pattern com Coreografia Log Aggregation Pattern

Saga Pattern com Orquestração Ports and Adapters Pattern

youtube.com/michellibrito @brito_michelli

Você também pode gostar