乐优商城(01)--项目启动
樂優(yōu)商城(01)–項(xiàng)目啟動(dòng)
一、項(xiàng)目介紹
- 樂優(yōu)商城是一個(gè)全品類的電商購(gòu)物網(wǎng)站(B2C)。
- 用戶可以在線購(gòu)買商品,加入購(gòu)物車,下單,秒殺商品
- 可以品論已購(gòu)買商品
- 管理員可以在后臺(tái)管理商品的上下架,促銷活動(dòng)
- 管理員可以監(jiān)控商品銷售狀況
- 客服可以在后臺(tái)處理退款操作
二、項(xiàng)目搭建
2.1、技術(shù)選型
前端技術(shù):
-
基礎(chǔ)的HTML,CSS,JavaScript的(基于ES6標(biāo)準(zhǔn))
-
JQuery的
-
Vue.js 2.0以及基于Vue的框架:Vuetify
-
前端構(gòu)建工具:的WebPack
-
前端安裝包工具:NPM
-
Vue的腳手架:Vue的-CLI
-
Vue的路由:VUE路由器
-
AJAX框架:愛可信
-
基于Vue的富文本框架:quill-editor
后端技術(shù):
-
基礎(chǔ)的SpringMVC、Spring 5.0和MyBatis3
-
Spring Boot 2.3.11.RELEASE版本
-
Spring Cloud Alibaba Hoxton.SR8版本
-
Redis-6.0.15
-
RabbitMQ-3.4.1-1
-
Elasticsearch-7.6.2
-
nginx-1.20.1:
-
FastDFS - 5.0.8
-
MyCat
-
Thymeleaf
2.2、域名
一級(jí)域名:www.leyou.com //前端門戶網(wǎng)站主域名
二級(jí)域名:manage.leyou.com、api.leyou.com //后端管理域名以及api調(diào)用域名
2.3、前端環(huán)境安裝
安裝node以及NPM工具
下載地址:https://nodejs.org/en/
下載完成后,cmd控制臺(tái)輸入,看見版本即說明安裝成功
node -vnode自帶NPM,控制臺(tái)輸入npm -v查看版本信息
npm默認(rèn)的倉(cāng)庫(kù)地址是在國(guó)外網(wǎng)站,速度較慢,建議設(shè)置到淘寶鏡像。但是切換鏡像是比較麻煩的。推薦一款切換鏡像的工具:nrm
安裝nrm,這里-g代表全局安裝。
npm install nrm -g然后通過nrm ls命令查看npm的倉(cāng)庫(kù)列表,帶*的就是當(dāng)前選中的鏡像倉(cāng)庫(kù):
通過nrm test來測(cè)試所有鏡像的下載速度,通過nrm use taobao來指定要使用的taobao鏡像源
注意:
- 有教程推薦使用cnpm命令,但是使用發(fā)現(xiàn)cnpm有時(shí)會(huì)有bug,不推薦。
安裝vue,下載地址:https://github.com/vuejs/vue
官網(wǎng):https://cn.vuejs.org/
隨意創(chuàng)建一個(gè)目錄,進(jìn)入該目錄cmd的控制臺(tái)
輸入命令npm init -y進(jìn)行初始化
安裝vue:輸入命令npm install vue --save
成功后,會(huì)發(fā)現(xiàn)多一個(gè)node_modules目錄,并且在下面有一個(gè)vue目錄。
node_modules是通過npm安裝的所有模塊的默認(rèn)位置。
2.4、后臺(tái)管理系統(tǒng)頁面搭建
解壓壓縮包leyou-manage-web.zip,解壓后進(jìn)入該目錄cmd控制臺(tái),輸入命令npm install安裝所需依賴
也可以將該項(xiàng)目用idea打開,點(diǎn)擊Terminal即開啟一個(gè)控制臺(tái)
運(yùn)行測(cè)試:npm run dev
若想修改啟動(dòng)端口可以進(jìn)入路徑config/index.js下進(jìn)行修改
項(xiàng)目目錄結(jié)構(gòu):
三、環(huán)境配置
3.1、統(tǒng)一環(huán)境
目前訪問后臺(tái)頁面的路徑為:http://localhost:9001,這不夠優(yōu)雅,對(duì)其進(jìn)行域名映射
之前有對(duì)項(xiàng)目所使用的域名有規(guī)定:
- 主域名是:www.leyou.com
- 管理系統(tǒng)域名:manage.leyou.com
- 網(wǎng)關(guān)域名:api.leyou.com
我們將以上域名進(jìn)行映射
3.2、域名解析
一個(gè)域名一定會(huì)被解析為一個(gè)或多個(gè)ip。這一般會(huì)包含兩步:
-
本地域名解析
瀏覽器會(huì)首先在本機(jī)的hosts文件中查找域名映射的IP地址,如果查找到就返回IP ,沒找到則進(jìn)行域名服務(wù)器解析,一般本地解析都會(huì)失敗,因?yàn)槟J(rèn)這個(gè)文件是空的。
-
Windows下的hosts文件地址:C:/Windows/System32/drivers/etc/hosts
-
Linux下的hosts文件所在路徑: /etc/hosts
-
-
域名服務(wù)器解析
本地解析失敗,才會(huì)進(jìn)行域名服務(wù)器解析,域名服務(wù)器就是網(wǎng)絡(luò)中的一臺(tái)計(jì)算機(jī),里面記錄了所有注冊(cè)備案的域名和ip映射關(guān)系,一般只要域名是正確的,并且備案通過,一定能找到
3.3、域名映射
官網(wǎng)下載nginx:http://nginx.org/en/download.html
選中windows版本,將壓縮包解壓即可
修改本地host文件,添加映射關(guān)系
127.0.0.1 manage.leyou.com 127.0.0.1 api.leyou.com修改后,本地訪問時(shí)路徑為http://manage.leyou.com:9001,這不夠優(yōu)雅,我們利用nginx進(jìn)行反向代理
nginx配置文件中添加下面內(nèi)容:
server {listen 80;server_name manage.leyou.com;proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Server $host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;location / {proxy_pass http://127.0.0.1:9001;proxy_connect_timeout 1000;proxy_read_timeout 1000;}}server {listen 80;server_name api.leyou.com;proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Server $host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}nginx的相關(guān)命令
#啟動(dòng) start nginx #停止 nginx -s stop #重啟 nginx -s reload3.4、測(cè)試
啟動(dòng)nginx,瀏覽器輸入manage.leyou.com后即進(jìn)入控制臺(tái)頁面
可能的錯(cuò)誤:Invalid Host header
原因:manage.leyou.com映射的ip是127.0.0.1,但是webpack會(huì)驗(yàn)證host是否符合配置。
解決辦法:在webpack.dev.conf.js中取消host驗(yàn)證:disableHostCheck: true
重新執(zhí)行npm run dev,刷新瀏覽器
流程分析:
四、創(chuàng)建父工程
創(chuàng)建統(tǒng)一的父工程:myLeyou,用來管理項(xiàng)目依賴及其版本,這里直接選用springBoot腳手架快速搭建
創(chuàng)建成功后,修改pom.xml文件,并將多余的目錄文件刪除
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.leyou.parent</groupId><artifactId>myLeyou</artifactId><version>0.0.1-SNAPSHOT</version><name>myLeyou</name><description>Demo project for Spring Boot</description><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.alibaba.version>2.2.5.RELEASE</spring.cloud.alibaba.version><spring.boot.version>2.3.11.RELEASE</spring.boot.version><spring.cloud.version>Hoxton.SR8</spring.cloud.version><mysql.version>5.1.47</mysql.version><fastDFS.client.version>1.26.1-RELEASE</fastDFS.client.version><mybatis.starter.version>2.1.1</mybatis.starter.version><mapper.starter.version>2.1.5</mapper.starter.version><pageHelper.starter.version>1.2.13</pageHelper.starter.version></properties><dependencyManagement><!--Spring Cloud alibaba的版本管理, 通過dependency完成繼承--><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring.cloud.alibaba.version}</version><type>pom</type><scope>import</scope></dependency><!--SpringBoot的版本管理--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>${spring.boot.version}</version><type>pom</type><scope>import</scope></dependency><!--Spring Cloud的版本管理--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring.cloud.version}</version><type>pom</type><scope>import</scope></dependency><!-- mybatis啟動(dòng)器 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${mybatis.starter.version}</version></dependency><!-- 通用Mapper啟動(dòng)器 --><dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>${mapper.starter.version}</version></dependency><!-- 分頁助手啟動(dòng)器 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>${pageHelper.starter.version}</version></dependency><!-- mysql驅(qū)動(dòng) --><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><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build></project>該工程的pom.xml文件,導(dǎo)入了我們后續(xù)所需要的用到的依賴,在該目錄下創(chuàng)建的子工程就不需要管理版本了
父工程目錄結(jié)構(gòu):
五、安裝Nacos注冊(cè)中心
查看官網(wǎng)https://github.com/alibaba/spring-cloud-alibaba/wiki/版本說明
選擇1.4.1的版本進(jìn)行下載安裝,這里我安裝在阿里云的服務(wù)器中,注意開放阿里云安全組中的端口默認(rèn)8848
5.1、下載安裝
下載地址:https://github.com/alibaba/nacos/tags
官方文檔:https://nacos.io/zh-cn/docs/deployment.html
將下載的安裝包上傳至/opt/目錄下(補(bǔ)充:服務(wù)器要安裝jdk)
輸入命令進(jìn)行解壓
tar -zxvf nacos-server-1.4.1.tar.gz解壓后,進(jìn)入nacos目錄下
cd nacos輸入命令單機(jī)啟動(dòng)
bin/startup.sh -m standalone也可以修改startup.sh文件,將MODE修改為standalone,一勞永逸
瀏覽器訪問 ip+端口/nacos,即進(jìn)入Nocas的登陸頁面,默認(rèn)用戶、密碼都為nacos
5.2、Nacos的簡(jiǎn)單介紹
這里簡(jiǎn)單介紹下Nacos:
官方:一個(gè)更易于構(gòu)建云原生應(yīng)用的動(dòng)態(tài)服務(wù)發(fā)現(xiàn)(Nacos Discovery )、服務(wù)配置(Nacos Config)和服務(wù)管理平臺(tái)
集 注冊(cè)中心+配置中心+服務(wù)管理 平臺(tái) 于一身
Nacos 的關(guān)鍵特性包括:
- 服務(wù)發(fā)現(xiàn)和服務(wù)健康監(jiān)測(cè)
- 動(dòng)態(tài)配置服務(wù)
- 動(dòng)態(tài) DNS 服務(wù)
- 服務(wù)及其元數(shù)據(jù)管理
核心功能:
-
服務(wù)注冊(cè):Nacos Client會(huì)通過發(fā)送REST請(qǐng)求的方式向Nacos Server注冊(cè)自己的服務(wù),提供自身的元數(shù)據(jù),比如ip地 址、端口等信息。Nacos Server接收到注冊(cè)請(qǐng)求后,就會(huì)把這些元數(shù)據(jù)信息存儲(chǔ)在一個(gè)雙層的內(nèi)存Map中。
-
服務(wù)心跳:在服務(wù)注冊(cè)后,Nacos Client會(huì)維護(hù)一個(gè)定時(shí)心跳來持續(xù)通知Nacos Server,說明服務(wù)一直處于可用狀態(tài),防 止被剔除。默認(rèn)5s發(fā)送一次心跳。
-
服務(wù)同步:Nacos Server集群之間會(huì)互相同步服務(wù)實(shí)例,用來保證服務(wù)信息的一致性。
-
服務(wù)發(fā)現(xiàn):服務(wù)消費(fèi)者(Nacos Client)在調(diào)用服務(wù)提供者的服務(wù)時(shí),會(huì)發(fā)送一個(gè)REST請(qǐng)求給NacosServer,獲取上面 注冊(cè)的服務(wù)清單,并且緩存在Nacos Client本地,同時(shí)會(huì)在Nacos Client本地開啟一個(gè)定時(shí)任務(wù)定時(shí)拉取服務(wù)端最新的注冊(cè)表信息更新到本地緩存
-
服務(wù)健康檢查:Nacos Server會(huì)開啟一個(gè)定時(shí)任務(wù)用來檢查注冊(cè)服務(wù)實(shí)例的健康情況,對(duì)于超過15s沒有收到客戶端心跳的實(shí)例會(huì)將它的healthy屬性置為false(客戶端服務(wù)發(fā)現(xiàn)時(shí)不會(huì)發(fā)現(xiàn)),如果某個(gè)實(shí)例超過30秒沒有收到心跳,直接剔除該實(shí)例(被剔除的實(shí)例如果恢復(fù)發(fā)送心跳則會(huì)重新注冊(cè))
六、創(chuàng)建gateway網(wǎng)關(guān)
選擇maven方式創(chuàng)建Module,然后填寫項(xiàng)目名稱,命名為:leyou-gateway
添加依賴
<?xml version="1.0" encoding="UTF-8"?> <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"><parent><artifactId>myLeyou</artifactId><groupId>com.leyou.parent</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>leyou-gateway</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator --><dependency><groupId>org.hibernate.validator</groupId><artifactId>hibernate-validator</artifactId><version>6.1.6.Final</version></dependency><!-- springboot提供微服務(wù)檢測(cè)接口,默認(rèn)對(duì)外提供幾個(gè)接口 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies></project>添加依賴會(huì)報(bào)一個(gè)錯(cuò)誤,目前也不知道什么原因,就是下載不下來該版本
解決方法是:添加6.1.6.Final的版本
編寫啟動(dòng)類
@SpringBootApplication @EnableDiscoveryClient //該注解,在alibaba框架中是默認(rèn)開啟的,這里顯示標(biāo)注 public class LeyouGateWayApplication {public static void main(String[] args) {SpringApplication.run(LeyouGateWayApplication.class,args);} }配置文件
server:port: 10010 spring:application:name: leyou-gatewaycloud:nacos:discovery:server-addr: #Nacos安裝的機(jī)器 ip+端口username: nacospassword: nacos到這里,基礎(chǔ)服務(wù)搭建完畢,為了便于開發(fā),Nacos的配置中心服務(wù)后面再添加
spring-cloud-gateway簡(jiǎn)單介紹
網(wǎng)關(guān)作為流量的入口,常用的功能包括路由轉(zhuǎn)發(fā),權(quán)限校驗(yàn),限流等。
Spring Cloud Gateway 是Spring Cloud官方推出的第二代網(wǎng)關(guān)框架,定位于取代 Netflix Zuul1.0。相比 Zuul 來說,Spring Cloud Gateway 提供更優(yōu)秀的性能,更強(qiáng)大的有功能。
Spring Cloud Gateway 是由 WebFlux + Netty + Reactor 實(shí)現(xiàn)的響應(yīng)式的 API 網(wǎng)關(guān)。它不能在傳統(tǒng)的 servlet 容器中工作,也不能構(gòu) 建成 war 包。
Spring Cloud Gateway 旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單且有效的 API 路由的管理方式,并基于 Filter 的方式提供網(wǎng)關(guān)的基本功能,例如 說安全認(rèn)證、監(jiān)控、限流等等
Spring Cloud Gateway 功能特征
- 基于Spring Framework 5, Project Reactor 和 Spring Boot 2.0 進(jìn)行構(gòu)建;
- 動(dòng)態(tài)路由:能夠匹配任何請(qǐng)求屬性;
- 支持路徑重寫;
- 集成 Spring Cloud 服務(wù)發(fā)現(xiàn)功能(Nacos、Eruka);
- 可集成流控降級(jí)功能(Sentinel、Hystrix);
- 可以對(duì)路由指定易于編寫的 Predicate(斷言)和 Filter(過濾器)
核心概念
- 路由(route) 路由是網(wǎng)關(guān)中最基礎(chǔ)的部分,路由信息包括一個(gè)ID、一個(gè)目的URI、一組斷言工廠、一組Filter組成。如果斷言為真,則說明請(qǐng)求的URL和 配置的路由匹配。
- 斷言(predicates) Java8中的斷言函數(shù),SpringCloud Gateway中的斷言函數(shù)類型是Spring5.0框架中的ServerWebExchange。斷言函數(shù)允許開發(fā)者去定義 匹配Http request中的任何信息,比如請(qǐng)求頭和參數(shù)等。
- 過濾器(Filter) SpringCloud Gateway中的filter分為Gateway FilIer和Global Filter。Filter可以對(duì)請(qǐng)求和響應(yīng)進(jìn)行處理
網(wǎng)關(guān)執(zhí)行流程:
Gateway Client向Gateway Server發(fā)送請(qǐng)求
請(qǐng)求首先會(huì)被HttpWebHandlerAdapter進(jìn)行提取組裝成網(wǎng)關(guān)上下文
然后網(wǎng)關(guān)的上下文會(huì)傳遞到DispatcherHandler,它負(fù)責(zé)將請(qǐng)求分發(fā)給RoutePredicateHandlerMapping
RoutePredicateHandlerMapping負(fù)責(zé)路由查找,并根據(jù)路由斷言判斷路由是否可用
如果斷言成功,由FilteringWebHandler創(chuàng)建過濾器鏈并調(diào)用
請(qǐng)求會(huì)一次經(jīng)過PreFilter–微服務(wù)–PostFilter的方法,最終返回響應(yīng)
與其他網(wǎng)關(guān)組件相比
| gateway | 6.61ms | 3.24K |
| linkered | 7.62ms | 2.82k |
| zuul | 12.56ms | 2.09k |
| none | 2.09ms | 11.77k |
七、創(chuàng)建商品微服務(wù)
7.1、微服務(wù)結(jié)構(gòu)
該服務(wù)為核心工程,包含對(duì)商品相關(guān)的一系列內(nèi)容的管理:
- 商品分類管理
- 品牌管理
- 商品規(guī)格參數(shù)管理
- 商品管理
- 庫(kù)存管理
與商品的品類相關(guān),工程命名為leyou-item.
需要注意的是,leyou-item是一個(gè)微服務(wù),那么將來肯定會(huì)有其它系統(tǒng)需要來調(diào)用服務(wù)中提供的接口,獲取的接口數(shù)據(jù),也需要對(duì)應(yīng)的實(shí)體類來封裝,因此肯定也會(huì)使用到接口中關(guān)聯(lián)的實(shí)體類。
因此這里需要使用聚合工程,將要提供的接口及相關(guān)實(shí)體類放到獨(dú)立子工程中,后續(xù)要引用的時(shí)候,只需要導(dǎo)入依賴坐標(biāo)即可。
在leyou-item中創(chuàng)建兩個(gè)子工程:
- leyou-item-interface:主要是對(duì)外暴露的接口及相關(guān)實(shí)體類
- leyou-item-service:所有業(yè)務(wù)邏輯及內(nèi)部使用接口
7.2、leyou-item父工程創(chuàng)建
依然使用maven方式創(chuàng)建工程
因?yàn)槭蔷酆瞎こ?#xff0c;所以將項(xiàng)目打包的方式設(shè)置為pom
<?xml version="1.0" encoding="UTF-8"?> <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"><parent><artifactId>myLeyou</artifactId><groupId>com.leyou.parent</groupId><version>0.0.1-SNAPSHOT</version></parent><packaging>pom</packaging><modelVersion>4.0.0</modelVersion><groupId>com.leyou.item</groupId><artifactId>leyou-item</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>這里補(bǔ)充一句:打包方式為pom的模塊一般為父模塊,用來管理子模塊。因此對(duì)父模塊的maven命令會(huì)對(duì)其依賴的子模塊若有依賴順序(子模塊與子模塊之間的依賴順序),按依賴順序依次執(zhí)行。方便了項(xiàng)目的關(guān)聯(lián)不需要一個(gè)一個(gè)模塊進(jìn)行同樣的操作,且具有很好的擴(kuò)展性,可拆分性。建議創(chuàng)建maven項(xiàng)目時(shí),以聚合項(xiàng)目(擁有父模塊)為好。
7.3、leyou-item-interface
在leyou-item工程上點(diǎn)擊右鍵,選擇new --> module,依然是使用maven構(gòu)建,注意父工程是leyou-item:
注意:目錄結(jié)構(gòu),保存到leyou-item下的leyou-item-interface目錄中
7.4、leyou-item-service
與leyou-item-interface類似,選擇在leyou-item上右鍵,新建module,然后填寫項(xiàng)目信息:
這里我刪除了父工程的src目錄
7.5、添加依賴
給leyou-item-service中添加依賴:
思考需要的依賴包:
- Nacos服務(wù)注冊(cè)
- web啟動(dòng)器
- mybatis啟動(dòng)器
- 通用mapper啟動(dòng)器
- 分頁助手啟動(dòng)器
- 連接池,用默認(rèn)的Hykira
- mysql驅(qū)動(dòng)
- 以及l(fā)eyou-item-interface中的實(shí)體類
這些依賴,在頂級(jí)父工程已經(jīng)被管理,所以直接引入即可,無需導(dǎo)入版本號(hào)
<?xml version="1.0" encoding="UTF-8"?> <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"><parent><artifactId>leyou-item</artifactId><groupId>com.leyou.item</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>leyou-item-service</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!--Nacos服務(wù)注冊(cè)--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- web啟動(dòng)器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- mybatis的啟動(dòng)器 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><!-- 通用mapper啟動(dòng)器 --><dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId></dependency><!-- 分頁助手啟動(dòng)器 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId></dependency><!-- jdbc啟動(dòng)器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- mysql驅(qū)動(dòng) --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.leyou.item</groupId><artifactId>leyou-item-interface</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- springboot檢測(cè)服務(wù)啟動(dòng)器 可要可不要 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies></project>7.6、編寫啟動(dòng)類和配置文件
@SpringBootApplication @EnableDiscoveryClient public class LeyouItemApplication {public static void main(String[] args) {SpringApplication.run(LeyouItemApplication.class,args);} }配置文件:
server:port: 8081 spring:application:name: item-servicedatasource:url: jdbc:mysql://127.0.0.1:3306/leyouusername: rootpassword: 123456hikari:max-lifetime: 28830000 # 一個(gè)連接的生命時(shí)長(zhǎng)(毫秒),超時(shí)而且沒被使用則被釋放(retired),缺省:30分鐘,建議設(shè)置比數(shù)據(jù)庫(kù)超時(shí)時(shí)長(zhǎng)少30秒,參考MySQL wait_timeout參數(shù)(show variables like '%timeout%';)maximum-pool-size: 9 # 連接池中允許的最大連接數(shù)。缺省值:10;推薦的公式:((core_count * 2) + effective_spindle_count)driver-class-name: com.mysql.jdbc.Drivercloud:nacos:discovery:server-addr: Nacos服務(wù)的ip+端口username: nacospassword: nacos7.7、添加商品微服務(wù)的路由規(guī)則
既然商品微服務(wù)已經(jīng)創(chuàng)建,接下來肯定要添加路由規(guī)則到gateway中,不使用默認(rèn)的路由規(guī)則。
在leyou-gateway工程的application.yml配置文件添加:
cloud:gateway:routes:- id: item-routeruri: http://127.0.0.1:8010predicates:- Path=/api/item/**filters:- StripPrefix=2 #注意這里暫時(shí)寫為2,用于測(cè)試 該屬性作用為:轉(zhuǎn)發(fā)之前去掉2層路徑7.8、啟動(dòng)測(cè)試
啟動(dòng)leyou-gateway,leyou-item-service
查看Nacos面板
7.9、測(cè)試路由
由于之前導(dǎo)入的依賴,所以無需寫接口測(cè)試
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId> </dependency>在瀏覽器輸入地址:127.0.0.1:8081/actuator/info
因?yàn)闆]有添加信息,所以是一個(gè)空的json,但是可以肯定的是:能夠訪問到item-service了。
接下來通過路由訪問試試,根據(jù)路由規(guī)則,需要訪問的地址是:127.0.0.1:10010/api/item/actuator/info
八、創(chuàng)建通用工具模塊
有些工具或通用的約定內(nèi)容,希望各個(gè)服務(wù)共享,因此需要?jiǎng)?chuàng)建一個(gè)工具模塊:leyou-common
右鍵myLeyou工程,使用maven來構(gòu)建module:
目錄結(jié)構(gòu)
總結(jié)
以上是生活随笔為你收集整理的乐优商城(01)--项目启动的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bootstrap --- 分页
- 下一篇: 计算机实物知识需求市场调研,能力本位计算