基于Ant DesignPro Vue + SpringBoot 前后端分離 - 后端微服化 + 接口網(wǎng)關(guān) + Nacos + Sentinel
通過Ant DesignPro Vue + SpringBoot 搭建的后臺(tái)管理系統(tǒng)后,實(shí)現(xiàn)了前后端分離,并實(shí)現(xiàn)了登錄認(rèn)證,認(rèn)證成功后返回該用戶相應(yīng)權(quán)限范圍內(nèi)可見的菜單。
- 后端采用SpringCloud構(gòu)建微服,采用SpringCloud Gateway做為服務(wù)網(wǎng)關(guān),采用Nacos做為統(tǒng)一配置中心,并在服務(wù)網(wǎng)關(guān)部分解決了前端跨域調(diào)用的問題。
- 前端VUE的所有向后端的請求全部指向服務(wù)網(wǎng)關(guān),接口網(wǎng)關(guān)根據(jù)Url請求路徑為/api/auth/或/api/account/,則將請求轉(zhuǎn)發(fā)至ms-login服務(wù);接口網(wǎng)關(guān)根據(jù)Url請求路徑為/api/user/,則將請求轉(zhuǎn)發(fā)至ms-user服務(wù);
- 通過Sentinel實(shí)現(xiàn)對網(wǎng)關(guān)調(diào)用接口的限流、溶斷
Sentinel簡介:
隨著微服務(wù)的流行,服務(wù)和服務(wù)之間的穩(wěn)定性變得越來越重要。Sentinel 是面向分布式、多語言異構(gòu)化服務(wù)架構(gòu)的流量治理組件,主要以流量為切入點(diǎn),從流量路由、流量控制、流量整形、熔斷降級、系統(tǒng)自適應(yīng)過載保護(hù)、熱點(diǎn)流量防護(hù)等多個(gè)維度來幫助開發(fā)者保障微服務(wù)的穩(wěn)定性。
備注:【Sentinel官方文檔中心】
Ant Design Pro相關(guān)系列文章:
一、AntDesign Pro安裝過程
二、基于Ant DesignPro實(shí)現(xiàn)通過SpringBoot后臺(tái)加載自定義菜單-前端部分
三、基于Ant DesignPro實(shí)現(xiàn)通過SpringBoot后臺(tái)加載自定義菜單-SpringBoot后端部分
四、搭建Vue版Ant Design Pro后臺(tái)管理系統(tǒng)
五、基于Ant DesignPro Vue實(shí)現(xiàn)通過SpringBoot后臺(tái)加載自定義菜單- 前后端分離
六、基于Ant DesignPro Vue + SpringBoot 前后端分離 - 部署后解決跨域的問題
七、基于Ant DesignPro Vue + SpringBoot 前后端分離 - 后端微服化 + 接口網(wǎng)關(guān) + Nacos
八、基于Ant DesignPro Vue + SpringBoot 前后端分離 - 后端微服化 + 接口網(wǎng)關(guān) + Nacos + Sentinel
源碼開源地址
- 后端Springboot工程代碼已上傳gitee,地址:https://gitee.com/duyanjun/mc-cloud.git
- 本文章中代碼是將文章【六、基于Ant DesignPro Vue + SpringBoot 前后端分離 - 部署后解決跨域的問題】中單體改造為微服架構(gòu)
- 本文章中代碼是將文章【七、基于Ant DesignPro Vue + SpringBoot 前后端分離 - 后端微服化 + 接口網(wǎng)關(guān) + Nacos】基礎(chǔ)上斷續(xù)集成了Sentinel
一、采用的技術(shù)架構(gòu)
序號 | 技術(shù)框架 | 說明 |
---|---|---|
1 | Spring Cloud Alibaba | Spring Cloud Alibaba微服體系架構(gòu) |
2 | Nacos | Api網(wǎng)關(guān)、服務(wù)注冊發(fā)現(xiàn)配置管理中心 |
3 | Spring Cloud Gateway | Spring Cloud 網(wǎng)關(guān) |
4 | Ant Design Pro Vue | 前端頁面 |
5 | Sentinel | 微服流控 |
二、Maven工程結(jié)構(gòu)
|-- ms-cloud # Root工程
| |-- ms-gateway # 服務(wù)網(wǎng)關(guān)
| |-- ms-mcv # 后端工程
| |-- ms-mcv-common # 后端工程-公共部分,例如實(shí)體類
| |-- ms-mcv-login # 后端工程-登錄接口服務(wù)
| |-- ms-mcv-user # 后端工程-用戶接口服務(wù)
三、微服架構(gòu)
四、前期準(zhǔn)備
1、微服架構(gòu)搭建
請參照文章【七、基于Ant DesignPro Vue + SpringBoot 前后端分離 - 后端微服化 + 接口網(wǎng)關(guān) + Nacos】搭建微服架構(gòu),實(shí)現(xiàn)Ant DesignPro Vue前端通過SpringCloud Gateway服務(wù)網(wǎng)關(guān)來統(tǒng)一調(diào)用后端微服務(wù)的各接口;
2、安裝Sentinel
1)、下載Sentinel
從Sentinel的Github【Release】畫面下載最新版的jar
2)、啟動(dòng)Sentinel
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-2.0.0-alpha-preview.jar
參數(shù)說明:
- Dserver.port:指定Sentinel服務(wù)端口
- Dcsp.sentinel.dashboard.server:指定訪問Sentinel服務(wù)控制臺(tái)畫面的地址
- Dproject.name: 指定應(yīng)用名稱
3)、訪問Sentinel
在瀏覽器輸入url http://127.0.0.1:8080,用戶名和密碼都是sentinel
五、集成Sentinel
1、在網(wǎng)關(guān)pom.xml中添加依賴
<!-- 集成sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
2、在application.yml中添加sentinel配置
server:
port: 9999
logging:
level:
com.ms: debug
pattern:
dateformat: MM-dd HH:mm:ss:SSS
spring:
application:
name: ms-gateway
cloud:
nacos:
server-addr: 192.168.2.146:8848
gateway:
enabled: true
routes:
- id: ms-login
uri: lb://ms-login
predicates:
- Path=/api/auth/**,/api/account/**
- id: ms-user
uri: lb://ms-user
predicates:
- Path=/api/user/**
sentinel:
eager: true # 立即加載
filter:
enabled: false
transport:
port: 9998
dashboard: 192.168.0.9:8080
clientIp: 192.168.0.100
scg:
fallback:
content-type: application/json
mode: response
response-status: 200
response-body: '{"code":0,"message":"服務(wù)器現(xiàn)在忙碌,請稍后再試..."}'
config:
import:
- optional:nacos:application.yml
- optional:nacos:${spring.application.name}.yml
參數(shù)說明:
- spring.cloud.sentinel.transport.port:指定Sentinel服務(wù)Dashboard與當(dāng)前服務(wù)網(wǎng)關(guān)服務(wù)的通信端口,通過此端口Sentinel服務(wù)獲取網(wǎng)關(guān)服務(wù)的監(jiān)控?cái)?shù)據(jù)
- spring.cloud.sentinel.transport.clientIp:指定服務(wù)網(wǎng)關(guān)服務(wù)向Sentinel服務(wù)Dashboard注冊的客戶端Ip,通過此IP+端口Sentinel服務(wù)獲取網(wǎng)關(guān)服務(wù)的監(jiān)控?cái)?shù)據(jù)
- spring.cloud.sentinel.transport.dashboard:指定Sentinel服務(wù)Dashboard的IP+端口
3、啟動(dòng)ms-gateway服務(wù)
4、在前端執(zhí)行兩次登錄后
5、在Sentinel配置接口流控
在瀏覽器快速刷新http://192.168.0.100:9999/api/user/info接口文章來源:http://www.zghlxwxcb.cn/news/detail-516455.html
總結(jié):
以上就是在微服務(wù)服務(wù)網(wǎng)關(guān)上集成Sentinel后的實(shí)現(xiàn)的流控效果。文章來源地址http://www.zghlxwxcb.cn/news/detail-516455.html
到了這里,關(guān)于基于Ant DesignPro Vue + SpringBoot 前后端分離 - 后端微服化 + 接口網(wǎng)關(guān) + Nacos + Sentinel的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!