Adds a property to disable gateway redis integration.
Fixes gh-2052
This commit is contained in:
@@ -24,6 +24,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration;
|
||||
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
|
||||
import org.springframework.cloud.gateway.support.ConfigurationService;
|
||||
@@ -43,6 +44,8 @@ import org.springframework.web.reactive.DispatcherHandler;
|
||||
@AutoConfigureBefore(GatewayAutoConfiguration.class)
|
||||
@ConditionalOnBean(ReactiveRedisTemplate.class)
|
||||
@ConditionalOnClass({ RedisTemplate.class, DispatcherHandler.class })
|
||||
@ConditionalOnProperty(name = "spring.cloud.gateway.redis.enabled",
|
||||
matchIfMissing = true)
|
||||
class GatewayRedisAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2013-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.gateway.config;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.experimental.runners.Enclosed;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
|
||||
import org.springframework.data.redis.core.script.RedisScript;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(Enclosed.class)
|
||||
public class GatewayRedisAutoConfigurationTests {
|
||||
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration
|
||||
protected static class Config {
|
||||
|
||||
}
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Config.class)
|
||||
public static class EnabledByDefault {
|
||||
|
||||
@Autowired(required = false)
|
||||
private RedisScript redisRequestRateLimiterScript;
|
||||
|
||||
@Autowired(required = false)
|
||||
private RedisRateLimiter redisRateLimiter;
|
||||
|
||||
@Test
|
||||
public void shouldInjectRedisBeans() {
|
||||
assertThat(redisRequestRateLimiterScript).isNotNull();
|
||||
assertThat(redisRateLimiter).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Config.class,
|
||||
properties = "spring.cloud.gateway.redis.enabled=false")
|
||||
public static class DisabledByProperty {
|
||||
|
||||
@Autowired(required = false)
|
||||
private RedisScript redisRequestRateLimiterScript;
|
||||
|
||||
@Autowired(required = false)
|
||||
private RedisRateLimiter redisRateLimiter;
|
||||
|
||||
@Test
|
||||
public void shouldDisableRedisBeans() {
|
||||
assertThat(redisRequestRateLimiterScript).isNull();
|
||||
assertThat(redisRateLimiter).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user