1. 项目概述与架构设计
在电商系统开发领域,微服务架构已成为主流解决方案。本次分享的电商系统基于Spring Cloud Finchley.SR2构建,包含用户管理、商品展示等核心模块。系统采用经典的三层架构设计:
- 前端展示层:使用Thymeleaf模板引擎渲染页面
- 业务逻辑层:通过Spring Cloud微服务组件实现服务治理
- 数据持久层:采用MyBatis框架操作MySQL数据库
技术选型方面,我们选择了以下核心组件:
- 服务注册中心:Eureka Server
- 服务通信:Feign + Ribbon
- 熔断机制:Hystrix
- 安全控制:Spring Security
提示:Spring Cloud版本与Spring Boot版本存在严格的对应关系,Finchley.SR2必须搭配Spring Boot 2.0.6.RELEASE使用,否则会出现兼容性问题。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 基础环境搭建
2.1 开发环境配置
项目开发需要准备以下环境:
- 操作系统:Windows 10/11
- JDK版本:1.8(强烈建议使用Oracle JDK)
- 构建工具:Maven 3.6.0+
- IDE:IntelliJ IDEA 2021+
- 数据库:MySQL 5.7+/8.0+
2.2 项目结构设计
采用Maven多模块架构,父工程为web-store,子模块包括:
code复制web-store
├── eureka-server # 服务注册中心
├── common # 公共模块
├── user-provider # 用户服务提供者
├── user-consumer # 用户服务消费者
└── goods-service # 商品服务(下篇实现)
3. Eureka Server实现
3.1 服务注册中心搭建
创建eureka-server模块的关键步骤:
- 添加依赖配置:
xml复制<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
- 配置application.yml:
yaml复制server:
port: 8001
spring:
security:
user:
name: admin
password: 123456
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@localhost:${server.port}/eureka/
- 启用Eureka Server功能:
java复制@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3.2 安全配置实践
为防止未授权访问,我们添加了基础安全控制:
java复制@Configuration
@EnableWebSecurity
public class WebSecurityConfig ext
