网关配置文件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")
        );
    }*/

}

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

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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐