mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Optimize Sentinel RestTemplate url resource for 1.x branch
This commit is contained in:
parent
7b02f93f20
commit
a3fef5d955
@ -1,5 +1,9 @@
|
|||||||
package org.springframework.cloud.alibaba.cloud.examples;
|
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;
|
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -7,8 +11,10 @@ import com.alibaba.csp.sentinel.slots.block.BlockException;
|
|||||||
*/
|
*/
|
||||||
public class ExceptionUtil {
|
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());
|
System.out.println("Oops: " + ex.getClass().getCanonicalName());
|
||||||
|
return new SentinelClientHttpResponse("custom block info");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
package org.springframework.cloud.alibaba.cloud.examples;
|
package org.springframework.cloud.alibaba.cloud.examples;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
*/
|
*/
|
||||||
|
@ -16,9 +16,9 @@
|
|||||||
"strategy": 0
|
"strategy": 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"resource": "abc",
|
"resource": "http://www.taobao.com",
|
||||||
"controlBehavior": 0,
|
"controlBehavior": 0,
|
||||||
"count": 1,
|
"count": 0,
|
||||||
"grade": 1,
|
"grade": 1,
|
||||||
"limitApp": "default",
|
"limitApp": "default",
|
||||||
"strategy": 0
|
"strategy": 0
|
||||||
|
@ -58,8 +58,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 = uri.getScheme() + "://" + uri.getHost()
|
||||||
+ (uri.getPort() == -1 ? 80 : uri.getPort());
|
+ (uri.getPort() == -1 ? "" : ":" + uri.getPort());
|
||||||
String hostWithPathResource = hostResource + uri.getPath();
|
String hostWithPathResource = hostResource + uri.getPath();
|
||||||
Entry hostEntry = null, hostWithPathEntry = null;
|
Entry hostEntry = null, hostWithPathEntry = null;
|
||||||
ClientHttpResponse response;
|
ClientHttpResponse response;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user