What is SCA

Spring Cloud Alibaba致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用服务的必需组件,方便开发者通过Spring Cloud编程模型轻松使用这些组件来开发分布式应用服务。

依托Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以将Spring Cloud应用接入阿里分布式应用解决方案,通过阿里中间件来迅速搭建分布式应用系统。

虽然Spring Cloud提供了非常强大的功能,但是它并不提供所有的实现,而是通过Spring Cloud Common子项目,定义了统一的抽象API。如下图所示:
在这里插入图片描述
而后,不同厂商结合其自身的中间件,提供自己的Spring Cloud 套件,例如说:
1.Netflix结合自己的EurekaRibbonHystrix等开源中间件,实现了spring-cloud-Netflix
2.Kubernetes结合自己的apiserverconfigmap等功能,实现了spring-cloud-kubernetes
3.Alibaba结合自己的NacosDubboSentinel等开源中间件,实现了spring-cloud-Alibaba

当然,Spring Cloud官方还是提供了一些功能的具体实现的,例如说:
1.API网关:Spring Cloud Gateway
2.配置中心:Spring Cloud Config
3.链路追踪:Spring Cloud Sleuth
4.消息驱动:Spring Cloud Stream
5.事件总线:Spring Cloud Bus
6.服务调用:Spring Cloud OpenFeign
7.负载均衡:Spring Cloud Load Balancer

Spring Cloud官方、NetflixAlibaba三者整理成如下表格:

Spring Cloud官方 Spring Cloud Netflix Spring Cloud Alibaba
配置中心 Spring Cloud Config
Spring Cloud Vault
Archaius Nacos
注册中心 - Eureka Nacos
服务调用 Spring Cloud OpenFeign
RestTemplate
- Dubbo
负载均衡 Spring Cloud Load Balancer Ribbon Dubbo
服务容错 - Hystrix Sentinel
API网关 Spring Cloud Gateway Zuul -
消息驱动 Spring Cloud Stream RabbitMQ
Spring Cloud Stream Kafka
- Spring Cloud Stream RocketMQ
事件总线 Spring Cloud Bus RabbitMQ
Spring Cloud Bus Kafka
- Spring Cloud Bus RocketMQ
链路追踪 Spring Cloud Sleuth - -
分布式事务 - - Seata
分布式调度 - - SchedulerX

二、Spring Cloud Alibaba 套件

Spring Cloud Alibaba套件,阿里开源组件、阿里云商业组件整合进Spring Cloud体系当中,同时对Spring Cloud GatewayOpenFeignRibbon等等进行集成。整体如下图所示:
在这里插入图片描述

Spring Cloud Alibaba套件中的阿里开源组件和阿里云商业组件整理如下对照表:

开源组件 阿里云商业组件
配置中心 Nacos ACM
注册中心 Nacos ANS
服务调用 Dubbo
负载均衡 Dubbo
服务容错 Sentinel AHAS
API 网关 - -
消息驱动 RocketMQ ONS
事件总线 RocketMQ ONS
分布式事务 Seata -
分布式调度 - SchedulerX
链路追踪 - OSS
短信服务 - SMS

目前Spring Cloud Alibaba提供了如下功能:
【1】服务限流降级: 支持WebServletWebFluxOpenFeignRestTemplateDubbo 限流降级功能的接入,可以在运行时通过控制台实时修改限流降级规则,还支持查看限流降级Metrics监控。
【2】服务注册与发现: 适配Spring Cloud服务注册与发现标准,默认集成了Ribbon的支持。
【3】分布式配置管理: 支持分布式系统中的外部化配置,配置更改时自动刷新。
【4】Rpc服务: 扩展Spring Cloud客户端RestTemplateOpenFeign,支持调用Dubbo RPC服务
【5】消息驱动能力: 基于Spring Cloud Stream为微服务应用构建消息驱动能力。
【6】分布式事务: 使用@GlobalTransactional注解, 高效并且对业务零侵入地解决分布式事务问题。
【7】阿里云对象存储: 阿里云提供的海量、安全、低成本、高可靠的云存储服务。支持在任何应用、任何时间、任何地点存储和访问任意类型的数据。
【8】分布式任务调度: 提供秒级、精准、高可靠、高可用的定时(基于Cron表达式)任务调度服务。同时提供分布式的任务执行模型,如网格任务。网格任务支持海量子任务均匀分配到所有Worker(schedulerx-client)上执行。
【9】阿里云短信服务: 覆盖全球的短信服务,友好、高效、智能的互联化通讯能力,帮助企业迅速搭建客户触达通道。

三、怎么用

引入Spring Cloud Alibaba依赖管理 ,Spring Cloud Alibaba BOM包含了它所使用的所有依赖的版本。

<dependencyManagement>
  <dependencies>
  	<dependency>
  	<groupId>com.alibaba.cloud</groupId>
  	<artifactId>spring-cloud-alibaba-dependencies</artifactId>
  	<version> 2.2.5.RELEASE </version>
  	<type>pom</type>
  	<scope>import</scope>
  	</dependency>
  </dependencies>
</dependencyManagement> 

然后在dependencies中添加自己所需使用的依赖即可使用。

版本说明:

Spring Cloud Alibaba Version Nacos Version Sentinel Version Rocket MQ Version Dubbo Version Seata Version
2021.1 or 2.2.5 RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE 1.8.0 1.4.1 4.4.0 2.7.8 1.3.0
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE 1.7.1 1.2.1 4.4.0 2.7.8 1.3.0
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE 1.7.1 1.1.4 4.4.0 2.7.6 1.2.0
2.2.0.RELEASE 1.7.1 1.1.4 4.4.0 2.7.4.1 1.0.0
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE 1.7.0 1.1.4 4.4.0 2.7.3 0.9.0
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE 1.6.3 1.1.1 4.4.0 2.7.3 0.7.1

毕业版本依赖关系(推荐使用)

Spring Cloud Version Spring Cloud Alibaba Version Spring Boot Version
Spring Cloud 2020.0.0 2021.1 2.4.2
Spring Cloud Hoxton.SR8 2.2.5.RELEASE 2.3.2.RELEASE
Spring Cloud Greenwich.SR6 2.1.4.RELEASE 2.1.13.RELEASE
Spring Cloud Hoxton.SR3 2.2.1.RELEASE 2.2.5.RELEASE
Spring Cloud Hoxton.RELEASE 2.2.0.RELEASE 2.2.X.RELEASE
Spring Cloud Greenwich 2.1.2.RELEASE 2.1.X.RELEASE
Spring Cloud Finchley 2.0.4.RELEASE(停止维护) 2.0.X.RELEASE
Spring Cloud Edgware 1.5.1.RELEASE(停止维护) 1.5.X.RELEASE

Spring Cloud Alibaba Dubbo比较重要的特性,使用Spring Cloud定义的应用级别的注册模型,将Dubbo服务注册到Spring Cloud编程模型的注册中心。如此,Spring Cloud Alibaba Dubbo又将FeignRestTemplate进一步增强,实现对Spring Cloud Alibaba Dubbo服务的调用。最终如下图所示:
在这里插入图片描述

四、Spring Cloud Alibaba 整合 Dubbo

项目结构
在这里插入图片描述
执行ProviderApplication来启动服务提供者。可以在Nacos可以看到demo-provider服务,如下图所示:
在这里插入图片描述

执行ConsumerApplication来启动服务消费者。可以在Nacos可以看到demo-consumer服务,如下图所示:
在这里插入图片描述
请求<http://127.0.0.1:8080/user/get?id=1>接口,返回结果如下:
在这里插入图片描述

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐