在当今互联网时代,微服务架构已经成为大型分布式系统的标准选择。相比传统的单体架构,微服务架构具有更好的可扩展性、可维护性和技术栈灵活性。本文将带你从零开始,使用Spring Cloud Alibaba生态系统搭建一个完整的微服务架构,涵盖服务注册发现、配置管理、熔断降级、分布式事务等核心功能。
为什么选择Spring Cloud Alibaba?
技术优势对比
Spring Cloud ***flix vs Spring Cloud Alibaba
Spring Cloud ***flix曾经是微服务领域的王者,但随着***flix公司逐步停止对开源组件的维护,Spring Cloud Alibaba应运而生,并迅速成为国内微服务架构的首选方案。
核心优势:
- 性能更优:Nacos作为注册中心,性能比Eureka提升30%以上
- 功能更全:集成了阿里巴巴多年来在分布式系统方面的最佳实践
- 生态完善:与Spring Boot深度集成,开箱即用
- 社区活跃:阿里巴巴官方维护,持续更新迭代
架构设计总览
整体架构图
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Gateway │ │ User Service │ │ Order Service │
│ (网关层) │ │ (用户服务) │ │ (订单服务) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
└───────────────────────┼───────────────────────┘
│
┌─────────────────────────────────┼─────────────────────────────────┐
│ │ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Nacos │ │ Seata │ │ Sentinel │ │
│ │ (注册中心) │ │ (分布式事务) │ │ (熔断降级) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │
└────────────────────── 基础设施层 ──────────────────────────────┘
技术栈选择
- 服务注册发现:Nacos Discovery
- 配置管理:Nacos Config
- 网关:Spring Cloud Gateway
- 熔断降级:Sentinel
- 分布式事务:Seata
- 负载均衡:Ribbon
- 服务调用:OpenFeign
- 数据库:MySQL + MyBatis Plus
- 缓存:Redis
- 消息队列:RocketMQ
环境准备与基础搭建
1. 开发环境要求
# Java版本
java version "1.8.0_281"
# Maven版本
Apache Maven 3.6.3
# Docker版本(用于启动中间件)
Docker version 20.10.7
2. 启动基础中间件
使用Docker ***pose一键启动所需的中间件:
version: '3.8'
services:
nacos:
image: nacos/nacos-server:v2.2.0
ports:
- "8848:8848"
- "9848:9848"
environment:
- MODE=standalone
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=mysql
- MYSQL_SERVICE_DB_NAME=nacos
- MYSQL_SERVICE_USER=nacos
- MYSQL_SERVICE_PASSWORD=nacos123
mysql:
image: mysql:8.0
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root123
- MYSQL_DATABASE=nacos
- MYSQL_USER=nacos
- MYSQL_PASSWORD=nacos123
redis:
image: redis:6.2
ports:
- "6379:6379"
***mand: redis-server --requirepass redis123
seata:
image: seataio/seata-server:1.6.1
ports:
- "8091:8091"
environment:
- SEATA_CONFIG_NAME=file:/root/seata-config/registry.conf
3. 父项目POM配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>***.example</groupId>
<artifactId>microservice-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<properties>
<maven.***piler.source>8</maven.***piler.source>
<maven.***piler.target>8</maven.***piler.target>
<spring.boot.version>2.7.8</spring.boot.version>
<spring.cloud.version>2021.0.5</spring.cloud.version>
<spring.cloud.alibaba.version>2021.0.4.0</spring.cloud.alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>***.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>