* An enhancement when multiple HystrixConcurrencyStrategy detected. Fixes #3256 * replace exception with wran log
This commit is contained in:
@@ -18,6 +18,9 @@ package org.springframework.cloud.netflix.hystrix.security;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
@@ -42,6 +45,7 @@ import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
|
||||
@Conditional(HystrixSecurityCondition.class)
|
||||
@ConditionalOnClass({ Hystrix.class, SecurityContext.class })
|
||||
public class HystrixSecurityAutoConfiguration {
|
||||
private static final Log LOGGER = LogFactory.getLog(HystrixSecurityAutoConfiguration.class);
|
||||
@Autowired(required = false)
|
||||
private HystrixConcurrencyStrategy existingConcurrencyStrategy;
|
||||
|
||||
@@ -56,18 +60,36 @@ public class HystrixSecurityAutoConfiguration {
|
||||
.getPropertiesStrategy();
|
||||
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance()
|
||||
.getCommandExecutionHook();
|
||||
HystrixConcurrencyStrategy concurrencyStrategy = detectRegisteredConcurrencyStrategy();
|
||||
|
||||
HystrixPlugins.reset();
|
||||
|
||||
// Registers existing plugins excepts the Concurrent Strategy plugin.
|
||||
HystrixPlugins.getInstance().registerConcurrencyStrategy(
|
||||
new SecurityContextConcurrencyStrategy(existingConcurrencyStrategy));
|
||||
new SecurityContextConcurrencyStrategy(concurrencyStrategy));
|
||||
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
|
||||
HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
|
||||
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
|
||||
HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);
|
||||
}
|
||||
|
||||
private HystrixConcurrencyStrategy detectRegisteredConcurrencyStrategy() {
|
||||
HystrixConcurrencyStrategy registeredStrategy = HystrixPlugins.getInstance()
|
||||
.getConcurrencyStrategy();
|
||||
if (existingConcurrencyStrategy == null) {
|
||||
return registeredStrategy;
|
||||
}
|
||||
//Hystrix registered a default Strategy.
|
||||
if (registeredStrategy instanceof HystrixConcurrencyStrategyDefault){
|
||||
return existingConcurrencyStrategy;
|
||||
}
|
||||
//If registeredStrategy not the default and not some use bean of existingConcurrencyStrategy.
|
||||
if (!existingConcurrencyStrategy.equals(registeredStrategy)){
|
||||
LOGGER.warn("Multiple HystrixConcurrencyStrategy detected. Bean of HystrixConcurrencyStrategy was used.");
|
||||
}
|
||||
return existingConcurrencyStrategy;
|
||||
}
|
||||
|
||||
static class HystrixSecurityCondition extends AllNestedConditions {
|
||||
|
||||
public HystrixSecurityCondition() {
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright 2013-2018 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
|
||||
*
|
||||
* http://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.netflix.hystrix.security;
|
||||
|
||||
import com.netflix.hystrix.strategy.HystrixPlugins;
|
||||
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
|
||||
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault;
|
||||
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier;
|
||||
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
|
||||
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher;
|
||||
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
|
||||
import org.junit.Test;
|
||||
import org.mockito.internal.util.reflection.FieldSetter;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author : ailin.zhou
|
||||
*/
|
||||
public class HystrixSecurityAutoConfigurationTest {
|
||||
|
||||
@Test
|
||||
public void testInit() throws NoSuchFieldException, IllegalAccessException {
|
||||
|
||||
//save test context
|
||||
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance()
|
||||
.getEventNotifier();
|
||||
HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance()
|
||||
.getMetricsPublisher();
|
||||
HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance()
|
||||
.getPropertiesStrategy();
|
||||
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance()
|
||||
.getCommandExecutionHook();
|
||||
HystrixConcurrencyStrategy concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();
|
||||
|
||||
//test
|
||||
testForMultiConcurrentStrategy();
|
||||
|
||||
//recover test context
|
||||
HystrixPlugins.reset();
|
||||
HystrixPlugins.getInstance().registerConcurrencyStrategy(concurrencyStrategy);
|
||||
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
|
||||
HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
|
||||
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
|
||||
HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void testForMultiConcurrentStrategy() throws IllegalAccessException, NoSuchFieldException {
|
||||
HystrixSecurityAutoConfiguration securityStrategy = new HystrixSecurityAutoConfiguration();
|
||||
|
||||
//1.existingConcurrencyStrategy is null, registeredStrategy is default
|
||||
HystrixPlugins.reset();
|
||||
securityStrategy.init();
|
||||
//result is default
|
||||
assertEquals(HystrixConcurrencyStrategyDefault.getInstance(), getOriginalInSecurityConcurrencyStrategy());
|
||||
|
||||
//2.existingConcurrencyStrategy is null, registered strategy is customized
|
||||
HystrixPlugins.reset();
|
||||
HystrixConcurrencyStrategy customized = new HystrixConcurrencyStrategy() {
|
||||
};
|
||||
HystrixPlugins.getInstance().registerConcurrencyStrategy(customized);
|
||||
securityStrategy.init();
|
||||
//result is customized
|
||||
assertEquals(customized, getOriginalInSecurityConcurrencyStrategy());
|
||||
|
||||
//3.existingConcurrencyStrategy is not null, registeredStrategy is default.
|
||||
HystrixPlugins.reset();
|
||||
HystrixConcurrencyStrategy existingConcurrencyStrategy = new HystrixConcurrencyStrategy() {
|
||||
};
|
||||
FieldSetter.setField(securityStrategy, securityStrategy.getClass().getDeclaredField("existingConcurrencyStrategy"), existingConcurrencyStrategy);
|
||||
securityStrategy.init();
|
||||
//result is existingConcurrencyStrategy
|
||||
assertEquals(existingConcurrencyStrategy, getOriginalInSecurityConcurrencyStrategy());
|
||||
|
||||
//4.existingConcurrencyStrategy is not null, registeredStrategy is customized.
|
||||
HystrixPlugins.reset();
|
||||
HystrixPlugins.getInstance().registerConcurrencyStrategy(customized);
|
||||
FieldSetter.setField(securityStrategy, securityStrategy.getClass().getDeclaredField("existingConcurrencyStrategy"), existingConcurrencyStrategy);
|
||||
securityStrategy.init();
|
||||
assertEquals(existingConcurrencyStrategy, getOriginalInSecurityConcurrencyStrategy());
|
||||
}
|
||||
|
||||
private HystrixConcurrencyStrategy getOriginalInSecurityConcurrencyStrategy() throws IllegalAccessException, NoSuchFieldException {
|
||||
HystrixConcurrencyStrategy concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();
|
||||
Field existingConcurrencyStrategy = concurrencyStrategy.getClass().getDeclaredField("existingConcurrencyStrategy");
|
||||
existingConcurrencyStrategy.setAccessible(true);
|
||||
HystrixConcurrencyStrategy strategyInSecurityStrategy = (HystrixConcurrencyStrategy) existingConcurrencyStrategy.get(concurrencyStrategy);
|
||||
return strategyInSecurityStrategy;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user