springcloud之getaway配置,限流,跨域

网关配置文件application.yml–>

server:
  port: 8090

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7002/eureka/
  instance:
    prefer-ip-address: true
    instance-id: party-gateway-${server.port}


spring:
  application:
    name: party-gateway
  cloud:
    gateway:
      discovery:
        locator:
          lower-case-service-id: true
      globalcors:
        cors-configurations:
          '[/**]':
             allowed-origins: "*"
             allowed-headers: "*"
             allowedMethods: "*"
             allow-credentials: true
             maxAge: 3600
      routes:
        - id: system-service
          uri: lb://system-service
          predicates:
            - Path=/system/**
          filters:
            - StripPrefix=1
              # 限流
            - name: RequestRateLimiter
              args:
                # 令牌桶每秒填充平均速率
                redis-rate-limiter.replenishRate: 100
                # 令牌桶的上限
                redis-rate-limiter.burstCapacity: 200
                # 使用SpEL表达式从Spring容器中获取Bean对象
                key-resolver: "#{@pathKeyResolver}"

        - id: meeting-service
          uri: lb://meeting-service
          predicates:
            - Path=/meeting/**
          filters:
            - StripPrefix=1
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 100
                redis-rate-limiter.burstCapacity: 200
                key-resolver: "#{@pathKeyResolver}"
        - id: article-service
          uri: lb://article-service
          predicates:
            - Path=/article/**
          filters:
            - StripPrefix=1
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 100
                redis-rate-limiter.burstCapacity: 200
                key-resolver: "#{@pathKeyResolver}"
        - id: file-service
          uri: lb://file-service
          predicates:
            - Path=/file/**
          filters:
            - StripPrefix=1
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 100
                redis-rate-limiter.burstCapacity: 200
                key-resolver: "#{@pathKeyResolver}"
        - id: question-service
          uri: lb://question-service
          predicates:
            - Path=/question/**
          filters:
            - StripPrefix=1
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 100
                redis-rate-limiter.burstCapacity: 200
                key-resolver: "#{@pathKeyResolver}"
        - id: studio-service
          uri: lb://studio-service
          predicates:
            - Path=/studio/**
          filters:
            - StripPrefix=1
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 100
                redis-rate-limiter.burstCapacity: 200
                key-resolver: "#{@pathKeyResolver}"
  redis:
    host: 192.168.30.75
    port: 6379
    password: 123456
    database: 1


跨域配置–>

package com.fs.partygateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.cloud.gateway.config.GlobalCorsProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @Author yjj
 * @date 2021/7/5
 */
@Configuration
@ConditionalOnBean(GlobalCorsProperties.class)
public class CorsAutoConfiguration {

    @Autowired
    private GlobalCorsProperties globalCorsProperties;

    @Bean
    public CorsWebFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        globalCorsProperties.getCorsConfigurations()
                .forEach((path, corsConfiguration) -> source.registerCorsConfiguration(path, corsConfiguration));
        return new CorsWebFilter(source);
    }
}

各种方式限流–>

server:
  port: 8090

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7002/eureka/
  instance:
    prefer-ip-address: true
    instance-id: party-gateway-${server.port}


spring:
  application:
    name: party-gateway
  cloud:
    gateway:
      discovery:
        locator:
          lower-case-service-id: true
      globalcors:
        cors-configurations:
          '[/**]':
             allowed-origins: "*"
             allowed-headers: "*"
             allowedMethods: "*"
             allow-credentials: true
             maxAge: 3600
      routes:
        - id: system-service
          uri: lb://system-service
          predicates:
            - Path=/system/**
          filters:
            - StripPrefix=1
              # 限流
            - name: RequestRateLimiter
              args:
                # 令牌桶每秒填充平均速率
                redis-rate-limiter.replenishRate: 100
                # 令牌桶的上限
                redis-rate-limiter.burstCapacity: 200
                # 使用SpEL表达式从Spring容器中获取Bean对象
                key-resolver: "#{@pathKeyResolver}"

        - id: meeting-service
          uri: lb://meeting-service
          predicates:
            - Path=/meeting/**
          filters:
            - StripPrefix=1
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 100
                redis-rate-limiter.burstCapacity: 200
                key-resolver: "#{@pathKeyResolver}"
        - id: article-service
          uri: lb://article-service
          predicates:
            - Path=/article/**
          filters:
            - StripPrefix=1
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 100
                redis-rate-limiter.burstCapacity: 200
                key-resolver: "#{@pathKeyResolver}"
        - id: file-service
          uri: lb://file-service
          predicates:
            - Path=/file/**
          filters:
            - StripPrefix=1
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 100
                redis-rate-limiter.burstCapacity: 200
                key-resolver: "#{@pathKeyResolver}"
        - id: question-service
          uri: lb://question-service
          predicates:
            - Path=/question/**
          filters:
            - StripPrefix=1
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 100
                redis-rate-limiter.burstCapacity: 200
                key-resolver: "#{@pathKeyResolver}"
        - id: studio-service
          uri: lb://studio-service
          predicates:
            - Path=/studio/**
          filters:
            - StripPrefix=1
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 100
                redis-rate-limiter.burstCapacity: 200
                key-resolver: "#{@pathKeyResolver}"
  redis:
    host: 192.168.30.75
    port: 6379
    password: 123456
    database: 1


package com.fs.partygateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

/**
 * @Author yjj
 * @date 2021/6/30
 */
@Configuration
public class ResolverConfig {

    /**
     *  根据路径限流
     */
    @Bean
    public KeyResolver pathKeyResolver() {
        return exchange -> Mono.just(
                exchange.getRequest()
                        .getPath()
                        .toString()
        );
    }

    /**
     *  根据IP限流
     */
    /*@Bean
    public KeyResolver ipKeyResolver() {
        return exchange -> Mono.just(
                exchange.getRequest()
                        .getHeaders()
                        .getFirst("X-Forwarded-For")
        );
    }*/
    /**
     *  根据用户限流
     */
    /*@Bean
    public KeyResolver userKeyResolver() {
        return exchange -> Mono.just(
                exchange.getRequest()
                        .getQueryParams()
                        .getFirst("user")
        );
    }*/

}

通过配置文件和获取这种方法的限流来实现限流。

网关配置参考
跨域参考
限流参考

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

bst@微胖子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值