springcloud之getaway配置,限流,跨域
网关配置文件application.yml–>server:port: 8090eureka:client:service-url:defaultZone: http://localhost:7002/eureka/instance:prefer-ip-address: trueinstance-id: party-gateway-${server.port}spring:applicati
·
网关配置文件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")
);
}*/
}
通过配置文件和获取这种方法的限流来实现限流。
更多推荐
已为社区贡献18条内容
所有评论(0)