av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

spring cloud zuul 與 sentinel的結(jié)合使用操作

瀏覽:3日期:2023-07-06 16:29:48
spring cloud zuul 與 sentinel結(jié)合

本來(lái)大型服務(wù)處理請(qǐng)求超時(shí),限流,降級(jí)熔斷工作用hystrix,但是這個(gè)這個(gè)項(xiàng)目不再更新了,雖說(shuō)它現(xiàn)在提供的版本不會(huì)影響到大多數(shù)開(kāi)發(fā)者的使用,但是長(zhǎng)遠(yuǎn)考慮,被更換是一件必然的事,而且現(xiàn)在像resilience4j, Sentinel這樣的替代品出現(xiàn),今天我們就看看使用zuul 與 Sentinel整合,實(shí)現(xiàn)降級(jí)與超時(shí)處理,其實(shí)網(wǎng)上有很多這樣的教程,這里我只是做一個(gè)自己的筆記而已

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、配置文件,其實(shí)Sentinel在這里沒(méi)什么配置

server: port: 6001spring: application: name: e-zuuleureka: instance: hostname: localhost lease-expiration-duration-in-seconds: 90 #表示服務(wù)端多長(zhǎng)時(shí)間沒(méi)有接受到心跳信息后可以刪除自己 lease-renewal-interval-in-seconds: 30 #表示需要要向服務(wù)端發(fā)送信息,表示自己還活著 ip-address: true client: healthcheck: enabled: true #客戶端心跳檢測(cè) 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、配置類, 其實(shí)配置類和后邊的降級(jí)回調(diào)處理類才是關(guān)鍵

而且配置類中幾個(gè)關(guān)于zuul與Sentinel的過(guò)濾器非常關(guān)鍵,這里要是不提供它們,將無(wú)法實(shí)現(xiàn)我們想要的功能,還有就是網(wǎng)關(guān)規(guī)則,可以選擇qps, 超時(shí),線程等,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() {// 注冊(cè) FallbackProviderZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider());initGatewayRules(); } /** * 配置限流規(guī)則 */ private void initGatewayRules() {Set<GatewayFlowRule> rules = new HashSet<>();rules.add(new GatewayFlowRule('e-user').setCount(3) // 限流閾值.setIntervalSec(1) // 統(tǒng)計(jì)時(shí)間窗口,單位是秒,默認(rèn)是 1 秒);rules.add(new GatewayFlowRule('e-user').setGrade(RuleConstant.DEGRADE_GRADE_RT)//設(shè)置超時(shí)類型規(guī)則.setMaxQueueingTimeoutMs(500));GatewayRuleManager.loadRules(rules); }}4、回調(diào)處理類,當(dāng)有請(qǐng)求被攔截到后,就會(huì)調(diào)用降級(jí)回調(diào)方法

// 自定義 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最新的網(wǎng)關(guān)流控組件一、說(shuō)明

Sentinel 網(wǎng)關(guān)流控支持針對(duì)不同的路由和自定義的 API 分組進(jìn)行流控,支持針對(duì)請(qǐng)求屬性(如 URL 參數(shù),Client IP,Header 等)進(jìn)行流控。

Sentinel 1.6.3 引入了網(wǎng)關(guān)流控控制臺(tái)的支持,用戶可以直接在 Sentinel 控制臺(tái)上查看 API Gateway 實(shí)時(shí)的 route 和自定義 API 分組監(jiān)控,管理網(wǎng)關(guān)規(guī)則和 API 分組配置。

spring cloud zuul 與 sentinel的結(jié)合使用操作

二、功能接入

1. 網(wǎng)關(guān)添加sentinel相關(guān)的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. 網(wǎng)關(guān)zuul的sentinel配置

spring: # sentinel動(dòng)態(tài)配置規(guī)則 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分組)的規(guī)則數(shù)據(jù)源為nacos并指定nacos上對(duì)應(yīng)的dataId和groupId

3. nacos規(guī)則配置

3.1. 限流配置gw-flow

spring cloud zuul 與 sentinel的結(jié)合使用操作

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

Group:DEFAULT_GROUP

配置內(nèi)容:

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

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

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

spring cloud zuul 與 sentinel的結(jié)合使用操作

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

Group:DEFAULT_GROUP

配置內(nèi)容:

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

上面配置意思為滿足規(guī)則的api都統(tǒng)一分組為uaa_api分組規(guī)則1:精準(zhǔn)匹配/user/login分組規(guī)則2:前綴匹配/api-uaa/oauth/**

4. 網(wǎng)關(guān)zuul啟動(dòng)參數(shù)

需要在接入端原有啟動(dòng)參數(shù)的基礎(chǔ)上添加-Dcsp.sentinel.app.type=1啟動(dòng)以將您的服務(wù)標(biāo)記為 API Gateway,在接入控制臺(tái)時(shí)您的服務(wù)會(huì)自動(dòng)注冊(cè)為網(wǎng)關(guān)類型,然后您即可在控制臺(tái)配置網(wǎng)關(guān)規(guī)則和 API 分組,例如:

java -Dcsp.sentinel.app.type=1 -jar zuul-gateway.jar三、sentinel控制臺(tái)管理

API管理(分組)

spring cloud zuul 與 sentinel的結(jié)合使用操作

網(wǎng)關(guān)流控規(guī)則

spring cloud zuul 與 sentinel的結(jié)合使用操作

四、測(cè)試限流api

1. 測(cè)試限流規(guī)則1

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

不加name參數(shù),可以訪問(wèn)api

spring cloud zuul 與 sentinel的結(jié)合使用操作

后面加上name參數(shù),請(qǐng)求被攔截

spring cloud zuul 與 sentinel的結(jié)合使用操作

2. 測(cè)試限流規(guī)則2

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

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

spring cloud zuul 與 sentinel的結(jié)合使用操作

精準(zhǔn)匹配/user/login

spring cloud zuul 與 sentinel的結(jié)合使用操作

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产精品伦 | 国产精品美女 | 亚洲第一黄色 | 黄色免费一级片 | 小日子的在线观看免费第8集 | 欧美精品在线观看 | 亚洲一区在线看 | 国产精品久久久久久久成人午夜 | 啪啪综合网 | 在线免费看毛片 | 九九成人 | 色婷婷一区二区三区四区 | 欧美精品第一页 | 亚洲视频网| 日本国产在线 | 欧美二三区 | 自拍偷在线精品自拍偷无码专区 | 99视频在线观看免费 | 成人免费在线视频观看 | 免费网站av | 精品视频国产 | 国产理论在线观看 | 日本在线看 | 美日韩一区二区三区 | 亚洲美女毛片 | 亚洲最大黄色网址 | 中文字幕在线视频观看 | 日韩欧美在线观看视频 | 午夜免费观看视频 | 黄色三级av | 国产福利久久 | 一级黄色片免费 | av综合网站 | 不卡在线 | 亚洲视频国产 | 午夜性影院 | 成人在线一区二区 | 91蝌蚪91九色白浆 | 18视频在线观看 | 在线看片你懂的 | 午夜天堂网 |