Microservice Arch 点点滴滴
Pattern: microservice architecture
“An architectural style that structures an application as a set of deployable/executable units, a.k.a. services”
- Highly maintainable and testable
- Minimal lead time (time from commit to deploy)
- Loosely coupled
- Independently deployable
- Implements a business capability
- Owned/developed/tested/deployed by a small team
行业架构最佳实践
Best practices framework for Oracle Cloud Infrastructure TODO
行业架构分享
不断更新行业的一些架构分享,进行分析总结。
Designing loosely coupled services[Slides]
by Chris Richardson, 介绍了几种类型 coupling 及其缺点和如何设计 loosely coupled 微服务.
Runtime coupling,订单服务需要等待客户服务返回时才给出响应,减少了可用性
Design time coupling,当客户服务变化时,订单服务也跟着变化。减少了开发的独立性
Minimizing design time coupling
- DRY
- Consume as little as possible
- Icebergs: expose as little as possible
- Using a database-per-service
Reducing runtime coupling
- Use resilience patterns for synchronous communication
- Self-contained service
- Improving availability: replace service with module
- Use asynchronous messaging
- Improving availability: sagas
- Improving availability: move responsibility + CQRS
Avoiding infrastructure coupling
- Use private infrastructure: minimizes resource contention and blast radius
- Use “Private” message brokers
- Fault isolated swim lanes
模式 PATTERNS
Data Management
Database per Microservice
CQRS
Event Sourcing
Materialized View Patterns
Generate prepopulated views over the data in one or more data stores when the data isn’t ideally formatted for required query operations. this can help support efficient querying and data extraction, and improve application performance.
Context and problem
选择存储数据的方式跟数据本身的格式、数据大小、数据完整性以及所使用的存储种类, 但是,这样带来查询的不好的影响。比如当查询数据的子集时,必须取出所有的相关的数据,比如查询一些客户的订单概览
Solution
为了支持高效率的查询,通用的解决办法是,提前生成数据视图(materializes the data in a format suited to the required results set.)
Messaging
Design and Implementation
BFF
API GateWay
Strangler
Consumer-Driven Contract Tracing
Externalized Configuration
Facilitators
Facilitators[5] are simple a new type that has access to the type you wished you had generic methods on. 比如,如果你是 ORM framework 的设计者,想提供一些查询表格的方法。你提供了一个中间类型(Querier),这个中间类型允许你写一些 generic querying functions。
Resilience Patterns - Sagas
Saga distributed transactions , a way to manage data consistency across microservices in distributed transaction scenarios。
- Choreography
- Orchestration.
Resilience Patterns - Circuit Breaker
" used to limit the amount of requests to a service based on configured thresholds – helping to prevent the service from being overloaded " – 断路器
同时,通过监控多少个请求失败了,来阻止其他的请求进入到服务里
CircuitBreaker 使用 sliding window 来存储和集合发生的请求。 可以选择 count-based 也可以选择 time-based。
Circuit Breakers in Go Golang语言的实现.
Resilience Patterns - Bulkhead
" Isolates services and consumers via partitions “, 舱壁模式, 在航运领域,舱壁是船的一部分,合上舱口后可以保护船的其他部分。
- SemophoreBulkhead, work well across a variety of threading and io models. it is based on a semaphore.
- ThreadPoolBulkhead, uses a bounded queue and a fixed thread pool.
防止级联失败发生. 但对应用来说该模式增加了负担.
什么时候使用:
- Isolate resources used to consume a set of backend services, especially if the application can provide some level of functionality even when one of the services is not responding.
- Isolate critical consumers from standard consumers
- Prodect the application from cascading failures
Request_id
Better Logging Approach For Microservices request_id在日志中打印,由请求方生成发起
从What is the X-REQUEST-ID http header?说明来看,建议是client生成x-request-id.
Resilience Patterns - RateLimiter
限流的基础算法
- 漏桶算法
- 漏桶算法的实现往往依赖于队列,请求到达如果队列未满则直接放入队列,然后有一个处理器按照固定频率从队列头取出请求进行处理。 如果请求量大,则会导致队列满,那么新来的请求就会被抛弃。
- 令牌桶算法
-
一个存放固定容量令牌的桶,按照固定速率往桶里添加令牌。桶中存放的令牌数有最大上限,超出之后 就会被丢弃或拒绝。当流量或者网络请求到达时,每个请求都要获取一个令牌,如果能获取到,则直接处理,并且令牌桶删除一个令牌。如果获取不到,则该请求就要被限流,要么直接丢弃,要不再缓冲区等待。
-
长期来看,所限制的请求速率的平均值等于 rate(每秒向桶添加令牌的速率r) 的值
-
实际请求达到的速率为 M, 达到的最大速率为 M = b + r (其中b 为令牌桶的最大值)
-
参考
What is the X-REQUEST-ID http header? https://stackoverflow.com/questions/25433258/what-is-the-x-request-id-http-header)
Resilience4j is a fault tolerance library designed for Java8 and functional programming https://github.com/resilience4j/resilience4j
Azure Cloud Design Patterns, used in the cloud for building reliable, scalable, secure applications https://docs.microsoft.com/en-us/azure/architecture/patterns/index-patterns
.NET Microservices: Architecture for Containerized .NET Applications (PDF 在Kami 上)https://docs.microsoft.com/en-us/dotnet/architecture/microservices/
[5] JBD: Generics facilitators in Go