Set<SwitchRule> switchRuleSet = initSwitchRule(); // 遍历规则 for (SwitchRule rule : switchRuleSet) { // 判断开关状态,开关未打开则跳过 if (!rule.getStatus().equalsIgnoreCase(SwitchRule.SWITCH_KEY_OPEN)) { continue; } if (rule.getResources() == null) { continue; } // 实现 include 语意 if (!CollectionUtils.isEmpty(rule.getResources().getInclude())) { if (rule.getResources().getInclude().contains(resource.getName())) { throw new SwitchException(resource.getName(),"switch"); } } // 实现 exclude 语意 if (!CollectionUtils.isEmpty(rule.getResources().getExclude())) { if (!rule.getResources().getExclude().contains(resource.getName())) { throw new SwitchException(resource.getName(),"switch"); } } }
SwitchSlot
1 2 3 4 5 6 7 8 9 10 11 12 13
public class SwitghSlot extends AbstractLinkedProcessorSlot<Object>{ @Override public void entry(Context context, Resourcelrapper resourcelrapper, Object o, int i, boolean b, 0bject... objects) throws Throwabl{ //在调用资源时执行开发降级的判断 SwitchRuleChecker.checkSwitch(resourceWrapper, context) fireEntry(context,resourceWrapper, o,i,b, objects) }
@Override public void exit (Context context, Resourcerapper resourceWrapper, int i, 0bject... objects){ fireExit(context, resourceWrapper, i, objects) } }
SlotChainBuilder
1 2 3 4 5 6 7 8 9
public class SwitchSlotChainBuilder extends DefaultSlotChainBuilder{ @Override public ProcessorSlotChain build(){ ProcessorSlotChain chain = super.build(): chain.addLast(new SwitchSlot()); return chain; } }