SpringCloudGateway实现路由过滤和请求限流

1、网关路由过滤配置

1.1、网关跨域配置

spring:
  application:
    name: gateway
  # 微服务网关跨域配置
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]': # 匹配所有请求
            allowedOrigins: "*" # 跨域处理 允许所有的域
            allowedMethods: # 支持的方法
              - GET
              - POST
              - PUT
              - DELETE

1.2、Host过滤配置

spring:
  application:
    name: gateway
  # 微服务网关跨域配置
  cloud:
    gateway:
      routes: # 路由过滤
        - id: user_service # 唯一标识
          # uri:请求要路由到的微服务地址
          uri: http://localhost:8081
          predicates: # 路由规则配置
          # Host:根据用户请求域名过滤
          # 所有以cloud.pkx.com的请求将被路由到http://localhost: 8081微服务
          - Host= cloud.pkx.com**

1.3、Path路径过滤配置

spring:
  application:
    name: gateway 
  cloud:
    gateway:
      routes: # 路由过滤
        - id: user_service # 唯一标识
          # uri:请求要路由到的微服务地址
          uri: http://localhost:8081

          predicates: # 路由规则配置
          # Path:根据用户请求路径过滤
          # 所有以/user开始的请求,都路由到http://localhost:8081微服务
          - Path=/user/**

1.4、StripPrefix过滤配置

spring:
  application:
    name: gateway
  # 微服务网关跨域配置
  cloud:
    gateway:
      routes: # 路由过滤
        - id: user_service # 唯一标识
          # uri:请求要路由到的微服务地址
          uri: http://localhost:8081
          predicates: # 路由规则配置
          # 所有以/api/user开始的请求,都路由到http://localhost:8081微服务
          - Path=/api/user/**
          filters:
            # StripPrefix:截取路径前缀路径
            # StripPrefix=1: 将请求路径中的第1个路径去掉,请求路径以/区分,一个/代表一个路径
            # 每次请求真实微服务时候,需要微服务网关将/api去掉
            - StripPrefix=1

1.5、PrefixPath过滤配置

spring:
  application:
    name: gateway
  # 微服务网关跨域配置
  cloud:
    gateway:
      routes: # 路由过滤
        - id: user_service # 唯一标识
          # uri:请求要路由到的微服务地址
          uri: http://localhost:8081
         
          predicates: # 路由规则配置
          # 所有以/**开始的请求,都路由到http://localhost:8081微服务
          - Path=/**
          
          filters:
          # PrefixPath:给请求路径自动加上前缀路径
          # PrefixPath=/user:当用户请求/**时:网关微服务在用户请求路径前加上:/user/**
          - PrefixPath=/user

1.6、Gateway-LoadBanlancerClient实现负载均衡

spring:
  application:
    name: gateway 
  cloud:
    gateway:
      routes: # 路由过滤
        - id: user_service # 唯一标识
        # lb: 使用loadBalanceClient实现负载均衡,后面users是微服务的名称[主要应用于集群环境]
          uri: lb://users

          predicates: # 路由规则配置
          # Path:根据用户请求路径过滤
          # 所有以/user开始的请求,都路由到http://localhost:8081微服务
          - Path=/user/**

2、网关限流配置--基于IP限流

2.1、导入依赖

<!--redis-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>

2.2、修改启动类:创建唯一标识对象

@SpringBootApplication
@EnableEurekaClient
public class GatewayWebApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayWebApplication.class, args);
    }
    /**
     * 创建用户唯一标识:使用IP作为用户唯一标识,根据IP进行限流操作
     * @return
     */
    @Bean(name = "iPKeyResolver")
    public KeyResolver userKeyResolver(){
        return new KeyResolver() {
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {
                // 获得用户iP
                String ip = exchange.getRequest().getRemoteAddress().getHostString();
                return Mono.just(ip);
            }
        };
    }

2.3、修改application.yml:增加配置信息

spring:
  application:
    name: gateway
  # 微服务网关跨域配置
  cloud:
    gateway:
      routes: # 路由过滤
        - id: user_service # 唯一标识
          #lb:使用loadBalanceClient实现负载均衡,后面users是微服务的名称[应用于集群环境]
          uri: lb://users

          # Path:根据用户请求路径过滤
          # 所有以/user开始的请求,都路由到http://localhost:8081微服务
          # - Path=/user/**

          # 局部限流过滤器
          - name: RequestRateLimiter # 请求数限流,名字不能随便写,使用默认的factory
            args:
              key-resolver: "#{@iPKeyResolver}"
              # 每秒钟只允许1个请求
              redis-rate-limiter.replenishRate: 1
              # 允许并发有2个请求[宽限的个数]
              reids-rate-limiter.burstCapacity: 2

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fkjaios_xkp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值