mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
b6c49c1e52
@ -1,6 +1,5 @@
|
|||||||
|
|
||||||
coverage:
|
coverage:
|
||||||
status:
|
status:
|
||||||
patch:
|
project: off
|
||||||
default:
|
patch: off
|
||||||
enabled: no
|
|
@ -20,7 +20,7 @@
|
|||||||
<sentinel.version>1.4.2</sentinel.version>
|
<sentinel.version>1.4.2</sentinel.version>
|
||||||
<oss.version>3.1.0</oss.version>
|
<oss.version>3.1.0</oss.version>
|
||||||
<fescar.version>0.2.3</fescar.version>
|
<fescar.version>0.2.3</fescar.version>
|
||||||
<nacos.client.version>0.9.1</nacos.client.version>
|
<nacos.client.version>1.0.0-RC2</nacos.client.version>
|
||||||
<nacos.config.version>0.8.0</nacos.config.version>
|
<nacos.config.version>0.8.0</nacos.config.version>
|
||||||
<acm.version>1.0.8</acm.version>
|
<acm.version>1.0.8</acm.version>
|
||||||
<ans.version>1.0.1</ans.version>
|
<ans.version>1.0.1</ans.version>
|
||||||
|
@ -141,11 +141,11 @@ Nacos Discovery Starter 默认集成了 Ribbon ,所以对于使用了 Ribbon
|
|||||||
2. 打包编译后启动:在 nacos-discovery-consumer-example 项目中执行 `mvn clean package` 将工程编译打包,然后执行 `java -jar nacos-discovery-consumer-example.jar`启动应用。
|
2. 打包编译后启动:在 nacos-discovery-consumer-example 项目中执行 `mvn clean package` 将工程编译打包,然后执行 `java -jar nacos-discovery-consumer-example.jar`启动应用。
|
||||||
|
|
||||||
#### 验证
|
#### 验证
|
||||||
1. 在流量器地址栏中输入 http://127.0.0.1:18083/echo-rest/1234,点击跳转,可以看到浏览器显示了 nacos-discovery-provider-example 返回的消息 "hello Nacos Discovery 1234",证明服务发现生效。
|
1. 在浏览器地址栏中输入 http://127.0.0.1:18083/echo-rest/1234,点击跳转,可以看到浏览器显示了 nacos-discovery-provider-example 返回的消息 "hello Nacos Discovery 1234",证明服务发现生效。
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
1. 在流量器地址栏中输入 http://127.0.0.1:18083/echo-feign/12345,点击跳转,可以看到浏览器显示 nacos-discovery-provider-example 返回的消息 "hello Nacos Discovery 12345",证明服务发现生效。
|
1. 在浏览器地址栏中输入 http://127.0.0.1:18083/echo-feign/12345,点击跳转,可以看到浏览器显示 nacos-discovery-provider-example 返回的消息 "hello Nacos Discovery 12345",证明服务发现生效。
|
||||||
|
|
||||||

|

|
||||||
## 原理
|
## 原理
|
||||||
|
@ -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,7 +16,9 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.sentinel.custom;
|
package org.springframework.cloud.alibaba.sentinel.custom;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -38,9 +40,7 @@ import org.springframework.util.ClassUtils;
|
|||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PostProcessor handle @SentinelRestTemplate Annotation, add interceptor for RestTemplate
|
* PostProcessor handle @SentinelRestTemplate Annotation, add interceptor for RestTemplate
|
||||||
@ -193,6 +193,9 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
|
|||||||
// register SentinelProtectInterceptor bean
|
// register SentinelProtectInterceptor bean
|
||||||
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext
|
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext
|
||||||
.getAutowireCapableBeanFactory();
|
.getAutowireCapableBeanFactory();
|
||||||
|
if (beanFactory.containsBean(interceptorBeanName)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
|
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
|
||||||
.genericBeanDefinition(SentinelProtectInterceptor.class);
|
.genericBeanDefinition(SentinelProtectInterceptor.class);
|
||||||
beanDefinitionBuilder.addConstructorArgValue(sentinelRestTemplate);
|
beanDefinitionBuilder.addConstructorArgValue(sentinelRestTemplate);
|
||||||
|
@ -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));
|
||||||
|
@ -85,6 +85,11 @@ public class SentinelRestTemplateTests {
|
|||||||
new AnnotationConfigApplicationContext(TestConfig9.class);
|
new AnnotationConfigApplicationContext(TestConfig9.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNormalWithoutParam() {
|
||||||
|
new AnnotationConfigApplicationContext(TestConfig10.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public static class TestConfig1 {
|
public static class TestConfig1 {
|
||||||
@Bean
|
@Bean
|
||||||
@ -220,6 +225,27 @@ public class SentinelRestTemplateTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public static class TestConfig10 {
|
||||||
|
@Bean
|
||||||
|
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||||
|
ApplicationContext applicationContext) {
|
||||||
|
return new SentinelBeanPostProcessor(applicationContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@SentinelRestTemplate
|
||||||
|
RestTemplate restTemplate() {
|
||||||
|
return new RestTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@SentinelRestTemplate
|
||||||
|
RestTemplate restTemplate2() {
|
||||||
|
return new RestTemplate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class ExceptionUtil {
|
public static class ExceptionUtil {
|
||||||
public static SentinelClientHttpResponse handleException(HttpRequest request,
|
public static SentinelClientHttpResponse handleException(HttpRequest request,
|
||||||
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
|
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user