網關Gateway斷?+過濾器整合注冊中心Nacos項目實戰
網關Gateway斷?+過濾器整合注冊中心Nacos項目實戰
- 一、前言
- 二、網關介紹
- 三、基本網關轉發
- 1、創建Gateway項目
- 2、配置
- 四、整合注冊中心Nacos
- 1、添加Nacos依賴
- 2、啟動類開啟支持
- 3、修改配置文件
- 4、網關訪問的代碼
- 五、Gateway內置斷言實現接口定時下線與強制參數
- 六、自定義全局過濾器實現鑒權
- 七、SpringCloud+gateway+nacos整合踩過的坑
一、前言
提示:本文講到的代碼部分來自上文
上文鏈接地址
提示:以下是本篇文章正文內容,下面案例可供參考
二、網關介紹
API Gateway,是系統的唯一對外的入口,介于客戶端和服務器端之間的中間層,處理非業務功能 提供路由請求、鑒權、監控、緩存、限流等功能
- 主流的網關
- zuul:是Netflix開源的微服務網關,和Eureka,Ribbon,Hystrix等組件配合使用,依賴組件比較多,性能教差
- kong: 由Mashape公司開源的,基于Nginx的API gateway
- nginx+lua:是一個高性能的HTTP和反向代理服務器,lua是腳本語言,讓Nginx執行Lua腳本,并且高并發、非阻塞的處理各種請求
- springcloud gateway: Spring公司專門開發的網關,替代zuul
三、基本網關轉發
1、創建Gateway項目
添加依賴
<!--gateway依賴--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency>
2、配置
server:port: 8888
spring:application:name: api-gatewaycloud:gateway:routes: #數組形式- id: spring-other2 #路由唯一標識,這里自定義,一般是服務名uri: http://127.0.0.1:9000 #想要轉發到的地址order: 1 #優先級,數字越小優先級越高predicates: #斷言 配置哪個路徑才轉發,前端轉發的前綴路徑- Path=/spring-other2/**filters: #過濾器,請求在傳遞過程中通過過濾器修改- StripPrefix=1 #去掉第一層前綴(例如下面的spring-other2)#訪問路徑 http://localhost:8888/spring-other2/other2/getusername4?name=sb
#轉發路徑 http://localhost:9000/other2/getusername4?name=sb
#需要過濾器去掉前面第一層
效果圖
四、整合注冊中心Nacos
1、添加Nacos依賴
<!--添加nacos客戶端--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
2、啟動類開啟支持
@EnableDiscoveryClient
3、修改配置文件
server:port: 8888
spring:application:name: api-gatewaycloud:nacos:discovery:server-addr: 127.0.0.1:8848 #nacos地址gateway:routes: #數組形式- id: spring-other2 #路由唯一標識,這里自定義,一般是服務名# uri: http://127.0.0.1:9000 #想要轉發到的地址uri: lb://spring-other2 #想要轉發到的地址,從nacos轉發進行,后面跟服務名(lb負載均衡的意思)order: 1 #優先級,數字越小優先級越高predicates: #斷言 配置哪個路徑才轉發,前端轉發的前綴路徑- Path=/spring-other2/**filters: #過濾器,請求在傳遞過程中通過過濾器修改- StripPrefix=1 #去掉第一層前綴(例如下面的spring-other2)discovery:locator:enabled: true #開啟網關拉取nacos服務#訪問路徑 http://localhost:8888/spring-other2/other2/getusername4?name=sb
#轉發路徑 http://localhost:9000/other2/getusername4?name=sb
#需要過濾器去掉前面第一層
Gateway內置的路由斷言
predicates:- Host=- Path=- Method=- Header=- Query=- Cookie=
4、網關訪問的代碼
@RestController
@RequestMapping("other2")
public class OtherController2 {/*** 測試gateway負載* @return*/@RequestMapping("getusername8")String getusername8( HttpServletRequest request) {return "other2的getUserName7反回的數據-由"+request.getServerName()+":"+request.getServerPort()+"發起";}
}
效果圖是一種默認輪詢
五、Gateway內置斷言實現接口定時下線與強制參數
- 需求:接口需要在指定時間進行下線,過后不可以在被訪問
- 使用Before ,只要當前時間小于設定時間,路由才會匹配請求
- 東8區的2020-09-11T01:01:01.000+08:00后,請求不可訪問
predicates:- Before=2020-09-09T01:01:01.000+08:00
強制參數,如果沒有source參數則拒絕訪問
predicates:- Query=source
六、自定義全局過濾器實現鑒權
網關不要加太多業務邏輯,否則會影響性能
//實現GlobalFilter, Ordered接口
@Component
public class UserGlobalFilter implements GlobalFilter, Ordered {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {//業務邏輯//從請求中獲取tokenString token = exchange.getRequest().getHeaders().getFirst("token");System.out.println(token);//如果token為空的業務邏輯if(StringUtils.isBlank(token)){exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);return exchange.getResponse().setComplete();}//如果成功繼續往下執行return chain.filter(exchange);}//數字越小,優先級越高@Overridepublic int getOrder() {return 0;}
}
七、SpringCloud+gateway+nacos整合踩過的坑
path不能和服務名一樣,例如服務名為spring-other2則path可以叫api-spring-other2不能為spring-other2,否則routes配置無效
predicates:- Path=
本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處:https://dhexx.cn/hk/18355.html
如若內容造成侵權/違法違規/事實不符,請聯系我的編程經驗分享網進行投訴反饋,一經查實,立即刪除!