Commit cb012f99 by Nepxion

重构Sentinel

parent 26833aeb
......@@ -8,6 +8,7 @@ package com.nepxion.discovery.plugin.example.service;
* @author Haojun Ren
* @version 1.0
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
......@@ -22,9 +23,10 @@ import com.nepxion.discovery.plugin.example.service.impl.MyDiscoveryEnabledStrat
import com.nepxion.discovery.plugin.example.service.impl.MyDiscoveryListener;
import com.nepxion.discovery.plugin.example.service.impl.MyLoadBalanceListener;
import com.nepxion.discovery.plugin.example.service.impl.MyRegisterListener;
import com.nepxion.discovery.plugin.example.service.impl.MySentinelExceptionHandler;
import com.nepxion.discovery.plugin.example.service.impl.MySentinelFlowRuleParser;
import com.nepxion.discovery.plugin.example.service.impl.MySubscriber;
import com.nepxion.discovery.plugin.example.service.sentinel.MyRestTemplateBlockHandler;
import com.nepxion.discovery.plugin.example.service.sentinel.MyRestTemplateFallbackHanlder;
import com.nepxion.discovery.plugin.example.service.sentinel.MySentinelFlowRuleParser;
import com.nepxion.discovery.plugin.strategy.service.aop.RestTemplateStrategyInterceptor;
@SpringBootApplication
......@@ -39,12 +41,9 @@ public class DiscoveryApplicationA1 {
new SpringApplicationBuilder(DiscoveryApplicationA1.class).run(args);
}
// @SentinelDataSource("spring.cloud.sentinel.datasource")
// protected ReadableDataSource dataSource;
@Bean
@LoadBalanced
@SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = MySentinelExceptionHandler.class)
@SentinelRestTemplate(blockHandler = "handleBlock", blockHandlerClass = MyRestTemplateBlockHandler.class, fallback = "handleFallback", fallbackClass = MyRestTemplateFallbackHanlder.class)
public RestTemplate restTemplate(@Autowired(required = false) RestTemplateStrategyInterceptor restTemplateStrategyInterceptor) {
RestTemplate restTemplate = new RestTemplate();
if (restTemplateStrategyInterceptor != null) {
......
......@@ -14,8 +14,11 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(value = "discovery-springcloud-example-a")
import com.nepxion.discovery.plugin.example.service.sentinel.MyAFeignFallbackHandler;
@FeignClient(value = "discovery-springcloud-example-a", fallback = MyAFeignFallbackHandler.class)
// Context-patch一旦被设置,在Feign也要带上context-path,外部Postman调用网关或者服务路径也要带context-path
// @FeignClient(value = "discovery-springcloud-example-a", path = "/nepxion")
// @FeignClient(value = "discovery-springcloud-example-a/nepxion")
public interface AFeign {
@RequestMapping(path = "/invoke", method = RequestMethod.POST)
......
......@@ -14,8 +14,11 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(value = "discovery-springcloud-example-b")
import com.nepxion.discovery.plugin.example.service.sentinel.MyBFeignFallbackHandler;
@FeignClient(value = "discovery-springcloud-example-b", fallback = MyBFeignFallbackHandler.class)
// Context-patch一旦被设置,在Feign也要带上context-path,外部Postman调用网关或者服务路径也要带context-path
// @FeignClient(value = "discovery-springcloud-example-b", path = "/nepxion")
// @FeignClient(value = "discovery-springcloud-example-b/nepxion")
public interface BFeign {
@RequestMapping(path = "/invoke", method = RequestMethod.POST)
......
......@@ -14,8 +14,11 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(value = "discovery-springcloud-example-c")
import com.nepxion.discovery.plugin.example.service.sentinel.MyCFeignFallbackHandler;
@FeignClient(value = "discovery-springcloud-example-c", fallback = MyCFeignFallbackHandler.class)
// Context-patch一旦被设置,在Feign也要带上context-path,外部Postman调用网关或者服务路径也要带context-path
// @FeignClient(value = "discovery-springcloud-example-c", path = "/nepxion")
// @FeignClient(value = "discovery-springcloud-example-c/nepxion")
public interface CFeign {
@RequestMapping(path = "/invoke", method = RequestMethod.POST)
......
......@@ -20,6 +20,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
@RestController
......@@ -31,7 +32,7 @@ public class ARestImpl extends AbstractRestImpl {
private RestTemplate restTemplate;
@RequestMapping(path = "/rest", method = RequestMethod.POST)
@SentinelResource("sentinel-resource")
@SentinelResource(value = "sentinel-resource", blockHandler = "handleBlock", fallback = "handleFallback")
public String rest(@RequestBody String value) {
value = doRest(value);
value = restTemplate.postForEntity("http://discovery-springcloud-example-b/rest", value, String.class).getBody();
......@@ -45,4 +46,20 @@ public class ARestImpl extends AbstractRestImpl {
public String test(@RequestBody String value) {
return value;
}
public String handleBlock(String value, BlockException e) {
LOG.info("Value={}", value);
LOG.info("Sentinel AServer Block Causes");
LOG.error("Sentinel AServer Block Exception", e);
LOG.info("Sentinel Rule Limit App={}", e.getRuleLimitApp());
return "Sentinel AServer Block Causes";
}
public String handleFallback(String value) {
LOG.info("Value={}", value);
LOG.info("Sentinel AServer Fallback Causes");
return "Sentinel AServer Fallback Causes";
}
}
\ No newline at end of file
......@@ -20,6 +20,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
@RestController
......@@ -31,7 +32,7 @@ public class BRestImpl extends AbstractRestImpl {
private RestTemplate restTemplate;
@RequestMapping(path = "/rest", method = RequestMethod.POST)
@SentinelResource("sentinel-resource")
@SentinelResource(value = "sentinel-resource", blockHandler = "handleBlock", fallback = "handleFallback")
public String rest(@RequestBody String value) {
value = doRest(value);
value = restTemplate.postForEntity("http://discovery-springcloud-example-c/rest", value, String.class).getBody();
......@@ -45,4 +46,20 @@ public class BRestImpl extends AbstractRestImpl {
public String test(@RequestBody String value) {
return value;
}
public String handleBlock(String value, BlockException e) {
LOG.info("Value={}", value);
LOG.info("Sentinel BServer Block Causes");
LOG.error("Sentinel BServer Block Exception", e);
LOG.info("Sentinel Rule Limit App={}", e.getRuleLimitApp());
return "Sentinel BServer Block Causes";
}
public String handleFallback(String value) {
LOG.info("Value={}", value);
LOG.info("Sentinel BServer Fallback Causes");
return "Sentinel BServer Fallback Causes";
}
}
\ No newline at end of file
......@@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
@RestController
......@@ -26,7 +27,7 @@ public class CRestImpl extends AbstractRestImpl {
private static final Logger LOG = LoggerFactory.getLogger(CRestImpl.class);
@RequestMapping(path = "/rest", method = RequestMethod.POST)
@SentinelResource("sentinel-resource")
@SentinelResource(value = "sentinel-resource", blockHandler = "handleBlock", fallback = "handleFallback")
public String rest(@RequestBody String value) {
value = doRest(value);
......@@ -39,4 +40,20 @@ public class CRestImpl extends AbstractRestImpl {
public String test(@RequestBody String value) {
return value;
}
public String handleBlock(String value, BlockException e) {
LOG.info("Value={}", value);
LOG.info("Sentinel CServer Block Causes");
LOG.error("Sentinel CServer Block Exception", e);
LOG.info("Sentinel Rule Limit App={}", e.getRuleLimitApp());
return "Sentinel CServer Block Causes";
}
public String handleFallback(String value) {
LOG.info("Value={}", value);
LOG.info("Sentinel CServer Fallback Causes");
return "Sentinel CServer Fallback Causes";
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.example.service.sentinel;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.nepxion.discovery.plugin.example.service.feign.AFeign;
public class MyAFeignFallbackHandler implements AFeign {
private static final Logger LOG = LoggerFactory.getLogger(MyAFeignFallbackHandler.class);
@Override
public String invoke(String value) {
LOG.info("Value={}", value);
return "Sentinel AFeign Client Fallback Causes";
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.example.service.sentinel;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.nepxion.discovery.plugin.example.service.feign.BFeign;
public class MyBFeignFallbackHandler implements BFeign {
private static final Logger LOG = LoggerFactory.getLogger(MyBFeignFallbackHandler.class);
@Override
public String invoke(String value) {
LOG.info("Value={}", value);
return "Sentinel BFeign Client Fallback Causes";
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.example.service.sentinel;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.nepxion.discovery.plugin.example.service.feign.CFeign;
public class MyCFeignFallbackHandler implements CFeign {
private static final Logger LOG = LoggerFactory.getLogger(MyCFeignFallbackHandler.class);
@Override
public String invoke(String value) {
LOG.info("Value={}", value);
return "Sentinel CFeign Client Fallback Causes";
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.example.service.impl;
package com.nepxion.discovery.plugin.example.service.sentinel;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -14,10 +14,15 @@ import org.slf4j.LoggerFactory;
import com.alibaba.csp.sentinel.slots.block.BlockException;
public class MySentinelExceptionHandler {
private static final Logger LOG = LoggerFactory.getLogger(MySentinelExceptionHandler.class);
public class MyRestTemplateBlockHandler {
private static final Logger LOG = LoggerFactory.getLogger(MyRestTemplateBlockHandler.class);
public static void handleException(BlockException e) {
LOG.error("Sentinel exception causes", e);
public static String handleBlock(String value, BlockException e) {
LOG.info("Value={}", value);
LOG.info("Sentinel RestTemplate Client Block Causes");
LOG.error("Sentinel RestTemplate Client Block Exception", e);
LOG.info("Sentinel Rule Limit App={}", e.getRuleLimitApp());
return "Sentinel RestTemplate Client Block Causes";
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.example.service.sentinel;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyRestTemplateFallbackHanlder {
private static final Logger LOG = LoggerFactory.getLogger(MyRestTemplateFallbackHanlder.class);
public static void hanldleFallback() {
LOG.info("Sentinel RestTemplate Client Fallback Causes");
}
}
\ No newline at end of file
......@@ -20,8 +20,5 @@ spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.version=1.0
spring.cloud.nacos.discovery.metadata.region=dev
# Sentinel config
spring.cloud.sentinel.transport.port=4100
# Admin config
management.server.port=5100
\ No newline at end of file
......@@ -20,8 +20,5 @@ spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.version=1.1
spring.cloud.nacos.discovery.metadata.region=qa
# Sentinel config
spring.cloud.sentinel.transport.port=4101
# Admin config
management.server.port=5101
\ No newline at end of file
......@@ -20,8 +20,5 @@ spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.version=1.0
spring.cloud.nacos.discovery.metadata.region=dev
# Sentinel config
spring.cloud.sentinel.transport.port=4200
# Admin config
management.server.port=5200
\ No newline at end of file
......@@ -20,8 +20,5 @@ spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.version=1.1
spring.cloud.nacos.discovery.metadata.region=qa
# Sentinel config
spring.cloud.sentinel.transport.port=4201
# Admin config
management.server.port=5201
\ No newline at end of file
......@@ -20,8 +20,5 @@ spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.version=1.0
spring.cloud.nacos.discovery.metadata.region=dev
# Sentinel config
spring.cloud.sentinel.transport.port=4300
# Admin config
management.server.port=5300
\ No newline at end of file
......@@ -20,8 +20,5 @@ spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.version=1.1
spring.cloud.nacos.discovery.metadata.region=qa
# Sentinel config
spring.cloud.sentinel.transport.port=4301
# Admin config
management.server.port=5301
\ No newline at end of file
......@@ -20,8 +20,5 @@ spring.cloud.nacos.discovery.metadata.group=example-service-group
spring.cloud.nacos.discovery.metadata.version=1.2
spring.cloud.nacos.discovery.metadata.region=qa
# Sentinel config
spring.cloud.sentinel.transport.port=4302
# Admin config
management.server.port=5302
\ No newline at end of file
......@@ -45,14 +45,19 @@ spring.redis.pool.min-idle=0
# Sentinel config
spring.cloud.sentinel.transport.dashboard=localhost:8075
# spring.cloud.sentinel.datasource.type=file
# spring.cloud.sentinel.datasource.recommendRefreshMs=3000
# spring.cloud.sentinel.datasource.bufSize=4056196
# spring.cloud.sentinel.datasource.charset=utf-8
# spring.cloud.sentinel.datasource.converter=mySentinelFlowRuleParser
# spring.cloud.sentinel.datasource.file=sentinel-rule.json
# spring.cloud.sentinel.datasource.file=E://sentinel-rule.json
spring.cloud.sentinel.eager=true
# 本地规则方式和远程规则方式不能同时并存,必须两选一。使用时候必须注释掉其中之一,生成环境必须使用远程规则方式
# 本地规则方式配置
spring.cloud.sentinel.datasource.ds1.file.file=classpath:sentinel.json
spring.cloud.sentinel.datasource.ds1.file.data-type=json
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
# 远程规则方式配置
# spring.cloud.sentinel.datasource.ds2.apollo.namespace-name=application
# spring.cloud.sentinel.datasource.ds2.apollo.flow-rules-key=example-service-group-${spring.application.name}-sentinel-flow
# spring.cloud.sentinel.datasource.ds2.apollo.rule-type=flow
# Feign和RestTemplate熔断降级限流开关
feign.sentinel.enabled=true
resttemplate.sentinel.enabled=true
# Admin config
# 该项只对Consul有效,而且必须配置在bootstrap.properties里,配置在application.properties无效
......
......@@ -2,7 +2,7 @@
{
"resource": "sentinel-resource",
"controlBehavior": 0,
"count": 5.0,
"count": 1,
"grade": 1,
"limitApp": "default",
"strategy": 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment