项目介绍
- 乐优商城是一个全品类的电商购物网站(B2C)。
- 用户可以在线购买商品、加入购物车、下单
- 可以评论已购买商品
- 管理员可以在后台管理商品的上下架、促销活动
- 管理员可以监控商品销售状况
- 客服可以在后台处理退款操作
- 希望未来3到5年可以支持千万用户的使用
系统架构
系统架构解读
整个乐优商城可以分为两部分:后台管理系统、前台门户系统。
-
后台管理:
- 后台系统主要包含以下功能:
- 商品管理,包括商品分类、品牌、商品规格等信息的管理
- 销售管理,包括订单统计、订单退款处理、促销活动生成等
- 用户管理,包括用户控制、冻结、解锁等
- 权限管理,整个网站的权限控制,采用JWT鉴权方案,对用户及API进行权限控制
- 统计,各种数据的统计分析展示
- 后台系统会采用前后端分离开发,而且整个后台管理系统会使用Vue.js框架搭建出单页应用(SPA)。
- 后台系统主要包含以下功能:
-
前台门户
- 前台门户面向的是客户,包含与客户交互的一切功能。例如:
- 搜索商品
- 加入购物车
- 下单
- 评价商品等等
- 前台系统我们会使用Thymeleaf模板引擎技术来完成页面开发。出于SEO优化的考虑,我们将不采用单页应用。
- 前台门户面向的是客户,包含与客户交互的一切功能。例如:
无论是前台还是后台系统,都共享相同的微服务集群,包括:
- 商品微服务:商品及商品分类、品牌、库存等的服务
- 搜索微服务:实现搜索功能
- 订单微服务:实现订单相关
- 购物车微服务:实现购物车相关功能
- 用户中心:用户的登录注册等功能
- Eureka注册中心
- Zuul网关服务
- …
项目搭建
技术选型
前端技术:
- 基础的HTML、CSS、JavaScript(基于ES6标准)
- JQuery
- Vue.js 2.0以及基于Vue的框架:Vuetify(UI框架)
- 前端构建工具:WebPack
- 前端安装包工具:NPM
- Vue脚手架:Vue-cli
- Vue路由:vue-router
- ajax框架:axios
- 基于Vue的富文本框架:quill-editor
后端技术:
- 基础的SpringMVC、Spring 5.x和MyBatis3
- Spring Boot 2.0.7版本
- Spring Cloud 版本
Finchley.SR2
- Redis-4.0
- RabbitMQ-3.4
- Elasticsearch-6.3
- nginx-1.14.2
- FastDFS - 5.0.8
- MyCat
- Thymeleaf
- mysql 5.6
开发环境
IDEA2020.1
+JDK1.8
+maven3.5.x
+git
域名
我们在开发的过程中,为了保证以后的生产、测试环境统一。尽量都采用域名来访问项目。
一级域名:www.leyou.com
,leyou.com
leyou.cn
二级域名:manage.leyou.com/item
, api.leyou.com
我们可以通过switchhost工具来修改自己的host对应的地址,只要把这些域名指向127.0.0.1,那么跟你用localhost的效果是完全一样的。
创建父工程
【New Project】>【Empty Project】>【Project Name:leyou
】>【finish】
编写pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
<mybatis.starter.version>1.3.2</mybatis.starter.version>
<mapper.starter.version>2.0.2</mapper.starter.version>
<druid.starter.version>1.1.9</druid.starter.version>
<mysql.version>5.1.32</mysql.version>
<pageHelper.starter.version>1.2.3</pageHelper.starter.version>
<leyou.latest.version>1.0.0-SNAPSHOT</leyou.latest.version>
<fastDFS.client.version>1.26.1-RELEASE</fastDFS.client.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- springCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.starter.version}</version>
</dependency>
<!-- 通用Mapper启动器 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper.starter.version}</version>
</dependency>
<!-- 分页助手启动器 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pageHelper.starter.version}</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--FastDFS客户端-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>${fastDFS.client.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
创建eureka注册中心服务
(1)创建工程
- 之所以要用
leyou-registry
,为了提高项目的扩展性,方便更换其他注册中心
创建过程
【
leyou
父工程右键【new module】】>【Maven(不要选择Create from archetype
)】>【Next】>【
Name:
leyou-registry
GroupId:
com.leyou
ArtifactId:
leyou-registry
】>
【Finish】
(2)编写pom.xml
,导入eureka服务端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
(3)编写application.yml
,覆盖默认配置
- 通过设置
register-with-eureka:false
,eureka就不会再注册自身
server:
port: 10086
spring:
application:
name: leyou-registry
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
register-with-eureka: false # 设置不注册eureka本身
fetch-registry: false #不拉取eureka服务列表
server:
enable-self-preservation: false # 关闭自我保护
eviction-interval-timer-in-ms: 10000 # 定期清除无效链接
(4)在引导类上添加相关注解
@SpringBootApplication
@EnableEurekaServer
public class LeyouRegistryApplication {
public static void main(String[] args) {
SpringApplication.run(LeyouRegistryApplication.class);
}
}
创建商品微服务
分析
既然是一个全品类的电商购物平台,那么核心自然就是商品。因此我们要搭建的第一个服务,就是商品微服务。其中会包含对于商品相关的一系列内容的管理,包括:
- 商品分类管理
- 品牌管理
- 商品规格参数管理
- 商品管理
- 库存管理
商品微服务的结构
leyou-item是一个微服务,它对外暴露接口,别的服务也会调用接口,获取接口数据,数据的封装需要实体类,所以接口需要关联实体类。
这里使用聚合工程,将要提供的接口和实体类分别放到不同的工程中,以后使用的时候,只需要导入坐标即可。
创建商品服务父工程(leyou-item),然后在其中创建两个子工程
- leyou-item-interface,这个工程放pojo,异常类等,对外暴露接口,提供实体类
- leyou-item-service,这个工程实现业务逻辑,对外提供REST接口
调用关系图:
创建商品微服务的父工程
(1)新建父工程
【
leyou
父工程右键【new module】】>【Maven(不要选择Create from archetype
)】>【Next】>【
Name:
leyou-item
GroupId:
com.leyou.item
ArtifactId:
leyou-item
】>
【Finish】
(2)编写pom.xml
- 因为要创建的是一个聚合工程,所以必须在pom.xml中的
packaging
标签指定为pom
<!--作为聚合工程中管理jar和war工程的管理类的工程,类型必须为pom-->
<packaging>pom</packaging>
创建leyou-item-interface
(1)新建工程
【
leyou-item
父工程右键【new module】】>【Maven(不要选择Create from archetype
)】>【Next】>【
这里父工程一定要选择
leyou-item
Name:
leyou-item-interface
GroupId:
com.leyou.item
ArtifactId:
leyou-item-interface
】>
【Finish】
(2)新建pojo包:com.leyou.item.pojo
创建leyou-item-service
(1)新建工程
【
leyou-item
父工程右键【new module】】>【Maven(不要选择Create from archetype
)】>【Next】>【
这里父工程一定要选择
leyou-item
Name:
leyou-item-service
GroupId:
com.leyou.item
ArtifactId:
leyou-item-service
】>
【Finish】
(2)编写pom.xml
spring-boot-starter-actuator模块
spring-boot-starter-actuator模块是一个spring提供的监控模块。
我们在开运行发过程中,需要实时和定时监控服务的各项状态和可用性。
Spring Boot的spring-boot-starter-actuator 模块(健康监控)功能提供了很多监控所需的接口,可以对应用系统进行配置查看、相关功能统计等。
项目使用依赖介绍
- web
- mybatis
- mysql
- jdbc
- 通用mapper
- eureka-client
- 特别注意:要使用
leyou-item-interface
的pojo包的类,所以要引入这个依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.leyou.item</groupId>
<artifactId>leyou-item-interface</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--springboot检查服务启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
(3)编写application.yml
,覆盖默认配置
server:
port: 8081
spring:
application:
name: item-service
datasource:
url: jdbc:mysql:///leyou
username: root
password: 123456
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
instance:
lease-renewal-interval-in-seconds: 5 # 心跳时间
lease-expiration-duration-in-seconds: 15 #过期时间
mybatis:
type-aliases-package: com.leyou.item.pojo # 设置扫描包别名
(4)在引导类上添加相关注解
@SpringBootApplication
@EnableDiscoveryClient
public class LeyouItemServiceApplication {
public static void main(String[] args) {
SpringApplication.run(LeyouItemServiceApplication.class);
}
}
创建zuul网关工程
(1)新建工程
【
leyou
】父工程右键【new module】】>【Maven(不要选择Create from archetype
)】>【Next】>【
这里父工程一定要选择
leyou
Name:
leyou-gateway
GroupId:
com.leyou
ArtifactId:
leyou-gateway
】>
【Finish】
(2)编写pom.xml
- eureka-client
- netflix-zuul
<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-netflix-zuul</artifactId>
</dependency>
(3)编写application.yml
,覆盖默认配置
- 添加对
leyou-service
的路由规则
server:
port: 10010
spring:
application:
name: leyou-gateway
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
registry-fetch-interval-seconds: 5 # 5s抓取一次服务列表
zuul:
prefix: /api
routes:
item-service: /item/** #路由到商品的微服务
(4)在引导类上添加相关注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class LeyouGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(LeyouGatewayApplication.class);
}
}
创建common工程
这个工程作为utils工具类等公用
(1)新建工程
【
leyou
】父工程右键【new module】】>【Maven(不要选择Create from archetype
)】>【Next】>【
这里父工程一定要选择
leyou
Name:
leyou-common
GroupId:
com.leyou.common
ArtifactId:
leyou-common
】>
【Finish】
测试
访问:http://localhost:10086/
:
然后点击:
新打开的标签页出现:
表示项目搭建成功。
PS:测试中链接可以点是因为我们使用了actuator
模块,它可以对应用程序进行监护和查看,如果没有使用,页面会显示: