亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

spring cloud zuul 與 sentinel的結合使用操作

瀏覽:5日期:2023-07-06 16:29:48
spring cloud zuul 與 sentinel結合

本來大型服務處理請求超時,限流,降級熔斷工作用hystrix,但是這個這個項目不再更新了,雖說它現在提供的版本不會影響到大多數開發者的使用,但是長遠考慮,被更換是一件必然的事,而且現在像resilience4j, Sentinel這樣的替代品出現,今天我們就看看使用zuul 與 Sentinel整合,實現降級與超時處理,其實網上有很多這樣的教程,這里我只是做一個自己的筆記而已

1、必須的依賴

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId></dependency><dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-zuul-adapter</artifactId> <version>1.7.1</version></dependency>2、配置文件,其實Sentinel在這里沒什么配置

server: port: 6001spring: application: name: e-zuuleureka: instance: hostname: localhost lease-expiration-duration-in-seconds: 90 #表示服務端多長時間沒有接受到心跳信息后可以刪除自己 lease-renewal-interval-in-seconds: 30 #表示需要要向服務端發送信息,表示自己還活著 ip-address: true client: healthcheck: enabled: true #客戶端心跳檢測 service-url: defaultZone: http://${eureka.instance.hostname}:3001/eureka/zuul: add-proxy-headers: true LogFilter: pre: disable=true: routes: e-email: serviceId: e-email path: /email/** e-user: serviceId: e-user path: /user/**3、配置類, 其實配置類和后邊的降級回調處理類才是關鍵

而且配置類中幾個關于zuul與Sentinel的過濾器非常關鍵,這里要是不提供它們,將無法實現我們想要的功能,還有就是網關規則,可以選擇qps, 超時,線程等,setGrade(RuleConstant.DEGRADE_GRADE_RT)提供選擇不同的策略

package com.mjlf.ezuul.config;import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackManager;import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulErrorFilter;import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPostFilter;import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPreFilter;import com.alibaba.csp.sentinel.slots.block.RuleConstant;import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;import com.netflix.zuul.ZuulFilter;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct;import java.util.HashSet;import java.util.Set;@Configurationpublic class ZuulConfig { @Bean public ZuulFilter sentinelZuulPreFilter() {// We can also provider the filter order in the constructor.return new SentinelZuulPreFilter(); } @Bean public ZuulFilter sentinelZuulPostFilter() {return new SentinelZuulPostFilter(); } @Bean public ZuulFilter sentinelZuulErrorFilter() {return new SentinelZuulErrorFilter(); } @PostConstruct public void doInit() {// 注冊 FallbackProviderZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider());initGatewayRules(); } /** * 配置限流規則 */ private void initGatewayRules() {Set<GatewayFlowRule> rules = new HashSet<>();rules.add(new GatewayFlowRule('e-user').setCount(3) // 限流閾值.setIntervalSec(1) // 統計時間窗口,單位是秒,默認是 1 秒);rules.add(new GatewayFlowRule('e-user').setGrade(RuleConstant.DEGRADE_GRADE_RT)//設置超時類型規則.setMaxQueueingTimeoutMs(500));GatewayRuleManager.loadRules(rules); }}4、回調處理類,當有請求被攔截到后,就會調用降級回調方法

// 自定義 FallbackProvider@Componentpublic class MyBlockFallbackProvider implements ZuulBlockFallbackProvider { private Logger logger = LoggerFactory.getLogger(DefaultBlockFallbackProvider.class); // you can define route as service level @Override public String getRoute() {return '*'; } @Override public BlockResponse fallbackResponse(String route, Throwable cause) {RecordLog.info(String.format('[Sentinel DefaultBlockFallbackProvider] Run fallback route: %s', route));if (cause instanceof BlockException) { return new BlockResponse(429, 'Sentinel block exception', route);} else { return new BlockResponse(500, 'System Error', route);} }}zuul集成Sentinel最新的網關流控組件一、說明

Sentinel 網關流控支持針對不同的路由和自定義的 API 分組進行流控,支持針對請求屬性(如 URL 參數,Client IP,Header 等)進行流控。

Sentinel 1.6.3 引入了網關流控控制臺的支持,用戶可以直接在 Sentinel 控制臺上查看 API Gateway 實時的 route 和自定義 API 分組監控,管理網關規則和 API 分組配置。

spring cloud zuul 與 sentinel的結合使用操作

二、功能接入

1. 網關添加sentinel相關的jar依賴

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId></dependency><dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId></dependency>

2. 網關zuul的sentinel配置

spring: # sentinel動態配置規則 cloud: sentinel: zuul:enabled: trueorder: pre: 2000 post: 500 error: -100 filter:enabled: false datasource:# 限流ds1: nacos: server-addr: ${zlt.nacos.server-addr} dataId: ${spring.application.name}-sentinel-gw-flow groupId: DEFAULT_GROUP rule-type: gw-flow# api分組ds2: nacos: server-addr: ${zlt.nacos.server-addr} dataId: ${spring.application.name}-sentinel-gw-api-group groupId: DEFAULT_GROUP rule-type: gw-api-group

綁定gw-flow(限流)和gw-api-group(api分組)的規則數據源為nacos并指定nacos上對應的dataId和groupId

3. nacos規則配置

3.1. 限流配置gw-flow

spring cloud zuul 與 sentinel的結合使用操作

Data ID:api-gateway-sentinel-gw-flow

Group:DEFAULT_GROUP

配置內容:

[ { 'resource': 'user', 'count': 0, 'paramItem': { 'parseStrategy': 3, 'fieldName': 'name' } }, { 'resource': 'uaa_api', 'count': 0 }]

規則1:所有user的請求只要參數帶有name的都攔截(qps=0),user為zuul路由配置上的routeId規則2:api分組為uaa_api的所有請求都攔截(qps=0)

3.2. api分組配置gw-api-group

spring cloud zuul 與 sentinel的結合使用操作

Data ID:api-gateway-sentinel-gw-api-group

Group:DEFAULT_GROUP

配置內容:

[ { 'apiName': 'uaa_api', 'predicateItems': [ {'pattern': '/user/login' }, {'pattern': '/api-uaa/oauth/**','matchStrategy': 1 } ] }]

上面配置意思為滿足規則的api都統一分組為uaa_api分組規則1:精準匹配/user/login分組規則2:前綴匹配/api-uaa/oauth/**

4. 網關zuul啟動參數

需要在接入端原有啟動參數的基礎上添加-Dcsp.sentinel.app.type=1啟動以將您的服務標記為 API Gateway,在接入控制臺時您的服務會自動注冊為網關類型,然后您即可在控制臺配置網關規則和 API 分組,例如:

java -Dcsp.sentinel.app.type=1 -jar zuul-gateway.jar三、sentinel控制臺管理

API管理(分組)

spring cloud zuul 與 sentinel的結合使用操作

網關流控規則

spring cloud zuul 與 sentinel的結合使用操作

四、測試限流api

1. 測試限流規則1

所有user的請求只要參數帶有name的都攔截(qps=0)

不加name參數,可以訪問api

spring cloud zuul 與 sentinel的結合使用操作

后面加上name參數,請求被攔截

spring cloud zuul 與 sentinel的結合使用操作

2. 測試限流規則2

api分組為uaa_api的所有請求都攔截(qps=0)

前綴匹配/api-uaa/oauth/**

spring cloud zuul 與 sentinel的結合使用操作

精準匹配/user/login

spring cloud zuul 與 sentinel的結合使用操作

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 欧美一级高清黄图片 | 一级毛片免费的 | 九九爱www高清免费人成 | 亚洲国产第一区二区香蕉日日 | 国产精品福利视频免费观看 | 一级做a爰视频免费观看2019 | 欧美成人国产 | 美女一级牲交毛片视频 | 亚洲一区二区欧美 | 一区二区不卡视频 | 久久九九精品一区二区 | 国产香蕉在线视频 | 看真人视频a级毛片 | 91福利国产在线观看 | 欧美一区二区手机在线观看视频 | 视频在线观看黄 | 黄色片视频在线播放 | 在线500福利视频国产 | 久草在线新首页 | 亚洲最大色视频 | 麻豆网站入口 | 国产免费人做爰午夜视频 | 婷婷色网站 | 亚洲国产精品自在在线观看 | 久夜色精品国产一区二区三区 | 狠狠综合视频精品播放 | 网站免费黄| 麻豆网站在线 | 欧美一级高清片免费一级 | 福利社区在线观看 | 九九热视频在线观看 | 国产女同一区二区三区五区 | 免费在线观看中日高清生活片 | 国产日韩欧美综合 | 成人国产精品一级毛片天堂 | 国产后进白嫩翘臀美女图片 | 精品入口麻豆 | 欧美特黄a级猛片a级 | 一级黄色在线观看 | 99国产精品热久久久久久夜夜嗨 | 70岁老妇女一级毛片爽 |