摘要:第篇的過(guò)濾器的使用一大致介紹我們?cè)趯W(xué)的時(shí)候,就有過(guò)濾器和攔截器的使用,而同樣也有過(guò)濾器的使用,本章節(jié)我們指在如何簡(jiǎn)單使用。是否執(zhí)行該過(guò)濾器。說(shuō)明需要過(guò)濾說(shuō)明不要過(guò)濾過(guò)濾器的具體邏輯。請(qǐng)求的添加服務(wù)網(wǎng)關(guān)微服務(wù)啟動(dòng)類(lèi)的過(guò)濾器的使用。
SpringCloud(第 021 篇)Zuul 的過(guò)濾器 ZuulFilter 的使用
-
一、大致介紹1、我們?cè)趯W(xué) Spring 的時(shí)候,就有過(guò)濾器和攔截器的使用,而 Zuul 同樣也有過(guò)濾器的使用,本章節(jié)我們指在如何簡(jiǎn)單使用 ZuulFilter。二、實(shí)現(xiàn)步驟 2.1 添加 maven 引用包
2.2 添加應(yīng)用配置文件(springms-gateway-zuul-filtersrcmainresourcesapplication.yml)4.0.0 springms-gateway-zuul-filter 1.0-SNAPSHOT jar com.springms.cloud springms-spring-cloud 1.0-SNAPSHOT org.springframework.cloud spring-cloud-starter-zuul org.springframework.cloud spring-cloud-starter-eureka
spring: application: name: springms-gateway-zuul-filter server: port: 8215 eureka: datacenter: SpringCloud # 修改 http://localhost:8761 地址 Eureka 首頁(yè)上面 System Status 的 Data center 顯示信息 environment: Test # 修改 http://localhost:8761 地址 Eureka 首頁(yè)上面 System Status 的 Environment 顯示信息 client: service-url: defaultZone: http://admin:admin@localhost:8761/eureka healthcheck: # 健康檢查 enabled: true instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} ##################################################################################################### # 打印日志 logging: level: root: INFO com.springms: DEBUG ##################################################################################################### ##################################################################################################### ribbon: ConnectTimeout: 3000 ReadTimeout: 60000 #####################################################################################################2.3 添加zuul的過(guò)濾器類(lèi)(springms-gateway-zuul-filtersrcmainjavacomspringmscloudfilterPreZuulFilter.java)
package com.springms.cloud.filter; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; /** * zuul 的過(guò)濾器。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/26 * */ public class PreZuulFilter extends ZuulFilter{ private static final Logger Logger = LoggerFactory.getLogger(PreZuulFilter.class); /** * 前置過(guò)濾器。 * * 但是在 zuul 中定義了四種不同生命周期的過(guò)濾器類(lèi)型: * * 1、pre:可以在請(qǐng)求被路由之前調(diào)用; * * 2、route:在路由請(qǐng)求時(shí)候被調(diào)用; * * 3、post:在route和error過(guò)濾器之后被調(diào)用; * * 4、error:處理請(qǐng)求時(shí)發(fā)生錯(cuò)誤時(shí)被調(diào)用; * * @return */ @Override public String filterType() { return "pre"; } /** * 過(guò)濾的優(yōu)先級(jí),數(shù)字越大,優(yōu)先級(jí)越低。 * * @return */ @Override public int filterOrder() { return 1; } /** * 是否執(zhí)行該過(guò)濾器。 * * true:說(shuō)明需要過(guò)濾; * * false:說(shuō)明不要過(guò)濾; * * @return */ @Override public boolean shouldFilter() { return false; } /** * 過(guò)濾器的具體邏輯。 * * @return */ @Override public Object run() { HttpServletRequest request = RequestContext.getCurrentContext().getRequest(); String host = request.getRemoteHost(); Logger.info("<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); Logger.info("<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); Logger.info("<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); Logger.info(" 請(qǐng)求的host:{} ", host); Logger.info("<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); Logger.info("<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); Logger.info("<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); return null; } }2.4 添加zuul服務(wù)網(wǎng)關(guān)微服務(wù)啟動(dòng)類(lèi)(springms-gateway-zuul-filtersrcmainjavacomspringmscloudMsGatewayZuulFilterApplication.java)
package com.springms.cloud; import com.springms.cloud.filter.PreZuulFilter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.Bean; /** * Zuul 的過(guò)濾器 ZuulFilter 的使用。 * * 注意 EnableZuulProxy 注解能注冊(cè)到 eureka 服務(wù)上,是因?yàn)樵撟⒔獍?eureka 客戶(hù)端的注解,該 EnableZuulProxy 是一個(gè)復(fù)合注解。 * * @EnableZuulProxy --> { @EnableCircuitBreaker、@EnableDiscoveryClient } 包含了 eureka 客戶(hù)端注解,同時(shí)也包含了 Hystrix 斷路器模塊注解。 * * http://localhost:8150/routes 地址可以查看該zuul微服務(wù)網(wǎng)關(guān)代理了多少微服務(wù)的serviceId。 * * 想看更多關(guān)于過(guò)濾器的使用的話(huà),請(qǐng)移步源碼路徑:spring-cloud-netflix-core-1.2.7.RELEASE.jar中的org.springframework.cloud.netflix.zuul.filters目錄下; * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/26 * */ @SpringBootApplication @EnableZuulProxy public class MsGatewayZuulFilterApplication { public static void main(String[] args) { SpringApplication.run(MsGatewayZuulFilterApplication.class, args); System.out.println("【【【【【【 GatewayZuulFilter微服務(wù) 】】】】】】已啟動(dòng)."); } /** * 即使其它配置都寫(xiě)好的話(huà),那么不添加這個(gè) Bean 的方法的話(huà),還是不會(huì)執(zhí)行任何過(guò)濾的方法; * * @return */ @Bean public PreZuulFilter preZuulFilter() { return new PreZuulFilter(); } }三、測(cè)試
/**************************************************************************************** 一、Zuul 的過(guò)濾器 ZuulFilter 的使用(正常情況測(cè)試): 1、編寫(xiě) application.yml 文件,添加應(yīng)用程序的注解 EnableZuulProxy 配置; 2、修改 PreZuulFilter 的 shouldFilter 方法返回 true 即可,表明要使用過(guò)濾功能; 3、啟動(dòng) springms-discovery-eureka 模塊服務(wù),啟動(dòng)1個(gè)端口; 4、啟動(dòng) springms-provider-user 模塊服務(wù),啟動(dòng)1個(gè)端口(application.yml 文件中的 appname 屬性不去掉的話(huà),測(cè)試一是無(wú)法測(cè)試通過(guò)的); 5、啟動(dòng) springms-gateway-zuul-filter 模塊服務(wù); 6、新起網(wǎng)頁(yè)頁(yè)簽,輸入 http://localhost:8215/routes 正常情況下是能看到zuul需要代理的各個(gè)服務(wù)列表; 7、新起網(wǎng)頁(yè)頁(yè)簽,然后輸入 http://localhost:8215/springms-provider-user/simple/1 正常情況下是能看到 ID != 0 一堆用戶(hù)信息被打印出來(lái),并且該zuul的微服務(wù)日志控制臺(tái)會(huì)打印一堆 PreZuulFilter 打印的日志內(nèi)容; 8、然后會(huì)看到 PreZuulFilter.run 方法中的日志被打印出來(lái),說(shuō)名確實(shí)進(jìn)入了過(guò)濾的方法里面,過(guò)濾起作用了; 總結(jié):過(guò)濾確實(shí)起了作用,那是因?yàn)檫^(guò)濾器的配置中 shouldFilter 設(shè)置的 true,需要過(guò)濾,所以當(dāng)然會(huì)過(guò)濾啦,直接 run 方法中的打印信息即可; ****************************************************************************************/ /**************************************************************************************** 二、Zuul 的過(guò)濾器 ZuulFilter 的使用(使用過(guò)濾器,但是使得過(guò)濾的run方法失效): 1、編寫(xiě) application.yml 文件,添加應(yīng)用程序的注解 EnableZuulProxy 配置; 2、修改 PreZuulFilter 的 shouldFilter 方法返回 false 即可,表明不需要使用過(guò)濾器; 3、啟動(dòng) springms-discovery-eureka 模塊服務(wù),啟動(dòng)1個(gè)端口; 4、啟動(dòng) springms-provider-user 模塊服務(wù),啟動(dòng)1個(gè)端口(application.yml 文件中的 appname 屬性不去掉的話(huà),測(cè)試一是無(wú)法測(cè)試通過(guò)的); 5、啟動(dòng) springms-gateway-zuul-filter 模塊服務(wù); 6、新起網(wǎng)頁(yè)頁(yè)簽,輸入 http://localhost:8215/routes 正常情況下是能看到zuul需要代理的各個(gè)服務(wù)列表; 7、新起網(wǎng)頁(yè)頁(yè)簽,然后輸入 http://localhost:8215/springms-provider-user/simple/1 正常情況下是能看到 ID != 0 一堆用戶(hù)信息被打印出來(lái),但是該zuul的微服務(wù)日志控制臺(tái)并不會(huì)打印一堆 PreZuulFilter 打印的日志內(nèi)容; 8、然后再看,PreZuulFilter.run 方法中的日志不見(jiàn)了,沒(méi)有被打印出來(lái),過(guò)濾的run方法失效了; 總結(jié):由此可見(jiàn),PreZuulFilter 的 shouldFilter 設(shè)置為 false,過(guò)濾器就已經(jīng)失去效果了; ****************************************************************************************/四、下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接
歡迎關(guān)注,您的肯定是對(duì)我最大的支持!!!
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/70595.html
摘要:但是如果將負(fù)載均衡器置于所有服務(wù)前便不是一個(gè)好主意,會(huì)造成瓶頸。服務(wù)超時(shí)使用的和庫(kù)來(lái)進(jìn)行請(qǐng)求。支持以下四種過(guò)濾器前置過(guò)濾器在將請(qǐng)求發(fā)送到目的地之前被調(diào)用。通常用于記錄從目標(biāo)服務(wù)返回的響應(yīng)處理錯(cuò)誤或?qū)徍嗣舾行畔ⅰ? showImg(https://segmentfault.com/img/remote/1460000019531578); springcloud 總集:https://ww...
摘要:服務(wù)網(wǎng)關(guān)的要素穩(wěn)定性安全性性能,并發(fā)性擴(kuò)展性路由過(guò)濾器核心是一系列的過(guò)濾器路由配置權(quán)限設(shè)置這個(gè)名稱(chēng)可以隨便填敏感頭過(guò)濾簡(jiǎn)潔寫(xiě)法不對(duì)外部訪問(wèn)代表集合查看所有的路由規(guī)則配置的動(dòng)態(tài)注入也可以寫(xiě)入啟動(dòng)類(lèi)中典型應(yīng)用場(chǎng)景前置過(guò)濾器限流鑒權(quán) 服務(wù)網(wǎng)關(guān)的要素 穩(wěn)定性 安全性 性能,并發(fā)性 擴(kuò)展性Spring Cloud Zuul - 路由+過(guò)濾器 - 核心是一系列的過(guò)濾器 showIm...
摘要:洞察和監(jiān)控在邊緣跟蹤有意義的數(shù)據(jù)和統(tǒng)計(jì)數(shù)據(jù),以便為我們提供準(zhǔn)確的生產(chǎn)視圖。壓力測(cè)試逐步增加集群的流量,以評(píng)估性能。減少負(fù)載為每種類(lèi)型的請(qǐng)求分配容量,并刪除超過(guò)限制的請(qǐng)求。在路由到源之前執(zhí)行,可以用于身份驗(yàn)證路由和裝飾請(qǐng)求。 showImg(https://segmentfault.com/img/remote/1460000018826272); 簡(jiǎn)介 Zuul是所有從設(shè)備和web站點(diǎn)...
摘要:網(wǎng)關(guān)以及業(yè)務(wù)組件將對(duì)象存儲(chǔ)到或從獲取對(duì)象。二存在問(wèn)題基于如上微服務(wù)的分布式架構(gòu)如果按照傳統(tǒng)方式,將對(duì)象存儲(chǔ)在內(nèi)存中。微服務(wù)架構(gòu)下共享對(duì)象實(shí)現(xiàn)說(shuō)明客戶(hù)端請(qǐng)求到,基于管理將對(duì)象存儲(chǔ)到,并將生成的返回給客戶(hù)端。 一.簡(jiǎn)單做一個(gè)背景說(shuō)明1.為說(shuō)明問(wèn)題,本文簡(jiǎn)單微服務(wù)架構(gòu)示例如下 showImg(https://segmentfault.com/img/bV7U8h?w=818&h=445); ...
閱讀 3783·2021-10-18 13:34
閱讀 2493·2021-08-11 11:15
閱讀 1304·2019-08-30 15:44
閱讀 784·2019-08-26 10:32
閱讀 1054·2019-08-26 10:13
閱讀 2132·2019-08-23 18:36
閱讀 1843·2019-08-23 18:35
閱讀 591·2019-08-23 17:10