Skip to content
清晨的一缕阳光
返回

Spring Cloud Alibaba 技术栈

Spring Cloud Alibaba 技术栈

什么是 Spring Cloud Alibaba

Spring Cloud Alibaba 是阿里巴巴联合 Spring 社区推出的一套微服务解决方案,它将阿里巴巴内部经过大规模实践验证的微服务组件与 Spring Cloud 标准无缝集成。

核心优势

核心组件

1. Nacos - 服务注册与配置中心

Nacos(Naming and Configuration Service)是阿里巴巴开源的动态服务发现、配置管理和服务管理平台。

核心功能

特点

2. Sentinel - 流量控制与熔断降级

Sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从流量路由、流量控制、熔断降级、系统负载保护等维度来维护系统的稳定性。

核心功能

特点

3. Seata - 分布式事务解决方案

Seata(Simple Extensible Autonomous Transaction Architecture)是阿里巴巴开源的一站式分布式事务解决方案。

支持的事务模式

特点

4. RocketMQ - 消息中间件

RocketMQ 是阿里巴巴开源的分布式消息中间件,支持事务消息、顺序消息、延迟消息等特性。

核心功能

5. Dubbo - 高性能 RPC 框架

Dubbo 是阿里巴巴开源的高性能 Java RPC 框架,提供基于接口方法的透明远程过程调用。

核心特性

Spring Cloud Alibaba 技术栈全景

┌─────────────────────────────────────────────────────┐
│                   微服务网关                          │
│            Spring Cloud Gateway                     │
└─────────────────────────────────────────────────────┘

        ┌────────────────┼────────────────┐
        │                │                │
┌───────▼───────┐ ┌──────▼──────┐ ┌──────▼──────┐
│  服务注册发现  │ │  配置管理    │ │  服务调用    │
│    Nacos      │ │   Nacos     │ │  OpenFeign  │
└───────────────┘ └─────────────┘ └─────────────┘
        │                │                │
        │         ┌──────▼──────┐        │
        │         │  熔断限流    │        │
        │         │  Sentinel   │        │
        │         └─────────────┘        │
        │                                │
        └────────────────┬───────────────┘

              ┌──────────▼──────────┐
              │   分布式事务         │
              │      Seata          │
              └─────────────────────┘

快速开始

1. 项目依赖

<dependencies>
    <!-- Spring Cloud Alibaba 依赖管理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2022.0.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- Nacos 服务注册发现 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

    <!-- Nacos 配置中心 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>

    <!-- Sentinel 流量控制 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>

    <!-- Seata 分布式事务 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    </dependency>
</dependencies>

2. 基础配置

spring:
  application:
    name: user-service
  cloud:
    nacos:
      # 服务注册
      discovery:
        server-addr: localhost:8848
        namespace: public
        group: DEFAULT_GROUP
      # 配置中心
      config:
        server-addr: localhost:8848
        namespace: public
        group: DEFAULT_GROUP
        file-extension: yaml
        refresh-enabled: true
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8719

3. 启用服务发现

@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }
}

4. 声明式服务调用

@FeignClient(name = "order-service")
public interface OrderClient {
    @GetMapping("/orders/{id}")
    Result<Order> getOrder(@PathVariable("id") Long id);
}

版本对应关系

Spring Cloud AlibabaSpring CloudSpring Boot
2022.0.0.02022.0.x3.0.x
2021.0.4.02021.0.x2.6.x
2021.0.1.02021.0.x2.4.x
2.2.9.0Hoxton2.2.x

最佳实践

1. 服务拆分

2. 配置管理

3. 流量控制

4. 事务处理

总结

Spring Cloud Alibaba 提供了一套完整的微服务解决方案,涵盖了服务注册发现、配置管理、流量控制、分布式事务等核心场景。

相比原生 Spring Cloud,Spring Cloud Alibaba 具有更好的性能、更丰富的功能和更完善的中文支持。

在后续文章中,我们将深入探讨各个组件的使用细节和最佳实践。

参考资源


分享这篇文章到:

上一篇文章
思维链(CoT)工程实践
下一篇文章
MySQL 架构设计与存储引擎