第1章 SpringCloud基础框架
Spring Cloud是Spring技术栈生态很重要的一部分,面向大型网站服务端的开发和架构设计,它以Spring/SpringBoot为基础,提供的一系列组件规范和具体实现。
提示
博客基于Spring Cloud 2021.0.x
版本编写,要求Spring Boot版本为2.6.x
和2.7.x
。
一、概述
Spring Cloud提供了用于更快速构建分布式系统的基础规范(例如配置中心、服务发现、熔断器等)。基于Spring Cloud规范可以快速实现这些组件和功能,我们常用的分布式系统框架实现方案有以下三种:
- Spring Cloud Netflix(停止维护)
- Spring Cloud Alibaba
- Spring Cloud Official(官方组件,建议使用)
二、简述分布式系统
在后端服务中,随着业务量的增长,简单的单体服务已经很难满足需求,大型网站往往会演进到微服务分布式架构模式。
从单体服务系统到分布式系统,是服务实例数从1到∞的一次发展,这种量级的扩展,给开发和运维带来了很多问题。
为了解决升级分布式系统带来的问题,于是有了一系列分布式系统的规范和组件,Spring Cloud 既提供了解决方案规范,也提供了开箱即用的落地实现。
1. 分布式系统常用组件
组件 | 功能 |
---|---|
配置中心 | 微服务的配置管理 |
注册中心 | 微服务的服务注册与服务发现 |
微服务网关 | 微服务的路由管理 |
服务调用 | 微服务之间的远程调用 |
负载均衡 | 微服务多个实例的负载均衡 |
熔断限流 | 微服务之间调用的熔断机制和流量限制 |
事件驱动 | 微服务之间的异步方案和事件驱动方案 |
任务调度 | 微服务的任务调度管理 |
指标监控 | 微服务的各项指标监控 |
链路追踪 | 微服务的调用链路监控 |
日志采集 | 微服务的日志采集和监控 |
分布式事务 | 微服务之间的事务处理 |
2. 分布式系统架构图
三、常用组件实现方案
Spring Cloud组件常用的三种实现方案如下,可以按具体业务需求选择解决方案。
组件 | Spring Cloud Netflix | Spring Cloud Alibaba | Spring Cloud Official |
---|---|---|---|
配置中心 | - | Nacos | Spring Cloud Config Spring Cloud Consul Spring Cloud Zookeeper |
注册中心 | Eureka | Nacos | Spring Cloud Consul Spring Cloud Zookeeper |
微服务网关 | Zuul | - | Spring Cloud Gateway |
服务调用 | Feign | Dubbo | Spring Cloud OpenFeign |
负载均衡 | Ribbon | Dubbo LB | Spring Cloud LoadBalancer |
熔断限流 | Hystrix | Sentinel | Spring Cloud Circuit Breaker |
事件驱动 | - | - | Spring Cloud Stream |
链路追踪 | - | - | Spring Cloud Sleuth |
分布式事务 | - | Seata | - |
常用其他组件:
- 事件驱动:Kafka、RocketMQ、Pulsar
- 指标监控:Micrometer、Prometheus
- 链路追踪:Zipkin
- 日志采集:ELK 技术栈、FileBeat
- 密钥管理:Vault