mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #447 from fangjian0423/master
add 'HTTPMethod:' prefix for the resource of using @SentinelRestTemplate
This commit is contained in:
commit
b8910ac908
@ -16,7 +16,7 @@
|
|||||||
"strategy": 0
|
"strategy": 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"resource": "http://www.taobao.com",
|
"resource": "GET:http://www.taobao.com",
|
||||||
"controlBehavior": 0,
|
"controlBehavior": 0,
|
||||||
"count": 0,
|
"count": 0,
|
||||||
"grade": 1,
|
"grade": 1,
|
||||||
|
@ -16,12 +16,10 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.sentinel.custom;
|
package org.springframework.cloud.alibaba.sentinel.custom;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.Entry;
|
import java.io.IOException;
|
||||||
import com.alibaba.csp.sentinel.SphU;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import com.alibaba.csp.sentinel.Tracer;
|
import java.lang.reflect.Method;
|
||||||
import com.alibaba.csp.sentinel.context.ContextUtil;
|
import java.net.URI;
|
||||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
|
||||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
|
|
||||||
|
|
||||||
import org.springframework.cloud.alibaba.sentinel.annotation.SentinelRestTemplate;
|
import org.springframework.cloud.alibaba.sentinel.annotation.SentinelRestTemplate;
|
||||||
import org.springframework.cloud.alibaba.sentinel.rest.SentinelClientHttpResponse;
|
import org.springframework.cloud.alibaba.sentinel.rest.SentinelClientHttpResponse;
|
||||||
@ -30,10 +28,12 @@ import org.springframework.http.client.ClientHttpRequestExecution;
|
|||||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||||
import org.springframework.http.client.ClientHttpResponse;
|
import org.springframework.http.client.ClientHttpResponse;
|
||||||
|
|
||||||
import java.io.IOException;
|
import com.alibaba.csp.sentinel.Entry;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import com.alibaba.csp.sentinel.SphU;
|
||||||
import java.lang.reflect.Method;
|
import com.alibaba.csp.sentinel.Tracer;
|
||||||
import java.net.URI;
|
import com.alibaba.csp.sentinel.context.ContextUtil;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interceptor using by SentinelRestTemplate
|
* Interceptor using by SentinelRestTemplate
|
||||||
@ -52,7 +52,8 @@ public class SentinelProtectInterceptor implements ClientHttpRequestInterceptor
|
|||||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
||||||
ClientHttpRequestExecution execution) throws IOException {
|
ClientHttpRequestExecution execution) throws IOException {
|
||||||
URI uri = request.getURI();
|
URI uri = request.getURI();
|
||||||
String hostResource = uri.getScheme() + "://" + uri.getHost()
|
String hostResource = request.getMethod().toString() + ":" + uri.getScheme()
|
||||||
|
+ "://" + uri.getHost()
|
||||||
+ (uri.getPort() == -1 ? "" : ":" + uri.getPort());
|
+ (uri.getPort() == -1 ? "" : ":" + uri.getPort());
|
||||||
String hostWithPathResource = hostResource + uri.getPath();
|
String hostWithPathResource = hostResource + uri.getPath();
|
||||||
boolean entryWithPath = true;
|
boolean entryWithPath = true;
|
||||||
|
@ -16,16 +16,15 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.sentinel;
|
package org.springframework.cloud.alibaba.sentinel;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
import static org.junit.Assert.assertEquals;
|
||||||
import com.alibaba.csp.sentinel.log.LogBase;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
import static org.mockito.Mockito.mock;
|
||||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
|
||||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
import java.util.Arrays;
|
||||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
import java.util.Map;
|
||||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
|
||||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -51,14 +50,16 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
|
||||||
import java.util.Map;
|
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||||
|
import com.alibaba.csp.sentinel.log.LogBase;
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||||
import static org.junit.Assert.assertEquals;
|
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
||||||
import static org.mockito.Mockito.mock;
|
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||||
|
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||||
@ -112,7 +113,7 @@ public class SentinelAutoConfigurationTests {
|
|||||||
FlowRule rule = new FlowRule();
|
FlowRule rule = new FlowRule();
|
||||||
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
|
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
|
||||||
rule.setCount(0);
|
rule.setCount(0);
|
||||||
rule.setResource(url);
|
rule.setResource("GET:" + url);
|
||||||
rule.setLimitApp("default");
|
rule.setLimitApp("default");
|
||||||
rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
|
rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
|
||||||
rule.setStrategy(RuleConstant.STRATEGY_DIRECT);
|
rule.setStrategy(RuleConstant.STRATEGY_DIRECT);
|
||||||
@ -120,7 +121,7 @@ public class SentinelAutoConfigurationTests {
|
|||||||
|
|
||||||
DegradeRule degradeRule = new DegradeRule();
|
DegradeRule degradeRule = new DegradeRule();
|
||||||
degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
|
degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
|
||||||
degradeRule.setResource(url + "/test");
|
degradeRule.setResource("GET:" + url + "/test");
|
||||||
degradeRule.setCount(0);
|
degradeRule.setCount(0);
|
||||||
degradeRule.setTimeWindow(60);
|
degradeRule.setTimeWindow(60);
|
||||||
DegradeRuleManager.loadRules(Arrays.asList(degradeRule));
|
DegradeRuleManager.loadRules(Arrays.asList(degradeRule));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user