javascript
Spring Cloud 7:Gateway
Zuul 網(wǎng)關(guān)
Zuul 是 Netfilx 開源的一個 API Gateway 服務(wù)器,本質(zhì)是一個 Web Servlet 應(yīng)用。其在微服務(wù)架構(gòu)體系中提供動態(tài)路由、監(jiān)控、彈性、安全等邊緣服務(wù)。
使用 Zuul 作為網(wǎng)關(guān),其主要原因有以下幾點(diǎn):
1、Zuul、Ribbon 以及 Consul 客戶端結(jié)合使用,能夠輕松實(shí)現(xiàn)智能路由、負(fù)載均衡功能;
2、在網(wǎng)關(guān)層統(tǒng)一對外提供 API 接口,保護(hù)了實(shí)際提供接口的微服務(wù)實(shí)現(xiàn)細(xì)節(jié),同時也方便測試人員對微服務(wù)接口進(jìn)行測試;
3、在網(wǎng)關(guān)層能夠統(tǒng)一添加身份認(rèn)證、鑒權(quán)等功能,防止對微服務(wù) API 接口的非法調(diào)用;
4、在網(wǎng)關(guān)層可以方便地對訪問請求進(jìn)行記錄,實(shí)現(xiàn)監(jiān)控相關(guān)功能;
5、在網(wǎng)關(guān)層實(shí)現(xiàn)流量監(jiān)控,在流量比較大時,方便對服務(wù)實(shí)施降級。
Zuul 工作原理
Zuul 的核心是一系列的 Filters,其作用可以類比 Servlet 框架的 Filter,或者 AOP。Zuul 中定義了四種標(biāo)準(zhǔn)過濾器類型,分別是 pre、post、routing 以及 error 過濾器。
1、pre 過濾器:在請求路由到具體微服務(wù)之前執(zhí)行,其主要用于身份驗(yàn)證、鑒權(quán)等功能;
2、routing 過濾器:其主要功能是將請求路由到具體的微服務(wù)實(shí)例;
3、post 過濾器:在對具體微服務(wù)調(diào)用之后執(zhí)行,其主要用于收集統(tǒng)計(jì)信息、指標(biāo)以及對請求響應(yīng)數(shù)據(jù)進(jìn)行處理等;
4、error 過濾器:在以上三種過濾器執(zhí)行出錯時執(zhí)行。
yang-gateway
pom.xml
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><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.version>Greenwich.M3</spring-cloud.version></properties><dependencies><!--Actuator--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--Consul--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><!--Zuul--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-zuul</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement> View Codebootstrap.yml
server:port: 1003 spring:application:name: yang-gatewaycloud:consul:host: 127.0.0.1port: 8500discovery:register: truehealthCheckPath: /server/consul/healthhealthCheckInterval: 10sinstance-id: ${spring.application.name} zuul:routes:yang-service:path: /**serviceId: yang-diverApplication.java
@SpringBootApplication @EnableZuulProxy public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class, args);} }訪問路徑:http://localhost:1003/user/list
此時Gateway訪問到了yang-diver服務(wù)的內(nèi)容了。
路由配置
傳統(tǒng)的路由配置
在不依賴于服務(wù)發(fā)現(xiàn)機(jī)制的情況下,通過在配置文件中具體指定每個路由表達(dá)式與服務(wù)實(shí)例的映射關(guān)系來實(shí)現(xiàn)API網(wǎng)關(guān)對外部請求的路由。
單實(shí)例配置
通過一組zuul.routes.<route>.path與zuul.routes.<route>.url參數(shù)對的方式配置。
server:port: 1003 spring:application:name: yang-gateway zuul:routes:yang-service:path: /yang-diver/**url: http://localhost:1002/凡是路徑為:http://localhost:1003/yang-diver/**?的請求,都會轉(zhuǎn)發(fā)請求到http://localhost:1002/**?地址
多實(shí)例配置
通過一組zuul.routes.<route>.path與zuul.routes.<route>.serviceId參數(shù)對的方式配置
zuul:routes:yang-service:path: /yang-diver/**serviceId: yang-diverribbon:eureka:enabled: false # 沒有配置服務(wù)治理(Eureka)就需要關(guān)閉,否則會找不到服務(wù)yang-service:ribbon:# 為serviceId去指定具體的服務(wù)實(shí)例地址listOfServers: http://localhost:1001/,http://localhost:1002/此時,凡是路徑為:http://localhost:1003/yang-diver/**?的請求,都會轉(zhuǎn)發(fā)請求到http://localhost:1001/**?和http://localhost:1002/**?地址
服務(wù)路由配置
整合服務(wù)治理后,只需要提供一組zuul.routes.<route>.path與zuul.routes.<route>.serviceId參數(shù)對的配置即可。
server:port: 1003 spring:application:name: yang-gatewaycloud:consul:host: 127.0.0.1port: 8500discovery:register: truehealthCheckPath: /server/consul/healthhealthCheckInterval: 10sinstance-id: ${spring.application.name} zuul:routes:yang-service:path: /**serviceId: yang-diver還可以通過zuul.routes.<serviceId>=<path>,直接進(jìn)行路由轉(zhuǎn)。,其中<serviceId>用來指定路由的具體服務(wù)名,<path>用來配置匹配的請求表達(dá)式。
zuul:routes:yang-diver:path: /** # serviceId: yang-diver實(shí)際上,服務(wù)注冊中心已經(jīng)維護(hù)了serverId與實(shí)例地址的映射關(guān)系。當(dāng)Gateway注冊到服務(wù)注冊中心后,就能從注冊中心獲取所有服務(wù)以及它們的實(shí)例清單。
服務(wù)網(wǎng)關(guān)之過濾器
Spring Cloud Zuul的過濾器的作用。
以權(quán)限控制為例。每個系統(tǒng)并不會將所有的微服務(wù)接口都開放出去。為了實(shí)現(xiàn)對客戶端請求的安全校驗(yàn)和權(quán)限控制,有以下幾點(diǎn)方案:
1、為每個微服務(wù)應(yīng)用都實(shí)現(xiàn)一套用于校驗(yàn)簽名和鑒別權(quán)限的過濾器或攔截器?!緳?quán)限的實(shí)現(xiàn)方式大同小異,開發(fā)繁瑣、維護(hù)困難,不推薦】
2、實(shí)現(xiàn)鑒權(quán)服務(wù),直接在微服務(wù)應(yīng)用中通過調(diào)用鑒權(quán)服務(wù)來實(shí)現(xiàn)校驗(yàn)。【分離不徹底】
3、通過前置的網(wǎng)關(guān)服務(wù)來完成這些非業(yè)務(wù)性質(zhì)的校驗(yàn),即通過在網(wǎng)關(guān)中完成校驗(yàn)和過濾?!就扑]】
AccessFilter
``` /*** 系統(tǒng)訪問 Filter** @Author YangXuyue* @Date 2018/11/28 23:34*/ @Component("accessFilter") public class AccessFilter extends ZuulFilter {private static final Logger LOGGER = LoggerFactory.getLogger(AccessFilter.class);/*** 過濾器的類型,它決定過濾器在請求的哪個生命周期中執(zhí)行。* 這里定義為pre,代表會在請求被路由之前執(zhí)行** @return* @Author YangXuyue* @Date 2018/11/28 23:39*/@Overridepublic String filterType() {return "pre";}/*** 過濾器的執(zhí)行順序。當(dāng)請求在一個階段中存在多個過濾器時,需要根據(jù)該方法返回的值來依次執(zhí)行** @return* @Author YangXuyue* @Date 2018/11/28 23:39*/@Overridepublic int filterOrder() {return 0;}/*** 判斷該過濾器是否需要被執(zhí)行** @return* @Author YangXuyue* @Date 2018/11/28 23:39*/@Overridepublic boolean shouldFilter() {return true;}/*** 過濾器的具體邏輯* 實(shí)現(xiàn)在請求被路由之前檢查HttpServletRequest中是否有accessToken參數(shù)* 若有就進(jìn)行路由,若沒有就拒絕訪問,返回401 Unauthorized錯誤。** @return* @throws ZuulException* @Author YangXuyue* @Date 2018/11/28 23:37*/@Overridepublic Object run() throws ZuulException {RequestContext ctx = RequestContext.getCurrentContext();HttpServletRequest request = ctx.getRequest();LOGGER.info("send {} request to {}", request.getMethod(), request.getRequestURL().toString());Object accessToken = request.getParameter("accessToken");if (accessToken == null) {LOGGER.warn("access token is empty");ctx.setSendZuulResponse(false);// 未授權(quán)ctx.setResponseStatusCode(401);return null;}LOGGER.info("access token ok");return null;} }此時訪問:http://localhost:1003/user/list出現(xiàn)401未授權(quán)的問題
?
如果訪問:http://localhost:1003/user/list?accessToken=true請求就能成功被轉(zhuǎn)發(fā)。
?
轉(zhuǎn)載于:https://www.cnblogs.com/yang21/p/10030043.html
總結(jié)
以上是生活随笔為你收集整理的Spring Cloud 7:Gateway的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到怀孕肚子爆炸怎么回事
- 下一篇: [JSOI2018]潜入行动