Fix typo in rule constants and add authority rule demo
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
parent
b76ad9b480
commit
4226868dcf
|
|
@ -27,8 +27,8 @@ public class RuleConstant {
|
||||||
public static final int DEGRADE_GRADE_RT = 0;
|
public static final int DEGRADE_GRADE_RT = 0;
|
||||||
public static final int DEGRADE_GRADE_EXCEPTION = 1;
|
public static final int DEGRADE_GRADE_EXCEPTION = 1;
|
||||||
|
|
||||||
public static final int WHILE = 0;
|
public static final int AUTHORITY_WHITE = 0;
|
||||||
public static final int BLACK = 1;
|
public static final int AUTHORITY_BLACK = 1;
|
||||||
|
|
||||||
public static final int STRATEGY_DIRECT = 0;
|
public static final int STRATEGY_DIRECT = 0;
|
||||||
public static final int STRATEGY_RELATE = 1;
|
public static final int STRATEGY_RELATE = 1;
|
||||||
|
|
|
||||||
|
|
@ -82,11 +82,11 @@ public class AuthorityRule extends AbstractRule {
|
||||||
contain = exactlyMatch;
|
contain = exactlyMatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strategy == RuleConstant.BLACK && contain) {
|
if (strategy == RuleConstant.AUTHORITY_BLACK && contain) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strategy == RuleConstant.WHILE && !contain) {
|
if (strategy == RuleConstant.AUTHORITY_WHITE && !contain) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,86 @@
|
||||||
|
/*
|
||||||
|
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||||
|
*
|
||||||
|
* 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 com.alibaba.csp.sentinel.demo.authority;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.Entry;
|
||||||
|
import com.alibaba.csp.sentinel.SphU;
|
||||||
|
import com.alibaba.csp.sentinel.context.ContextUtil;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Authority rules is designed for limiting by request origins. In blacklist mode,
|
||||||
|
* requests will be blocked when blacklist contains current origin, otherwise will pass.
|
||||||
|
* In whitelist mode, only requests from whitelist origin can pass.
|
||||||
|
*
|
||||||
|
* @author Eric Zhao
|
||||||
|
*/
|
||||||
|
public class AuthorityDemo {
|
||||||
|
|
||||||
|
private static final String RESOURCE_NAME = "testABC";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("========Testing for black list========");
|
||||||
|
initBlackRules();
|
||||||
|
testFor(RESOURCE_NAME, "appA");
|
||||||
|
testFor(RESOURCE_NAME, "appB");
|
||||||
|
testFor(RESOURCE_NAME, "appC");
|
||||||
|
testFor(RESOURCE_NAME, "appE");
|
||||||
|
|
||||||
|
System.out.println("========Testing for white list========");
|
||||||
|
initWhiteRules();
|
||||||
|
testFor(RESOURCE_NAME, "appA");
|
||||||
|
testFor(RESOURCE_NAME, "appB");
|
||||||
|
testFor(RESOURCE_NAME, "appC");
|
||||||
|
testFor(RESOURCE_NAME, "appE");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void testFor(/*@NonNull*/ String resource, /*@NonNull*/ String origin) {
|
||||||
|
ContextUtil.enter(resource, origin);
|
||||||
|
Entry entry = null;
|
||||||
|
try {
|
||||||
|
entry = SphU.entry(resource);
|
||||||
|
System.out.println(String.format("Passed for resource %s, origin is %s", resource, origin));
|
||||||
|
} catch (BlockException ex) {
|
||||||
|
System.err.println(String.format("Blocked for resource %s, origin is %s", resource, origin));
|
||||||
|
} finally {
|
||||||
|
if (entry != null) {
|
||||||
|
entry.exit();
|
||||||
|
}
|
||||||
|
ContextUtil.exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void initWhiteRules() {
|
||||||
|
AuthorityRule rule = new AuthorityRule();
|
||||||
|
rule.setResource(RESOURCE_NAME);
|
||||||
|
rule.setStrategy(RuleConstant.AUTHORITY_WHITE);
|
||||||
|
rule.setLimitApp("appA,appE");
|
||||||
|
AuthorityRuleManager.loadRules(Collections.singletonList(rule));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void initBlackRules() {
|
||||||
|
AuthorityRule rule = new AuthorityRule();
|
||||||
|
rule.setResource(RESOURCE_NAME);
|
||||||
|
rule.setStrategy(RuleConstant.AUTHORITY_BLACK);
|
||||||
|
rule.setLimitApp("appA,appB");
|
||||||
|
AuthorityRuleManager.loadRules(Collections.singletonList(rule));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue