当前位置: 首页 > news >正文

网站上的图片多大合适网站建设|

网站上的图片多大合适,网站建设|,wordpress 最新 热门 随机 切换,产品seo怎么优化Spring Cloud(Kilburn 2022.0.2版本)系列教程(五) 服务网关(SpringCloud Gateway) 一、服务网关 1.1 什么是网关 在微服务架构中#xff0c;服务网关是一个至关重要的组件。它作为系统的入口#xff0c;负责接收客户端的请求#xff0c;并将这些请求路由到相应的后端服务…Spring Cloud(Kilburn 2022.0.2版本)系列教程(五) 服务网关(SpringCloud Gateway) 一、服务网关 1.1 什么是网关 在微服务架构中服务网关是一个至关重要的组件。它作为系统的入口负责接收客户端的请求并将这些请求路由到相应的后端服务。API网关是一个搭建在客户端和微服务之间的服务可以在API网关中处理一些非业务功能的逻辑例如权限验证、监控、缓存、请求路由等。API网关就像整个微服务系统的门面一样是系统对外的唯一入口。有了它客户端会先将请求发送到API网关由API网关根据请求的标识信息将请求转发到微服务实例。Spring Cloud Gateway是Spring Cloud提供的一个强大的服务网关解决方案。 Spring Cloud Gateway 底层使用了高性能的通信框架Netty。Netty 是高性能中间件的通讯底座 rocketmq 、seata、nacos 、sentinel、redission 、dubbo 等太多、太多的的大名鼎鼎的中间件无一例外都是基于netty。 与 Spring Cloud Netflix Zuul 相比Spring Cloud Gateway具有更好的性能和更丰富的功能。例如Gateway 基于WebFlux构建支持响应式编程能够更好地处理高并发场景。同时它的路由和过滤配置更加灵活和简洁。Spring Cloud Gateway替换掉Zuul的成本上是非常低的几乎可以无缝切换。Spring Cloud Gateway几乎包含了Zuul的所有功能。随着时间的发展Spring Cloud Netflix Zuul会被Spring Cloud Gateway取代想学点新技术的小伙伴就去玩转Spring Cloud Gateway吧别再死死抓住Zuul不放了。 1.2 网关的作用 作用说明路由接口服务的统一代理实现前端对接口服务的统一访问。过滤对用户请求进行拦截、过滤、用户鉴权、监控等。限流限制用户的访问流量。 1.3 常用的网关 网关名称优点缺点Spring Cloud Gateway1. 基于 Spring 框架与 Spring 生态系统集成方便如和 Spring Boot、Spring Cloud 等配合良好。 2. 支持多种路由匹配方式包括路径、请求头、请求参数等灵活性高。 3. 可以方便地通过 Java 代码实现自定义的过滤器用于实现诸如安全认证、日志记录、请求修改等功能。 4. 具备较好的性能能处理高并发请求。1. 对于不熟悉 Spring 框架的开发者有一定的学习成本。 2. 配置相对复杂尤其是大规模复杂场景下路由和过滤器的配置可能变得繁琐。Spring Cloud Netflix Zuul1. 是 Spring Cloud 早期的网关解决方案和 Spring Cloud 生态融合度较好。 2. 支持动态路由、监控、弹性和安全等功能能够方便地对请求进行路由和过滤。 3. 可以通过编写 Zuul Filter 来扩展功能。1. 相比一些新兴的网关性能在高并发场景下可能稍逊一筹。 2. 新功能更新速度相对较慢社区活跃度不如一些其他网关。Kong1. 开源的、轻量级的高性能 API 网关基于 Nginx 和 OpenResty 构建。 2. 支持插件机制可以轻松扩展功能如身份验证、限流、日志记录等。 3. 可用于微服务架构中对前后端分离的应用进行有效的请求转发和管理。 4. 提供了良好的可扩展性和分布式部署能力。1. 文档在某些方面可能不够详细对于初次使用者有一定的学习门槛。 2. 复杂的插件配置可能会影响性能如果插件使用不当可能出现一些难以排查的问题。Nginx Lua1. Nginx 本身性能卓越处理高并发能力强可以高效地处理大量的网络请求。 2. Lua 脚本可以方便地嵌入到 Nginx 中实现自定义的业务逻辑如动态路由、请求过滤等。 3. 可以灵活地进行配置和优化适用于各种规模的网络架构。1. 开发和维护需要对 Nginx 和 Lua 都有深入的了解技术门槛较高。 2. Lua 脚本编写不当可能会导致内存泄漏等问题影响系统的稳定性。Traefik1. 自动发现后端服务支持多种容器编排工具(如 Docker、Kubernetes 等)配置简单。 2. 具有动态配置更新能力无需重启即可应用新的配置。 3. 提供了简洁直观的 Web UI 用于监控和管理。 4. 性能较好能有效处理大规模流量。1. 一些高级功能可能需要额外的配置和学习对复杂场景的支持可能需要更多探索。 2. 在处理某些特殊的业务逻辑定制方面可能不如一些基于编程实现的网关灵活。 SpringCloud GateWay的主要功能之一是转发请求转发规则的定义主要包含三大核心概念 术语功能Route(路由)路由是网关的基本单元由ID、URI、一组Predicate、一组Filter组成根据Predicate进行匹配转发。Predicate(谓语、断言)路由转发的判断条件目前SpringCloud Gateway支持多种方式常见如Path、Query、Method、Header等写法必须遵循 keyvlue的形式。Filter(过滤器)过滤器是路由转发请求时所经过的过滤逻辑可用于修改请求、响应内容。 二、SpringCloud Gateway路由 复制eurekaClient工程修改名称为hepGatewayEntrance。 修改pom.xml文件 artifactIdhepGatewayEntrance/artifactId namehepGatewayEntrance/name descriptionhepGatewayEntrance/description引入以下依赖 dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway/artifactId /dependency修改系统的hostsWindows10的hosts文件路径为C:\Windows\System32\drivers\etc\hosts。Linux和macOS的文件路径为/etc/hosts。增加一行127.0.0.1 hepGatewayEntrance。 2.1 基础URI路由配置 修改application.yml文件为 server:port: 8005 # 端口号spring:main:web-application-type: reactivecloud:gateway:discovery:locator:enabled: trueroutes:- id: hep-api-userServiceuri: http://localhost:8000predicates:- Path/**application:name: hepGatewayEntrance #Gateway名称eureka:instance:prefer-ip-address: falsehostname: hepGatewayEntrancelease-renewal-interval-in-seconds: 5lease-expiration-duration-in-seconds: 10client:healthcheck:enabled: truefetch-registry: trueregister-with-eureka: trueservice-url:defaultZone: http://eureka:eurekapwdeurekaServer:8761/eureka/ registry-fetch-interval-seconds: 5Spring Cloud Gateway配置各字段含义如下 id自定义的路由 ID保持唯一 uri目标服务地址 Predicate断言 这是一个Java 8 Function Predicate。输入类型是Spring Framework ServerWebExchange。允许开发人员匹配来自HTTP请求的任何内容例如Header或参数。Predicate接受一个输入参数返回一个布尔值结果。Spring Cloud Gateway内置了许多Predicate这些Predicate的源码在org.springframework.cloud.gateway.handler.predicate包中。 上面这段配置的意思是配置了一个 id 为 hep-api-userService的URI代理规则路由的规则为当访问地址http://localhost:8005/getUserById时会路由到上游地址http://localhost:8000/getUserById。 常见的 Predicate 断言如下 断言示例说明Path- Path/user/**当请求路径与 /user/** 匹配时该请求才能被转发到 http://localhost:8000/ 上。Before- Before2024-10-20T11:47:34.25508:00[Asia/Shanghai]在 2024 年 10 月 20 日 11 时 47 分 34.255 秒之前的请求才会被转发到 http://localhost:8000/ 上。After- After2024-10-20T11:47:34.25508:00[Asia/Shanghai]在 2024 年 10 月 20 日 11 时 47 分 34.255 秒之后的请求才会被转发到http://localhost:8000/ 上。Between- Between2024-10-20T15:18:33.22608:00[Asia/Shanghai],2024-10-20T15:23:33.22608:00[Asia/Shanghai]在 2024 年 10 月 20 日 15 时 18 分 33.226 秒 到 2024年 10 月 20 日 15 时 23 分 33.226 秒之间的请求才会被转发到http://localhost:8000/服务器上。Cookie- Cookiename,c.biancheng.net携带 Cookie 且 Cookie 的内容为 namec.biancheng.net 的请求才会被转发到http://localhost:8000/上。Header- HeaderX-Request-Id,\d请求头上携带属性 X-Request-Id 且属性值为整数的请求才会被转发到http://localhost:8000/上。Method- MethodGET只有 GET 请求才会被转发到http://localhost:8000/上。 当然我们也可以把EurekaClient的接口修改一下使其前面增加一个/user/的路径 RestController RequestMapping(/user) public class UserController {RequestMapping(/getUserById)public String getUserById() throws Exception{return {id:1,name:jason,age:23,sex: man};} }在Spring Cloud Gateway中也设置一下/user/路径Path/user/** spring:main:web-application-type: reactivecloud:gateway:discovery:locator:enabled: trueroutes:- id: hep-api-userServiceuri: http://localhost:8000predicates:- Path/user/**当访问地址http://localhost:8005/user/getUserById时会路由到上游地址http://localhost:8000/user/getUserById。 2.2 动态路由配置 Spring Cloud Gateway 会根据Eureka Server服务注册中心中维护的服务列表以服务名(spring.application.name)作为路径创建动态路由进行转发从而实现动态路由功能。 我们把Spring Cloud Gateway的uri修改为uri: lb://EUREKACLIENT其中lb标识负载均衡为uri的协议表示开启 Spring Cloud Gateway 的负载均衡功能EUREKACLIENT是服务的名称注意一般是大写。 spring:main:web-application-type: reactivecloud:gateway:discovery:locator:enabled: trueroutes:- id: hep-api-userServiceuri: lb://EUREKACLIENTpredicates:- Path/user/**三、SpringCloud Gateway 过滤器 3.1 GatewayFilter 网关过滤器 我们来使用一下PrefixPath GatewayFilter吧当我们增加一项PrefixPath/user并把Path中的/user/去掉改成Path/** spring:main:web-application-type: reactivecloud:gateway:discovery:locator:enabled: trueroutes:- id: hep-api-userServiceuri: lb://EUREKACLIENTpredicates:- Path/**filters:- PrefixPath/user所有匹配该路由的请求路径都会被添加/user前缀。例如一个原本请求/getUserById的请求经过该过滤器后实际请求路径会变为/user/getUserById然后被转发到http://localhost:8000/user/getUserById。 Spring Cloud Gateway 内置了多达 31 种 GatewayFilter下表中列举了几种常用的网关过滤器及其使用示例。 路由过滤器描述参数使用示例AddRequestHeader拦截传入的请求并在请求上添加一个指定的请求头参数。name需要添加的请求头参数的 key value需要添加的请求头参数的 value。- AddRequestHeadermy-request-header,1024AddRequestParameter拦截传入的请求并在请求上添加一个指定的请求参数。name需要添加的请求参数的 key value需要添加的请求参数的 value。- AddRequestParametermy-request-param,c.biancheng.netAddResponseHeader拦截响应并在响应上添加一个指定的响应头参数。name需要添加的响应头的 key value需要添加的响应头的 value。- AddResponseHeadermy-response-header,c.biancheng.netPrefixPath拦截传入的请求并在请求路径增加一个指定的前缀。prefix需要增加的路径前缀。- PrefixPath/consumerPreserveHostHeader转发请求时保持客户端的 Host 信息不变然后将它传递到提供具体服务的微服务中。无- PreserveHostHeaderRemoveRequestHeader移除请求头中指定的参数。name需要移除的请求头的 key。- RemoveRequestHeadermy-request-headerRemoveResponseHeader移除响应头中指定的参数。name需要移除的响应头。- RemoveResponseHeadermy-response-headerRemoveRequestParameter移除指定的请求参数。name需要移除的请求参数。- RemoveRequestParametermy-request-paramRequestSize配置请求体的大小当请求体过大时将会返回 413 Payload Too Large。maxSize请求体的大小。- name: RequestSize args: maxSize: 5000000 31 种 GatewayFilter列表 1. AddRequestHeader 在请求头中添加参数 2. AddRequestParameter 添加请求参数 3. AddResponseHeader 4. The DedupeResponseHeader GatewayFilter Factory 5. The Hystrix GatewayFilter Factory 6. Spring Cloud CircuitBreaker GatewayFilter Factory 7. The FallbackHeaders GatewayFilter Factory 8. The MapRequestHeader GatewayFilter Factory 9. The PrefixPath GatewayFilter Factory 10. The PreserveHostHeader GatewayFilter Factory 11. The RequestRateLimiter GatewayFilter Factory 12. The RedirectTo GatewayFilter Factory 13. The RemoveRequestHeader GatewayFilter Factory 14. RemoveResponseHeader GatewayFilter Factory 15. The RemoveRequestParameter GatewayFilter Factory 16. The RewritePath GatewayFilter Factory 17. RewriteLocationResponseHeader GatewayFilter Factory 18. The RewriteResponseHeader GatewayFilter Factory 19. The SaveSession GatewayFilter Factory 20. The SecureHeaders GatewayFilter Factory 21. The SetPath GatewayFilter Factory 22. The SetRequestHeader GatewayFilter Factory 23. The SetResponseHeader GatewayFilter Factory 24. The SetStatus GatewayFilter Factory 25. The StripPrefix GatewayFilter Factory 26. The Retry GatewayFilter Factory 27. The RequestSize GatewayFilter Factory 28. The SetRequestHostHeader GatewayFilter Factory 29. Modify a Request Body GatewayFilter Factory 30. Modify a Response Body GatewayFilter Factory 31. Default Filters3.2 GlobalFilter 全局过滤器 GlobalFilter 是一种作用于所有的路由上的全局过滤器通过它我们可以实现一些统一化的业务功能例如权限认证、IP 访问限制等。当某个请求被路由匹配时那么所有的 GlobalFilter 会和该路由自身配置的 GatewayFilter 组合成一个过滤器链。 GlobalFilter 全局过滤器有Gateway Metrics Filter、Local Response Cache Filter、Forward Routing Filter、Netty Routing Filter、Netty Write Response Filter、ReactiveLoadBalancerClientFilter、RouteToRequestUrl Filter、Websocket Routing Filter等。 3.3 GlobalFilter和GatewayFilter的区别 FilteringWebHandler在处理请求时会将GlobalFilter装饰为GatewayFilter然后放到同一个过滤器链中排序以后依次执行。 Spring Cloud Gateway 根据作用范围划分为 GatewayFilter 和 GlobalFilter二者区别如下 GatewayFilter网关过滤器需要通过 spring.cloud.routes.filters 配置在具体路由下只作用在当前路由上或通过 spring.cloud.default-filters 配置在全局作用在所有路由上。 GlobalFilter全局过滤器不需要在配置文件中配置作用在所有的路由上最终通过 GatewayFilterAdapter 包装成 GatewayFilterChain 可识别的过滤器它为请求业务以及路由的 URI 转换为真实业务服务请求地址的核心过滤器不需要配置系统初始化时加载并作用在每个路由上。 四、自定义过滤器 无论是GatewayFilter还是GlobalFilter都支持自定义只不过编码方式、使用方式略有差别。 4.1 自定义GatewayFilter 自定义GatewayFilter不是直接实现GatewayFilter而是实现AbstractGatewayFilterFactory。最简单的方式是这样的 Component public class PrintLogGatewayFilterFactory extends AbstractGatewayFilterFactoryObject {Overridepublic GatewayFilter apply(Object config) {return new GatewayFilter() {Overridepublic MonoVoid filter(ServerWebExchange exchange, GatewayFilterChain chain) {// 获取请求ServerHttpRequest request exchange.getRequest();// 编写过滤器逻辑System.out.println(PrintLog executed.);// 放行return chain.filter(exchange);}};} }注意该类的名称一定要以GatewayFilterFactory为后缀哈。 然后在yaml配置中这样使用 spring:cloud:gateway:default-filters:- PrintLog # 此处直接以自定义的GatewayFilterFactory类名称前缀类声明过滤器另外这种过滤器还可以支持动态配置参数不过实现起来比较复杂示例 Component public class PrintLogGatewayFilterFactory // 父类泛型是内部类的Config类型extends AbstractGatewayFilterFactoryPrintAnyGatewayFilterFactory.Config {Overridepublic GatewayFilter apply(Config config) {// OrderedGatewayFilter是GatewayFilter的子类包含两个参数// - GatewayFilter过滤器// - int order值值越小过滤器执行优先级越高return new OrderedGatewayFilter(new GatewayFilter() {Overridepublic MonoVoid filter(ServerWebExchange exchange, GatewayFilterChain chain) {// 获取config值String a config.getA();String b config.getB();String c config.getC();// 编写过滤器逻辑System.out.println(a a);System.out.println(b b);System.out.println(c c);// 放行return chain.filter(exchange);}}, 100);}// 自定义配置属性成员变量名称很重要下面会用到Datastatic class Config{private String a;private String b;private String c;}// 将变量名称依次返回顺序很重要将来读取参数时需要按顺序获取Overridepublic ListString shortcutFieldOrder() {return List.of(a, b, c);}// 返回当前配置类的类型也就是内部的ConfigOverridepublic ClassConfig getConfigClass() {return Config.class;}}然后在yaml文件中使用 spring:cloud:gateway:default-filters:- PrintAny1,2,3 # 注意这里多个参数以,隔开将来会按照shortcutFieldOrder()方法返回的参数顺序依次复制上面这种配置方式参数必须严格按照shortcutFieldOrder()方法的返回参数名顺序来赋值。 还有一种用法无需按照这个顺序就是手动指定参数名 spring:cloud:gateway:default-filters:- name: PrintAnyargs: # 手动指定参数名无需按照参数顺序a: 1b: 2c: 34.2 自定义GlobalFilter 自定义GlobalFilter则简单很多直接实现GlobalFilter即可而且也无法设置动态参数 Component public class PrintLogGlobalFilter implements GlobalFilter, Ordered {Overridepublic MonoVoid filter(ServerWebExchange exchange, GatewayFilterChain chain) {// 编写过滤器逻辑System.out.println(未登录无法访问);// 放行// return chain.filter(exchange);// 拦截ServerHttpResponse response exchange.getResponse();response.setRawStatusCode(401);//设置401无法访问return response.setComplete();}Overridepublic int getOrder() {// 过滤器执行顺序值越小优先级越高return 0;} }五、SpringCloud Gateway 限流 Gateway是基于令牌桶算法使用redis作为“桶”结合过滤器实现了网关限流。 在pom.xml文件中增加依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis-reactive/artifactId /dependency5.1 IP限流 为了简便一点我们就不单独创建一个Config的java类了直接在启动类中创建一个ipKeyResolver并指定Bean(name ipKeyResolver)。 package cc.huerpu.eurekaclient;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import java.util.Objects;SpringBootApplication EnableDiscoveryClient public class HepGatewayEntranceApplication {public static void main(String[] args) {SpringApplication.run(HepGatewayEntranceApplication.class, args);}Bean(name ipKeyResolver)public KeyResolver ipKeyResolver() {return new KeyResolver() {Overridepublic MonoString resolve(ServerWebExchange exchange) {String hostName Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getHostName();System.out.println(hostName: hostName);return Mono.just(hostName);}};}}使用 RequestRateLimiter 过滤器可以实现限流功能。首先需要在配置文件中配置限流规则。在application.yml文件中指定过滤器为RequestRateLimiterkey-resolver用于限流的键的解析器的Bean对象的名字。它使用 SpEL 表达式根据#{ipKeyResolver}从 Spring 容器中获取Bean对象redis-rate-limiter.replenishRate指定令牌桶每s的填充速度redis-rate-limiter.burstCapacity指定令牌桶容量redis-rate-limiter.requestedTokens指定每个请求消耗多少个令牌当然还要配置好redis相关信息。 server:port: 8005 # 端口号spring:main:web-application-type: reactivecloud:redis:host: 127.0.0.1port: 6379password: database: 0gateway:filter:request-rate-limiter:deny-empty-key: true #如果keyResolver返回空key则拒绝该请求403默认true表示拒绝false则表示允许访问discovery:locator:enabled: true #启用基于服务发现的路由routes:- id: hep-api-userServiceuri: lb://EUREKACLIENTpredicates:- Path/**filters:- name: RequestRateLimiterargs:key-resolver: #{ipKeyResolver}redis-rate-limiter.replenishRate: 1 #令牌桶每s的填充速度redis-rate-limiter.burstCapacity: 2 #令牌桶容量redis-rate-limiter.requestedTokens: 1 #每个请求消耗多少个令牌- PrefixPath/userapplication:name: hepGatewayEntrance # Gateway名称当限流的时候会返回429 Too Many Request而正当访问会返回正确的结果数据。 我们使用Postman进行了100次的连续请求可以看到有成功的也有429 Too Many Request的这个限流的表象就跃然纸上了。当我们去Redis服务器查看的时候也可以看到配置的db0数据库中有限流的相关数据信息。 5.2 用户限流 根据用户来做限流需要获取当前请求用户的userId我们在启动类中创建一个userIdResolver并指定Bean(name userIdResolver)。 package cc.huerpu.eurekaclient;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono;import java.util.Objects;SpringBootApplication EnableDiscoveryClient public class HepGatewayEntranceApplication {public static void main(String[] args) {SpringApplication.run(HepGatewayEntranceApplication.class, args);}Bean(name userIdResolver)public KeyResolver userKeyResolver() {return exchange - Mono.just(Objects.requireNonNull(exchange.getRequest().getQueryParams().getFirst(userId)));}}在application.yml文件中指定key-resolver: #{userIdResolver}。 server:port: 8005 # 端口号spring:main:web-application-type: reactivecloud:redis:host: 127.0.0.1port: 6379password: database: 0gateway:filter:request-rate-limiter:deny-empty-key: true #如果keyResolver返回空key则拒绝该请求403默认true表示拒绝false则表示允许访问discovery:locator:enabled: true #启用基于服务发现的路由routes:- id: hep-api-userServiceuri: lb://EUREKACLIENTpredicates:- Path/**filters:- name: RequestRateLimiterargs:key-resolver: #{userIdResolver}redis-rate-limiter.replenishRate: 1 #令牌桶每s的填充速度redis-rate-limiter.burstCapacity: 2 #令牌桶容量redis-rate-limiter.requestedTokens: 1 #每个请求消耗多少个令牌- PrefixPath/userapplication:name: hepGatewayEntrance # Gateway名称我们在请求参数中增加userId参数并请求http://localhost:8005/getUserById?userId1111可以看到当限流的时候会返回429 Too Many Request而正当访问会返回正确的结果数据。 5.3 接口限流 接口限流获取请求地址的uri作为限流的key我们指定一个名为Bean(name apiKeyResolver)的Bean。 package cc.huerpu.eurekaclient;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono;import java.util.Objects;SpringBootApplication EnableDiscoveryClient public class HepGatewayEntranceApplication {public static void main(String[] args) {SpringApplication.run(HepGatewayEntranceApplication.class, args);}Bean(name apiKeyResolver)public KeyResolver apiKeyResolver() {return exchange - Mono.just(exchange.getRequest().getPath().value());}}在application.yml文件中指定key-resolver: #{apiKeyResolver}。 server:port: 8005 # 端口号spring:main:web-application-type: reactivecloud:redis:host: 127.0.0.1port: 6379password: database: 0gateway:filter:request-rate-limiter:deny-empty-key: true #如果keyResolver返回空key则拒绝该请求403默认true表示拒绝false则表示允许访问discovery:locator:enabled: true #启用基于服务发现的路由routes:- id: hep-api-userServiceuri: lb://EUREKACLIENTpredicates:- Path/**filters:- name: RequestRateLimiterargs:key-resolver: #{apiKeyResolver}redis-rate-limiter.replenishRate: 1 #令牌桶每s的填充速度redis-rate-limiter.burstCapacity: 2 #令牌桶容量redis-rate-limiter.requestedTokens: 1 #每个请求消耗多少个令牌- PrefixPath/userapplication:name: hepGatewayEntrance # Gateway名称很简单的这里就不截图了哈。 Reference 代码共享地址https://github.com/jinglisen/SpringCloud-Kilburn-2022/tree/master Spring Cloud Gateway(Kilburn 2022.0.2版本)官方文档https://docs.spring.io/spring-cloud-gateway/docs/4.0.9/reference/html/ Spring Cloud Gateway官方文档https://cloud.spring.io/spring-cloud-gateway/reference/html/ Spring Cloud Gateway中文文档https://springdoc.cn/spring-cloud-gateway/ SpringCloud-Gateway搭建保姆级教程https://www.cnblogs.com/sun-10387834/p/17719582.html SpringCloud Gateway史上最全https://www.cnblogs.com/crazymakercircle/p/11704077.html 深入探索Spring Cloud Gateway微服务网关的最佳实践https://blog.csdn.net/zhengzhaoyang122/article/details/142744233 Spring Cloud Gateway API网关组件非常详细https://c.biancheng.net/springcloud/gateway.html spring cloud gateway 之限流篇https://www.cnblogs.com/chengmr/p/13277824.html spring cloud gateway网关使用JMeter进行限流测试与熔断https://cloud.tencent.com/developer/article/2344888 ring Cloud Gateway官方文档https://cloud.spring.io/spring-cloud-gateway/reference/html/ Spring Cloud Gateway中文文档https://springdoc.cn/spring-cloud-gateway/ SpringCloud-Gateway搭建保姆级教程https://www.cnblogs.com/sun-10387834/p/17719582.html SpringCloud Gateway史上最全https://www.cnblogs.com/crazymakercircle/p/11704077.html 深入探索Spring Cloud Gateway微服务网关的最佳实践https://blog.csdn.net/zhengzhaoyang122/article/details/142744233 Spring Cloud Gateway API网关组件非常详细https://c.biancheng.net/springcloud/gateway.html spring cloud gateway 之限流篇https://www.cnblogs.com/chengmr/p/13277824.html spring cloud gateway网关使用JMeter进行限流测试与熔断https://cloud.tencent.com/developer/article/2344888 深入学习spring cloud gateway 限流熔断https://zhuanlan.zhihu.com/p/520347258
http://www.hkea.cn/news/14399102/

相关文章:

  • asp保险网站源码中国服装设计公司排名
  • wordpress仿站视频太突然我国突然宣布
  • 网站改版建设公司免费空间网
  • 网站做充值和提现广东智慧团建系统入口
  • cms网站建设平面设计都学什么
  • 呼伦贝尔市住房和城乡建设局网站模板素材库
  • 什么网站不能备案网页开发多少钱
  • 上海外贸网站wordpress怎么改中文
  • 深圳做网站企业买一个软件大概多少钱
  • 企业网站页头背景图专题网站可以做什么
  • 网站设计案例分析商城网站模板html
  • 石家庄建立网站的公司网络营销论文题目
  • 网站无法处理请求ppt模板大全百度云
  • 柳州城乡建设管理局网站深圳沙头角网站建设
  • 什么是网站平台开发百度网站加v
  • 网站设计的书深圳做公司英文网站多少钱
  • 深圳服务网站建设如何做电商运营推广
  • 个人网站建设存在哪些问题营销型企业网站的提出
  • 自己怎么做wap网站个人备案网站用于企业
  • 孟村网 网站类模板模板下载网站
  • 做直播网站宽带河北工程大学网站开发成本
  • wap站开发shopify是什么平台
  • 黄页网站大全免费网在线电子类 购物网站
  • 一个网站的建立需要什么同城推广引流平台
  • 怎么把网站放到服务器池州网站建设电话
  • 营销型网站建设应该考虑哪些因素会员管理系统哪个好用
  • 网站建设三把火科技wordpress自动给关键词加内链方法
  • 泰安哪个做网站wordpress 指定目录页
  • 电商网站布局设计营销型wordpress模板
  • 如何高效的完成网站建设步骤手工制作大全简单