電商項目的介紹及其框架搭建
一、項目介紹
- 該電商購物網站是 B2C 模式。
- 用戶可以在線
購買商品
、加入購物車
、下單
- 可以
評論已購買商品
- 管理員可以在
後臺管理商品的上下架
、促銷活動
- 管理員可以
監控商品銷售狀況
- 客服可以在
後臺處理退款操作
- 希望未來3到5年可以支持千萬用戶的使用
二、項目架構縮略圖
前端結構
使用 vue 架構
node.js :相當於 jdk
webPack:用於熱部署;類似於後臺的 Tomcat
npm:相當於後臺的 Maven
vuetify:提供了vue寫的組件
nuxt:
靜態資源CDN服務:可以通過 Nginx 直接進行調用。
系統門戶 Portal :通過 JWT 鑑權進行權限校驗
通過 Nginx 調用後臺接口,並進行負載均衡
後端結構
後臺微服務和zuul都需要在 Eureka 中進行註冊
Eureka 爲了保證高可用
可以進行集羣部署
Zuul 在進行調用後臺服務的時候,需要進行負載均衡
、容錯處理
服務間也可以進行通過 Feign 進行調用
數據存儲
mysql:
redis:
elasticsearch:索引庫,通過RabbitMQ
和 MySQL進行數據同步
三、系統架構說明
商城可以分爲兩部分:後臺管理系統、前臺門戶系統。
- 後臺管理:
- 後臺系統主要包含以下功能:
- 商品管理,包括商品
分類
、品牌
、商品規格
等信息的管理 - 銷售管理,包括
訂單統計
、訂單退款處理
、促銷活動
生成等 - 用戶管理,包括
用戶控制
、凍結
、解鎖
等 - 權限管理,整個
網站的權限控制
,採用JWT鑑權方案
,對用戶
及API
進行權限控制 - 統計,
各種數據的統計分析展示
- 商品管理,包括商品
- 後臺系統會採用前後端分離開發,而且整個後臺管理系統會使用Vue.js框架搭建出單頁應用(SPA)。
- 後臺系統主要包含以下功能:
- 前臺門戶
- 前臺門戶面向的是客戶,包含與客戶交互的一切功能。例如:
搜索商品
加入購物車
下單
評價商品等等
- 前臺門戶面向的是客戶,包含與客戶交互的一切功能。例如:
- 無論是前臺還是後臺系統,都共享相同的微服務集羣,包括:
- 商品微服務:商品及商品分類、品牌、庫存等的服務
- 搜索微服務:實現搜索功能
- 訂單微服務:實現訂單相關
- 購物車微服務:實現購物車相關功能
- 用戶中心:用戶的登錄註冊等功能
- Eureka註冊中心
- Zuul網關服務
- …
四、項目搭建
4.1 技術選型
前端技術:
- 基礎的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
4.2 開發環境
爲了保證開發環境的統一,希望每個人都按照我的環境來配置:
- IDE:我們使用Idea 2017.3 版本
- JDK:統一使用JDK1.8
- 項目構建:maven3.3.9以上版本即可(3.5.2)
- 版本控制工具:git
idea大家可以在我的課前資料中找到。另外,使用幫助大家可以參考課前資料的《idea使用指南.md》
4.3 域名
我們在開發的過程中,爲了保證以後的生產、測試環境統一。儘量都採用域名來訪問項目。
一級域名:www.leyou.com,leyou.com leyou.cn
二級域名:manage.leyou.com/item , api.leyou.com
我們可以通過switchhost工具
來修改自己的host對應的地址,只要把這些域名指向127.0.0.1,那麼跟你用localhost的效果是完全一樣的。
switchhost可以去課前資料尋找。
五、後臺環境的搭建
5.1 創建統一的父工程:excellent
用來管理依賴及其版本,注意是創建project,而不是module
5.2 搭建 EurekaServer
- 創建工程
我們的註冊中心,起名爲:excellent-registry
選擇新建module:
- 在 pom.xml 文件中添加依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
- 修改配置文件
server:
port: 9090
spring:
application:
name: excellent-registry
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:${server.port}/eureka
register-with-eureka: false # 把自己註冊到 eureka 服務列表
fetch-registry: false # 拉取eureka服務信息
server:
enable-self-preservation: false # 關閉自我保護
eviction-interval-timer-in-ms: 5000 # 每隔5秒鐘,進行一次服務列表的清理
- 編寫啓動類
- 報錯
Failed to load property source from location 'classpath:/application.yml'
原因:application.yml 文件有語法錯誤;
解決方案:
解決辦法:
- 1、改正文件中存在錯誤語法的地方。
- 2、File–>Settings–>File Encodings
這三個地方設置成UTF-8格式。重啓啓動項目。- 3、如第一步並未解決問題,則可以用第二步(終極殺招)。
刪除application.yml文件中所有中文註釋。
- 訪問
5.3 搭建 Zuul 網關
- 創建工程
我們的Zuul網關,起名爲:excellent-gateway
選擇新建module:
- 在 pom.xml 中添加依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- springboot提供微服務檢測接口,默認對外提供幾個接口 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
- 修改 application.yml 配置文件
server:
port: 10010
spring:
application:
name: leyou-gateway
eureka:
client:
registry-fetch-interval-seconds: 5
service-url:
defaultZone: http://127.0.0.1:10086/eureka
zuul:
prefix: /api # 路由路徑前綴
- 編寫啓動類
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ExcellentGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ExcellentGatewayApplication.class, args);
System.out.println("Gateway Startup success!!!");
}
}
- 訪問
5.4 創建商品微服務
既然是一個全品類的電商購物平臺,那麼核心自然就是商品。因此我們要搭建的第一個服務,就是商品微服務。其中會包含對於商品相關的一系列內容的管理,包括:
- 商品分類管理
- 品牌管理
- 商品規格參數管理
- 商品管理
- 庫存管理
5.4.1 微服務的結構
因爲與商品的品類相關,工程命名爲excellent-item
.
需要注意的是,我們的excellent-item
是一個微服務,那麼將來肯定會有其它系統需要來調用服務中提供的接口,獲取的接口數據,也需要對應的實體類來封裝
,因此肯定也會使用到接口中關聯的實體類。
因此這裏我們需要使用聚合工程,將要提供的接口及相關實體類放到獨立子工程中,以後別人引用的時候,只需要知道座標即可。
我們會在leyou-item中創建兩個子工程:
- excellent-item-interface:主要是對外暴露的接口及相關實體類
- excellent-item-service:所有業務邏輯及內部使用接口
調用關係如圖所示:
5.4.2 搭建商品微服務【聚合模塊
】
5.4.2.1 創建工程
起名爲:excellent-item
選擇新建module:(具體方式如上所示)
5.4.2.2 在pom.xml 文件中添加依賴
它是一個聚合模塊,所以需要使用 pom.xml 中進行聲明
<!-- 打包方式爲pom -->
<packaging>pom</packaging>
5.4.2.3 在該模塊中新建一個 Module
- 在excellent-item工程上點擊右鍵,選擇new --> module:
5.4.2.4 在該模塊中再新建一個 Module
- 在excellent-item工程上點擊右鍵,選擇new --> module:
5.5 整個微服務的架構
此時可以刪除 excellent-item 工程的 src 目錄
5.5.1 添加依賴
接下來我們給excellent-item-service
中添加依賴:
思考一下我們需要什麼?
- Eureka客戶端
- web啓動器
- mybatis啓動器
- 通用mapper啓動器
- 分頁助手啓動器
- 連接池,我們用默認的Hykira
- mysql驅動
- 千萬不能忘了,我們自己也需要
excellent-item-interface
中的實體類
<dependencies>
<!-- web啓動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- mybatis的啓動器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- 通用mapper啓動器 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
<!-- 分頁助手啓動器 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
<!-- jdbc啓動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.excellent.item</groupId>
<artifactId>excellent-item-interface</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!-- springboot檢測服務啓動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 單元測試類 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
5.5.2. 在 application.yml
server:
port: 8080
spring:
application:
name: item-service
datasource:
url: jdbc:mysql://localhost:3306/market
username: root
password: root
hikari:
max-lifetime: 28830000 # 一個連接的生命時長(毫秒),超時而且沒被使用則被釋放(retired),缺省:30分鐘,建議設置比數據庫超時時長少30秒,參考MySQL wait_timeout參數(show variables like '%timeout%';)
maximum-pool-size: 9 # 連接池中允許的最大連接數。缺省值:10;推薦的公式:((core_count * 2) + effective_spindle_count)
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9090/eureka
instance:
lease-renewal-interval-in-seconds: 5 # 5秒鐘發送一次心跳
lease-expiration-duration-in-seconds: 10 # 10秒不發送就過期
#添加 mybatis 的別名掃描
mybatis:
type-aliases-package: com.excellent.item.pojo
5.5.3 新建包掃描路徑下的目錄
5.5.4 編寫引導類
@SpringBootApplication
@EnableDiscoveryClient
public class ExcellentItemServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ExcellentItemServiceApplication.class);
System.out.println("Excellent-Item-ServiceApplication Startup success!!!");
}
}
5.6 搭建一個通用的工具類模塊 Module
- 搭建通用工具模塊