What are microservices?

pattern

The beginnings of a pattern language for microservices architectures

.

点击这里,访问本系列文章的中文翻译

Click here for Chinese translation of patterns

Which architecture should you choose for

an application?

  • Monolithic
  • architecture: Design an application as a single deployable unit

  • Microservices architecture: Design an application as a collection of loosely coupled

services

How to decompose an application into services?

  • Decompose by business capability: define services corresponding to business capabilities

  • Decompose by subdomain: define services corresponding to DDD subdomains
  • Autonomous service: Design services to

  • handle synchronous requests without waiting for other services to respondnew Team Servicenew
  • Strangler app
  • Anti-corruption layer

How to maintain data consistency and implement queries?

  • Per-service database: Each service has its own
  • private database Shared database: Services share a

  • database
  • Saga: Use sagas, which are local transaction sequences, to maintain data consistency
  • across services. API composition: Implement queries by

  • invoking the services that own the data and performing an in-memory join
  • CQRS: Implement queries by maintaining one or more materialized views that can be efficiently queried
  • Domain event:

  • publishes an event each time
  • the data changes Event source

  • : persists aggregates

as a sequence of events Transactional messaging How

publish messages as part of a database transaction

?

  • Transactional outbox
  • Transaction log Poll

editor

How to facilitate testing?

  • Consumer-directed contract testing: a set of tests for a service written by the developers of another service that consumes it Consumer-side contract test: a set of tests for a service customer

  • (for example, another service) that verifies that it can communicate with the
  • service SEST Service Component – a set of tests

  • that tests a service in isolation using test doubles for any service it invokes

How to implement the services of an application?

Multiple service instances per host:

  • Deploy multiple service instances on a single host Service instance per host: Deploy each service instance on its own host Service instance per virtual machine: Deploy each service instance on your VM

  • Per-container service instance: Deploy each service instance in your container
  • Serverless deployment

  • :
  • Deploy
  • a service using the platform
  • Serverless deployment Service deployment platform: Deploy services using a highly automated deployment platform that provides a service abstraction

How to handle

cross-cutting concerns?

  • Microservices chassis: A framework that handles cross-cutting concerns and simplifies service development
  • Outsourced configuration: Outsource all configuration, such as database location and
  • credentials. Service Template: A template that implements standard cross-cutting issues and is intended to be copied by a developer to quickly start developing a new service

. Style

What communication mechanisms do services use to communicate with each other and with their external clients?

  • Remote procedure invocation: Use an RPI-based
  • protocol for communication between services

  • Messaging: Use asynchronous messaging for communication
  • between services Domain-specific protocol: Use a domain-specific protocol
  • Idempotent consumer: Ensure that message consumers can cope with being invoked multiple times with the same

external API

message

How do external clients communicate with

services? API Gateway: A service

  • that provides
  • each client with a unified interface to services

  • Back-end to front-end: A separate API gateway for each type of

client

Service discovery

How does the client of an RPI-based service discover the network location of a service instance?

Client-side discovery: The client queries a service log to discover the locations of the service instances Server-side discovery: The router queries a service log to discover the locations of the service instances Service logging: A database of service instance locations Automatic logging: The service instance is registered with the service log 3rd party log: a

  • third party registers a service instance with
  • the service log

Reliability

How to prevent a

network or service failure from cascading to other

services?

  • Circuit breaker – invoke a remote service through a proxy that fails immediately when the error rate of the remote call exceeds a threshold

How to communicate the identity of the applicant to the services that handle the request?

  • Access token: a token that securely stores information about the user that is exchanged between services

How to

understand the behavior of an application and troubleshoot problems?

  • Log aggregation: adding application logs
  • Application metrics: Instrumenting a service’s code to collect statistics about operations
  • Audit logging: Logging user activity in a database
  • Distributed tracing: Instrument services with code that assigns each external request a unique identifier that is passed between services. Record information (e.g., start time, end time) about work (e.g., service requests) performed when handling the external request in

  • a centralized service Exception tracking: Report all exceptions to a centralized exception tracking service that

  • aggregates and tracks exceptions and notifies developers.

  • Health Check API

  • : Service API (for example, HTTP endpoint) that returns the status of the service and is intended to receive ping, For example, by using a monitoring service

  • Log deployments and changes

How to implement a screen or UI page that displays data from multiple services?

Server-side page snippet composition: Create a web page on the server by composing HTML snippets

    generated by multiple enterprise-capacity/subdomain-specific web applications Client-side UI composition

  • : Create a client-side UI by composing UI snippets

  • represented by various capability-specific UI components Enterprise/Subdomain