限流一般可以根據(jù)客戶端IP,請求的URL,用戶登陸信息進(jìn)行限制,每秒鐘限制多次數(shù),這從別一方面也提升了系統(tǒng)的性能,無用的并發(fā)沒那么多了。
依賴包
<dependency> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency>
<groupId>com.marcosbarbero.cloud</groupId>
<artifactId>spring-cloud-zuul-ratelimit</artifactId>
<version>1.3.2.RELEASE</version> </dependency>
zuul的配置
zuul: routes: #實(shí)現(xiàn)了反向代碼和負(fù)載均衡,負(fù)載均衡采用輪詢的方式實(shí)現(xiàn) a: path: /a/** sensitiveHeaders: "*"
#不過濾任何頭信息 serviceId: servicea b: path: /b/** sensitiveHeaders: "*" serviceId:
serviceb c: path: /c/** sensitiveHeaders: "*" serviceId: servicec
add-proxy-headers: false stripPrefix: true #為true的情況下:向后端轉(zhuǎn)發(fā)之后是不會攜帶 '/a' 的。為
false 則相反
ratelimit限制配置
ratelimit: enabled: true repository: REDIS #使用redis存儲,一定要大寫! policies: a:
#針對上面那個(gè)服務(wù)的限流 limit: 100 #每秒多少個(gè)請求 refreshInterval: 60 #刷新時(shí)間窗口的時(shí)間,默認(rèn)值 (秒) type: -
ORIGIN #這里一定要大寫,類型說明:URL通過請求路徑區(qū)分,ORIGIN通過客戶端IP地址區(qū)分,USER是通過登錄用戶名進(jìn)行區(qū)分,也包括匿名用戶
這樣,服務(wù)a的限流就做好了!
熱門工具 換一換