-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($api-gateway): support request rate limit
support request rate limit BREAKING CHANGE: support request rate limit
- Loading branch information
1 parent
d74797a
commit c0dc896
Showing
11 changed files
with
137 additions
and
17 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...va/com/jmsoftware/maf/apigateway/universal/configuration/DiscoveryRouteConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.jmsoftware.maf.apigateway.universal.configuration; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
import org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties; | ||
import org.springframework.cloud.gateway.filter.FilterDefinition; | ||
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
/** | ||
* Description: DiscoveryRouteConfiguration, change description here. | ||
* | ||
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 2/7/2021 1:15 PM | ||
**/ | ||
@Slf4j | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class DiscoveryRouteConfiguration { | ||
private final DiscoveryLocatorProperties discoveryLocatorProperties; | ||
|
||
@PostConstruct | ||
void postConstruct() { | ||
val filter = new FilterDefinition(); | ||
filter.setName("RequestRateLimiter"); | ||
// TODO: arguments should be passed from configuration dynamically | ||
filter.addArg("redis-rate-limiter.replenishRate", "1"); | ||
filter.addArg("redis-rate-limiter.burstCapacity", "1"); | ||
filter.addArg("redis-rate-limiter.requestedTokens", "1"); | ||
discoveryLocatorProperties.getFilters().add(filter); | ||
log.info("Added filter[{}] for discovery services, filters: {}", RedisRateLimiter.class.getSimpleName(), | ||
discoveryLocatorProperties.getFilters()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 2 additions & 10 deletions
12
api-gateway/src/main/resources/application-development-local.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,9 @@ | ||
spring: | ||
zipkin: | ||
base-url: http://localhost:9411 | ||
devtools: | ||
add-properties: true | ||
zipkin: | ||
base-url: http://localhost:9411 | ||
redis: | ||
database: 0 | ||
host: localhost | ||
port: 6379 | ||
password: 123456 | ||
timeout: 10000ms | ||
lettuce: | ||
pool: | ||
max-active: 20 | ||
max-idle: 10 | ||
max-wait: -1ms | ||
min-idle: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
spring: | ||
zipkin: | ||
base-url: http://maf.zipkin.production:9411 | ||
devtools: | ||
add-properties: false | ||
zipkin: | ||
base-url: http://maf.zipkin.production:9411 | ||
redis: | ||
host: maf.redis.production | ||
port: 6379 | ||
password: 123456 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
spring: | ||
zipkin: | ||
base-url: http://maf.zipkin.stage:9411 | ||
devtools: | ||
add-properties: false | ||
zipkin: | ||
base-url: http://maf.zipkin.stage:9411 | ||
redis: | ||
host: maf.redis.stage | ||
port: 6379 | ||
password: 123456 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
spring: | ||
zipkin: | ||
base-url: http://maf.zipkin.test:9411 | ||
devtools: | ||
add-properties: false | ||
zipkin: | ||
base-url: http://maf.zipkin.test:9411 | ||
redis: | ||
host: maf.redis.test | ||
port: 6379 | ||
password: 123456 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
.../java/com/jmsoftware/maf/reactivespringcloudstarter/configuration/RedisConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.jmsoftware.maf.reactivespringcloudstarter.configuration; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.core.ReactiveRedisTemplate; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
import org.springframework.data.redis.serializer.RedisSerializer; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
/** | ||
* Description: RedisConfiguration, change description here. | ||
* | ||
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/30/2020 1:08 PM | ||
**/ | ||
@Slf4j | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class RedisConfiguration { | ||
@PostConstruct | ||
private void postConstruct() { | ||
log.warn("Initial bean: '{}'", RedisConfiguration.class.getSimpleName()); | ||
} | ||
|
||
/** | ||
* Reactive redis template factory. | ||
* | ||
* @param connectionFactory the reactive redis connection factory | ||
* @return the reactive redis template | ||
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 12/30/2020 1:43 PM | ||
*/ | ||
@Bean | ||
ReactiveRedisTemplate<Object, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory) { | ||
log.warn("Initial bean: '{}'", ReactiveRedisTemplate.class.getSimpleName()); | ||
Jackson2JsonRedisSerializer<Object> valueSerializer = new Jackson2JsonRedisSerializer<>(Object.class); | ||
RedisSerializationContext<Object, Object> serializationContext = RedisSerializationContext | ||
.newSerializationContext( | ||
RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.string())) | ||
.value(valueSerializer) | ||
.hashValue(valueSerializer) | ||
.build(); | ||
return new ReactiveRedisTemplate<>(connectionFactory, serializationContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters