1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Optimize Sentinel RestTemplate url resource

This commit is contained in:
fangjian0423 2018-12-18 17:35:45 +08:00
parent feeacc0a15
commit d204a54b76
5 changed files with 13 additions and 7 deletions

View File

@ -185,7 +185,7 @@ Sentinel RestTemplate 限流的资源规则提供两种粒度:
* `schema://host:port`:协议、主机和端口
NOTE: 以 `https://www.taobao.com/test` 这个 url 为例。对应的资源名有两种粒度,分别是 `https://www.taobao.com:80` 以及 `https://www.taobao.com:80/test`
NOTE: 以 `https://www.taobao.com/test` 这个 url 为例。对应的资源名有两种粒度,分别是 `https://www.taobao.com` 以及 `https://www.taobao.com/test`
### 动态数据源支持

View File

@ -187,7 +187,7 @@ Sentinel RestTemplate provides two granularities for resource rate limiting:
* `schema://host:port` Protocol, host and port
NOTE: Take `https://www.taobao.com/test` as an example. The corresponding resource names have two levels of granularities, `https://www.taobao.com:80` and `https://www.taobao.com:80/test`.
NOTE: Take `https://www.taobao.com/test` as an example. The corresponding resource names have two levels of granularities, `https://www.taobao.com` and `https://www.taobao.com/test`.
### Dynamic Data Source Support

View File

@ -1,5 +1,9 @@
package org.springframework.cloud.alibaba.cloud.examples;
import org.springframework.cloud.alibaba.sentinel.rest.SentinelClientHttpResponse;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import com.alibaba.csp.sentinel.slots.block.BlockException;
/**
@ -7,8 +11,10 @@ import com.alibaba.csp.sentinel.slots.block.BlockException;
*/
public class ExceptionUtil {
public static void handleException(BlockException ex) {
public static SentinelClientHttpResponse handleException(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
return new SentinelClientHttpResponse("custom block info");
}
}

View File

@ -16,9 +16,9 @@
"strategy": 0
},
{
"resource": "abc",
"resource": "http://www.taobao.com",
"controlBehavior": 0,
"count": 1,
"count": 0,
"grade": 1,
"limitApp": "default",
"strategy": 0

View File

@ -58,8 +58,8 @@ public class SentinelProtectInterceptor implements ClientHttpRequestInterceptor
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
URI uri = request.getURI();
String hostResource = uri.getScheme() + "://" + uri.getHost() + ":"
+ (uri.getPort() == -1 ? 80 : uri.getPort());
String hostResource = uri.getScheme() + "://" + uri.getHost()
+ (uri.getPort() == -1 ? "" : ":" + uri.getPort());
String hostWithPathResource = hostResource + uri.getPath();
Entry hostEntry = null, hostWithPathEntry = null;
ClientHttpResponse response;