電商專案搭建(一):架構概述&詳細操作指南
基本架構拆分如下:
其中最為關鍵的幾個部分:
支付系統:對外暴露介面,但是內在集成了N多系統
搜尋系統:需要利用搜索引擎,比如Lurence,進行全文檢索,可以用solr elasticsearch 來簡化搜尋的開發
秒殺系統:需要獨立成一個服務,避免高併發影響其他子系統
廣告系統&內容系統:技術含量不高,但是是垂直電商區分水平電商的關鍵
大概的結構如下:
shop-parent
|—shop-goods-api(商品系統介面)
|—shop-goods-server(商品系統)
|—shop-order-api(訂單系統介面)
|—shop-order-server(訂單系統)
|—shop-member-api(會員系統介面)
|—shop-member-server(會員系統)
|—shop-mgrsite(後臺管理介面)
|—shop-mobile(提供移動端的介面專案)
|—shop-scheduler(定時任務專案)
|—shop-common(公共專案)
其中scheduler定時任務專案,不能整合到其他系統中,只是單獨做一個系統出來就OK(且不叢集),這樣就可以保證定時任務只會執行一次,避免效能的浪費
以上說明完成,接下來就應該做具體的搭建了
首先需要說明的是,這裡使用的工具是STS,後面不再贅述
1.準備工作
說明:首先我們想要所有的專案到放在一個資料夾下,所以
首先選擇檢視模式為Working Sets
接下來建立在Configure Working Set…中建立對應的資料夾
附帶說明:
使用STS的同時必須要記得開啟程式碼提示,如何開啟程式碼提示
具體如何開啟程式碼提示:https://blog.csdn.net/baidu_32739019/article/details/64121023
如果以上工作都完成了,那麼基本準備也就完成了
- 基本架構搭建
基本思路:
首先搭建架構之前,先看看基本的架構圖:
這裡我們先根據shop-goods這條線搭建基本的架構:
shop-parent --> shop-common —>shop-goods-api —>> shop-goods-server
----------------------------------------------------------------------->> shop-mgrsite
2.1 搭建父專案
首先新建一個Maven專案:
2.2 搭建子專案
打包方式說明:
由於我們這裡的架構是做微服務的架構,因此spring boot 必不可少,spring boot的專案都有一個特點,就是web服務要儘量打成JAR包
具體原因是:
springboot 內部集成了tomcat的伺服器,WEB應用的JAR可以直接被載入,
但是war包就不一樣了,需要Jetty或者tomcat容器去載入,那麼必然會引起Jar衝突
看到下面的module生成了,就說明成功了
接下來就用相同的方法,完成下面其餘Module的建立
3.建立關聯
回到我們之間的架構圖,可以發現:
一、 所有的專案都是parent的子專案
二、 專案之間彼此有依賴關係
從Goods線來看:
- shop-goods-api是依賴shop-common
- 而shop-goods-server和shop-mgrsite都是依賴shop-goods-api的
附:為什麼兩個專案會去依賴:shop-goods-api?
原因是:shop-goods-server和shop-mgrsite有共同的domain和介面類,為了方便統一管理,故抽出做管理
以上內容在本人的ZooKeeper: 在CentOS7中配置Zookeeper並搭建測試環境有詳細的闡述:
https://blog.csdn.net/fenghuoliuxing990124/article/details/84979590
回到正題,我們開始建立對應的依賴,由於上面要建立依賴,於是在parent中:
在shop-goods-api中依賴shop-common:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.csdn.shop</groupId>
<artifactId>shop_demo-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>shop_demo-goods-api</artifactId>
<name>shop_demo-goods-api</name>
<dependencies>
<dependency>
<groupId>cn.csdn.shop</groupId>
<artifactId>shop_demp-common</artifactId>
</dependency>
</dependencies>
</project>
附:可能會出現的問題:missing artifact
如果出現此類的問題:簡單來說:無解
請重新刪除出現問題的module後(包括硬碟),重新new一個module來解決該問題
建議不要在網上找答案(浪費時間),因為maven的依賴是個玄學問題
4.搭建SpringBoot環境
由於我們要搭建的是微服務,下一步就是為搭建微服務提供基本的SpringBoot環境
首先我們這裡為parent匯入的父依賴是:
<!--Spring Boot父專案-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
在匯入子專案所需依賴之前,先點選spring-boot-starter-parent跳入依賴中,
複製下面這段話,並將JDK的版本從1.6修改為1.8
<properties>
<java.version>1.8</java.version>
<resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't clash with Spring ${} placeholders -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
接下來我們要為子專案匯入必要的依賴:
<!-- Spring Boot Dubbo 依賴 -->
<dependency>
<groupId>com.gitee.reger</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.10</version>
</dependency>
<!--spring-boot-web依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--紅辣椒-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--阿里巴巴fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<!--上傳檔案工具類-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!--springboot-redis依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--spring-boot的activeMQ依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
剩下將下面的依賴匯入parent的dependencyManagement
<!--阿里巴巴的druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.25</version>
</dependency>
<!--spring-boot的mybatis依賴 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
OK,至此電商專案基本的架構就完成了,下一步就是逐步完善了