1. 环境准备
- JDK 17+:Spring Boot 3.x需要JDK 17及以上。
- Maven 3.6+或Gradle 7.x:构建工具。
- IDE:推荐IntelliJ IDEA或VS Code。
2. 创建父项目(管理依赖)
使用Spring Initializr(start.spring.io)生成父项目:
- Project: Maven
- Language: Java
- Spring Boot: 3.2.x
- Packaging: Jar
- Java Version: 17
在Advanced Options中添加依赖:
- Spring Cloud Config(配置中心)
- Spring Cloud Discovery(服务发现)
生成项目后,修改pom.xml
,添加dependencyManagement
管理Spring Cloud版本:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3. 创建Eureka服务注册中心
- 新建子模块:在父项目下创建
eureka-server
模块。 - 添加依赖(
pom.xml
):<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
- 配置文件(
application.yml
):server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false
- 启动类:添加
@EnableEurekaServer
注解:@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
4. 创建配置中心(Config Server)
- 新建子模块:创建
config-server
模块。 - 添加依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
- 配置文件:
server: port: 8888 spring: cloud: config: server: git: uri: https://github.com/your-repo/config-repo clone-on-start: true
- 启动类:添加
@EnableConfigServer
注解。
5. 创建微服务(Eureka Client)
- 新建子模块:如
user-service
。 - 添加依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
- 配置文件(
bootstrap.yml
):spring: application: name: user-service cloud: config: uri: http://localhost:8888 eureka: client: service-url: defaultZone: http://localhost:8761/eureka
6. 创建API网关(Spring Cloud Gateway)
- 新建子模块:创建
api-gateway
。 - 添加依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
- 配置文件:
server: port: 8080 spring: cloud: gateway: routes: - id: user-service uri: lb://user-service predicates: - Path=/api/users/** eureka: client: service-url: defaultZone: http://localhost:8761/eureka
7. 服务间通信与熔断
- 使用OpenFeign:
添加依赖:
启动类添加<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
@EnableFeignClients
。 - 熔断器(Resilience4j):
添加依赖:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId> </dependency>
8. 测试验证
- 启动顺序:Config Server → Eureka Server → 微服务 → Gateway。
- 访问Eureka Dashboard:
http://localhost:8761
,确认服务注册。 - 通过网关访问服务:
http://localhost:8080/api/users/1
。 - 检查配置中心:确保微服务从Git仓库获取配置。
常见问题解决
- 服务未注册:检查
defaultZone
配置和Eureka Server状态。 - 配置加载失败:确认
bootstrap.yml
正确,Config Server可达。 - 版本冲突:统一父pom中的Spring Cloud和Boot版本。