Spring Gateway基本使用示例小結(jié)
Spring Gateway基本使用
spring gateway官方文檔:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/
Springcloud Gateway使用了Webflux中的reactor-netty響應(yīng)式編程組件,底層使用了Netty通訊框架,具有以下特性:
- 基于Spring Framework 5,Project Reactor和Spring Boot2.0進行構(gòu)建
- 動態(tài)路由,能夠匹配任何請求屬性
- 可以對路由指定Predicate(斷言)和Filter(過濾器)
- 集成Hystrix的斷路器功能
- 集成Spring Cloud服務(wù)發(fā)現(xiàn)功能
- 易于編寫的Predicate(斷言)和Filter(過濾器)
- 請求限流功能
- 支持路徑重寫
spring gateway的三個重要概念
- Route(路由)
路由時構(gòu)建網(wǎng)關(guān)的基本模塊,它由ID,目標URI,一系列的斷言和過濾器組成,如果斷言為true則匹配該路由
- Predicate(斷言)
返回布爾值,判斷匹配條件是否符合要求
- Filter(過濾)
指的是Spring框架中GatewayFilter的實例,使用過濾器,可以在請求被路由前或者之后對請求進行修改
代碼實戰(zhàn)
導(dǎo)入依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
注意:spring gateway的webflux模塊和web模塊沖突,不能導(dǎo)入以下兩個依賴,不然啟動會報錯
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
編寫配置文件
server: port: 9527 spring: application: name: gateway cloud: gateway: routes: # 路由的ID,沒有固定規(guī)則但要求唯一,建議配合服務(wù)名 - id: payment-routh1 # 匹配后提供服務(wù)的路由地址 uri: http://localhost:8001 # 斷言,路徑相匹配的進行路由 predicates: - Path=/payment/get/** - id: payment_routh2 uri: http://localhost:8001 predicates: - Path=/payment/query/** eureka: instance: hostname: gateway-9527 client: service-url: register-with-eureka: true fetch-registry: true defaultZone: http://127.0.0.1:7001/eureka
訪問:http://localhost:9527/payment/get/** 自動轉(zhuǎn)發(fā)到地址: http://localhost:8001/payment/get/**
訪問:http://localhost:9527/payment/query/** 自動轉(zhuǎn)發(fā)到地址: http://localhost:8001/payment/query/**
編碼方式配置路由
上面通過配置文件配置了路由相關(guān)設(shè)置,通過編碼方式也可以實現(xiàn)路由配置
編寫配置類
package com.yl.gateway.config; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 路由配置 * * @auther Y-wee */ @Configuration public class GateWayConfig { /** * 配置一個id為service_route的路由: * 當訪問地址http://localhost:9527/guonei時自動轉(zhuǎn)發(fā)到地址:http://news.baidu.com/guonei * 當訪問地址http://localhost:9527/guonji時自動轉(zhuǎn)發(fā)到地址:http://news.baidu.com/guonji * * @param routeLocatorBuilder * @return */ @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) { // 路由構(gòu)建對象 RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes(); // 配置路由 routes.route("service_route1", r -> r.path("/guonei") .uri("http://news.baidu.com/guonei")).build(); routes.route("service_route2", r -> r.path("/guoji") .uri("http://news.baidu.com/guoji")).build(); return routes.build(); } }
配置動態(tài)路由
默認情況下Gateway會根據(jù)注冊中心注冊的服務(wù)列表,以注冊中心上微服務(wù)名為路徑創(chuàng)建動態(tài)路由進行轉(zhuǎn)發(fā),從而實現(xiàn)動態(tài)路由的功能
修改配置文件
server: port: 9527 spring: application: name: gateway cloud: gateway: discovery: locator: # 開啟從注冊中心動態(tài)創(chuàng)建路由的功能,利用微服務(wù)名進行路由 enabled: true routes: - id: payment-routh1 # 匹配后提供服務(wù)的路由地址(lb相當于@LoadBalanced,PAYMENT是被調(diào)用的服務(wù)名) uri: lb://PAYMENT predicates: - Path=/payment/get/** - id: payment_routh2 uri: lb://PAYMENT predicates: - Path=/payment/query/** eureka: instance: hostname: gateway-9527 client: service-url: register-with-eureka: true fetch-registry: true defaultZone: http://127.0.0.1:7001/eureka
自定義全局過濾器
實現(xiàn)GlobalFilter,Ordered接口
package com.yl.gateway.filter; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import java.util.Date; /** * 自定義全局過濾器 * * @auther Y-wee */ @Component @Slf4j public class MyLogGateWayFilter implements GlobalFilter, Ordered { /** * 自定義過濾邏輯 * * @param exchange * @param chain * @return */ @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { log.info("come in MyLogGateWayFilter: " + new Date()); String uname = exchange.getRequest().getQueryParams().getFirst("uname"); if (uname == null) { log.info("用戶名為null,非法用戶,o(╥﹏╥)o"); exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); return exchange.getResponse().setComplete(); } return chain.filter(exchange); } /** * 此過濾器的優(yōu)先級順序,數(shù)值越低優(yōu)先級越高 * * @return */ @Override public int getOrder() { return 0; } }
到此這篇關(guān)于Spring Gateway基本使用的文章就介紹到這了,更多相關(guān)Spring Gateway使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談如何在項目中使用Spring Cloud Alibaba Sentinel組件
隨著微服務(wù)的流行,服務(wù)和服務(wù)之間的穩(wěn)定性變得越來越重要。本文主要介紹了使用Spring Cloud Alibaba Sentinel組件,感興趣的可以了解一下2021-07-07@ConfigurationProperties遇到的坑及解決
這篇文章主要介紹了解決@ConfigurationProperties遇到的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Springboot實現(xiàn)接口傳輸加解密的步驟詳解
這篇文章主要給大家詳細介紹了Springboot實現(xiàn)接口傳輸加解密的操作步驟,文中有詳細的圖文解釋和代碼示例供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-09-09SpringBoot加載應(yīng)用事件監(jiān)聽器代碼實例
這篇文章主要介紹了SpringBoot加載應(yīng)用事件監(jiān)聽器代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06