官网:https://sentinelguard.io/zh-cn/docs/circuit-breaking.html
熔断降级:服务由于响应慢、异常等原因触发熔断策略后,快速失败,避免线程堆积造成服务雪崩(熔断降级通常在调用端配置)
熔断策略
# 慢调用比例:SLOW_REQUEST_RATIO,以慢调用比例作为阈值
慢调用:如果一个请求的响应时间需要大于设置的慢调用响应时间(RT),该请求统计为慢调用;
熔断触发:单位统计时长(statIntervalMs)内请求数目大于设置的最小请求数目,
并且慢调用的比例大于阈值,在熔断时长内自动触发熔断(调用方法时,抛出DegradeException)
熔断恢复:在熔断时长后,熔断器进入探测恢复状态(half-open状态),
如果接下来的请求响应时间 < 设置的慢调用响应时间,则结束熔断
如果接下来的请求响应时间 > 设置的慢调用响应时间,则继续在熔断时长内保持熔断
# 异常比例 :ERROR_RATIO,以请求异常比例作为阀值,阈值范围是[0.0, 1.0]
异常请求:请求调用抛出异常,则该请求统计为异常请求
熔断触发:单位统计时长(statIntervalMs)内请求数目 > 设置的最小请求数目,
并且异常的比例 > 阈值,在熔断时长内自动被熔断
熔断恢复:在熔断时长后,熔断器进入探测恢复状态(HALF-OPEN 状态),
如果接下来的一个请求执行成功,则结束熔断;
如果请求执行失败,则继续在熔断时长内保持熔断
# 异常数:ERROR_COUNT,以异常请求数作为阀值
异常请求:请求调用抛出异常,则该请求统计为异常请求
熔断触发:单位统计时长(statIntervalMs)内请求数目 > 设置的最小请求数目,
并且异常请求 > 设置的异常请求数阀值,在熔断时长内自动被熔断
熔断恢复:在熔断时长后,熔断器进入探测恢复状态(HALF-OPEN 状态),
如果接下来的一个请求执行成功,则结束熔断;
如果请求执行失败,则继续在熔断时长内保持熔断
DegradeRule:熔断降级规则
public class DegradeRule extends AbstractRule {
private int grade = 0; //熔断策略,支持慢调用比例(默认)、异常比例、异常数策略
private double count; //限流阀值
private int timeWindow; //熔断时间窗口,单位为秒
private int minRequestAmount = 5; //最小请求数
private double slowRatioThreshold = 1.0D; //慢调用比例阈值
private int statIntervalMs = 1000; //统计时间窗口,默认1s
public DegradeRule() {
}
AbstractRule
public abstract class AbstractRule implements Rule {
private String resource; //限流资源
private String limitApp; //调用来源、default、other
public AbstractRule() {
}
DegradeSlot
@SpiOrder(-1000)
public class DegradeSlot extends AbstractLinkedProcessorSlot {
public DegradeSlot() {
}
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) throws Throwable {
this.performChecking(context, resourceWrapper); //检查熔断规则,如果触发熔断,直接抛出DegradeException异常
this.fireEntry(context, resourceWrapper, node, count, prioritized, args);
//追星后续限流操作
}
void performChecking(Context context, ResourceWrapper r) throws BlockException {
List circuitBreakers = DegradeRuleManager.getCircuitBreakers(r.getName());
//获取资源相关的断路器
if (circuitBreakers != null && !circuitBreakers.isEmpty()) {
Iterator var4 = circuitBreakers.iterator();
CircuitBreaker cb;
do {
if (!var4.hasNext()) {
return;
}
cb = (CircuitBreaker)var4.next();
} while(cb.tryPass(context)); //熔断窗口到期,检查单词请求是否可以通过
throw new DegradeException(cb.getRule().getLimitApp(), cb.getRule());
//熔断检查未通过,抛出DegradeException异常
}
}
public void exit(Context context, ResourceWrapper r, int count, Object... args) {
Entry curEntry = context.getCurEntry();
if (curEntry.getBlockError() != null) {
this.fireExit(context, r, count, args);
} else {
List circuitBreakers = DegradeRuleManager.getCircuitBreakers(r.getName());
if (circuitBreakers != null && !circuitBreakers.isEmpty()) {
if (curEntry.getBlockError() == null) {
Iterator var7 = circuitBreakers.iterator();
while(var7.hasNext()) {
CircuitBreaker circuitBreaker = (CircuitBreaker)var7.next();
circuitBreaker.onRequestComplete(context);
}
}
this.fireExit(context, r, count, args);
} else {
this.fireExit(context, r, count, args);
}
}
}
}
CircuitBreaker
public interface CircuitBreaker {
DegradeRule getRule();
boolean tryPass(Context var1);
CircuitBreaker.State currentState();
void onRequestComplete(Context var1);
public static enum State {
OPEN,
HALF_OPEN,
CLOSED;
private State() {
}
}
}
AbstractCircuitBreaker
public abstract class AbstractCircuitBreaker implements CircuitBreaker {
protected final DegradeRule rule;
protected final int recoveryTimeoutMs;
private final EventObserverRegistry observerRegistry;
protected final AtomicReference currentState;
protected volatile long nextRetryTimestamp;
public AbstractCircuitBreaker(DegradeRule rule) {
this(rule, EventObserverRegistry.getInstance());
}
AbstractCircuitBreaker(DegradeRule rule, EventObserverRegistry observerRegistry) {
this.currentState = new AtomicReference(State.CLOSED);
AssertUtil.notNull(observerRegistry, "observerRegistry cannot be null");
if (!DegradeRuleManager.isValidRule(rule)) {
throw new IllegalArgumentException("Invalid DegradeRule: " + rule);
} else {
this.observerRegistry = observerRegistry;
this.rule = rule;
this.recoveryTimeoutMs = rule.getTimeWindow() * 1000;
}
}
public DegradeRule getRule() {
return this.rule;
}
public State currentState() {
return (State)this.currentState.get();
}
public boolean tryPass(Context context) { //熔断检查
if (this.currentState.get() == State.CLOSED) { //断路器为closed,返回true
return true;
} else if (this.currentState.get() != State.OPEN) { //断路器状态不为open,返回false
return false;
} else {
return this.retryTimeoutArrived() && this.fromOpenToHalfOpen(context);
} //判断是否是请求重试时间,如果不是,直接返回false
//如果是,断路器状态设置为HalfOpen,异步执行单次请求,
//如果单次请求不通过,断路器状态切换为open,
//如果单次请求通过,返回true,可执行后续限流操作
}
abstract void resetStat();
protected boolean retryTimeoutArrived() {
return TimeUtil.currentTimeMillis() >= this.nextRetryTimestamp;
} //判断是否为请求重试时间
protected void updateNextRetryTimestamp() {
this.nextRetryTimestamp = TimeUtil.currentTimeMillis() + (long)this.recoveryTimeoutMs;
} //下次重试时间:当前时间 + recoveryTimeoutMs(即rule.getTimeWindow() * 1000)
protected boolean fromCloseToOpen(double snapshotValue) {
State prev = State.CLOSED;
if (this.currentState.compareAndSet(prev, State.OPEN)) {
this.updateNextRetryTimestamp();
this.notifyObservers(prev, State.OPEN, snapshotValue);
return true;
} else {
return false;
}
}
protected boolean fromOpenToHalfOpen(Context context) {
if (this.currentState.compareAndSet(State.OPEN, State.HALF_OPEN)) {
//使用cas将断路器状态设置为HALF_OPEN
this.notifyObservers(State.OPEN, State.HALF_OPEN, (Double)null);
Entry entry = context.getCurEntry();
entry.whenTerminate(new BiConsumer() {
public void accept(Context context, Entry entry) {
if (entry.getBlockError() != null) {
//请求出现异常,状态设置为open
AbstractCircuitBreaker.this.currentState.compareAndSet(State.HALF_OPEN, State.OPEN);
AbstractCircuitBreaker.this.notifyObservers(State.HALF_OPEN, State.OPEN, 1.0D);
}
}
});
return true; //如果没有异常,返回true
} else {
return false;
}
}
private void notifyObservers(State prevState, State newState, Double snapshotValue) {
Iterator var4 = this.observerRegistry.getStateChangeObservers().iterator();
while(var4.hasNext()) {
CircuitBreakerStateChangeObserver observer = (CircuitBreakerStateChangeObserver)var4.next();
observer.onStateChange(prevState, newState, this.rule, snapshotValue);
}
}
protected boolean fromHalfOpenToOpen(double snapshotValue) {
if (this.currentState.compareAndSet(State.HALF_OPEN, State.OPEN)) {
this.updateNextRetryTimestamp();
this.notifyObservers(State.HALF_OPEN, State.OPEN, snapshotValue);
return true;
} else {
return false;
}
}
protected boolean fromHalfOpenToClose() {
if (this.currentState.compareAndSet(State.HALF_OPEN, State.CLOSED)) {
this.resetStat();
this.notifyObservers(State.HALF_OPEN, State.CLOSED, (Double)null);
return true;
} else {
return false;
}
}
protected void transformToOpen(double triggerValue) {
State cs = (State)this.currentState.get();
switch(cs) {
case CLOSED:
this.fromCloseToOpen(triggerValue);
break;
case HALF_OPEN:
this.fromHalfOpenToOpen(triggerValue);
}
}
}
**********
服务端
application.yml
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
dubbo:
#registry:
# address: spring-cloud://localhost:8848
protocol:
name: dubbo
port: -1
HelloService
public interface HelloService {
String hello();
}
HelloServiceImpl
@DubboService
public class HelloServiceImpl implements HelloService {
@Override
public String hello() {
throw new RpcException("出错了");
}
}
DemoApplication
@EnableDubbo //开启dubbo
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
**********
消费端
application.yml
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
dubbo:
#registry:
# address: spring-cloud://localhost:8848
protocol:
name: dubbo
port: -1
CustomDegradeRule
public class CustomDegradeRule implements InitFunc {
@Override
public void init() throws Exception {
DegradeRule degradeRule = new DegradeRule("com.example.demo.service.HelloService:hello()");
degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
.setCount(1).setMinRequestAmount(5)
.setTimeWindow(10);
DegradeRuleManager.loadRules(Collections.singletonList(degradeRule));
}
}
HelloService
public interface HelloService {
String hello();
}
HelloController
@RestController
public class HelloController {
@DubboReference
//@DubboReference(mock = "true")
private HelloService helloService;
@RequestMapping("/hello")
public String hello(){
System.out.println(helloService.hello());
return "success";
}
}
DemoApplication
@EnableDubbo
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
META-INF/services/com.alibaba.csp.sentinel.init.InitFunc
com.example.demo.config.CustomDegradeRule
**********
jmeter 测试
线程组、http请求
查看结果树
2022-04-06 17:08:19.155 WARN 4870 --- [client.listener] a.c.d.m.r.DubboServiceMetadataRepository : Current application will subscribe all services(size:2) in registry, a lot of memory and CPU cycles may be used, thus it's strongly recommend you using the externalized property 'dubbo.cloud.subscribed-services' to specify the services
2022-04-06 17:08:21.411 INFO 4870 --- [erverWorker-4-1] o.a.d.r.t.netty4.NettyServerHandler : [DUBBO] The connection of /192.168.5.9:54897 -> /192.168.5.9:20881 is established., dubbo version: 2.7.8, current host: 192.168.5.9
2022-04-06 17:08:23.509 INFO 4870 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-04-06 17:08:23.509 INFO 4870 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-04-06 17:08:23.523 INFO 4870 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 13 ms
2022-04-06 17:08:23.550 ERROR 4870 --- [nio-8081-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.dubbo.rpc.RpcException: 出错了] with root cause
org.apache.dubbo.rpc.RpcException: 出错了
at com.example.demo.service.impl.HelloServiceImpl.hello(HelloServiceImpl.java:12) ~[na:na]
at org.apache.dubbo.common.bytecode.Wrapper3.invokeMethod(Wrapper3.java) ~[na:na]
at org.apache.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:47) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:84) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:56) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:52) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at com.alibaba.csp.sentinel.adapter.dubbo.SentinelDubboProviderFilter.invoke(SentinelDubboProviderFilter.java:80) ~[sentinel-apache-dubbo-adapter-1.8.0.jar:na]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:89) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:46) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:77) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:129) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:152) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:41) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:145) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:100) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:175) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:57) ~[dubbo-2.7.8.jar:2.7.8]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_211]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_211]
2022-04-06 17:08:23.591 ERROR 4870 --- [nio-8081-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.dubbo.rpc.RpcException: 出错了] with root cause
org.apache.dubbo.rpc.RpcException: 出错了
at com.example.demo.service.impl.HelloServiceImpl.hello(HelloServiceImpl.java:12) ~[na:na]
at org.apache.dubbo.common.bytecode.Wrapper3.invokeMethod(Wrapper3.java) ~[na:na]
at org.apache.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:47) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:84) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:56) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:52) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at com.alibaba.csp.sentinel.adapter.dubbo.SentinelDubboProviderFilter.invoke(SentinelDubboProviderFilter.java:80) ~[sentinel-apache-dubbo-adapter-1.8.0.jar:na]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:89) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:46) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:77) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:129) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:152) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:41) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:145) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:100) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:175) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:57) ~[dubbo-2.7.8.jar:2.7.8]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_211]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_211]
2022-04-06 17:08:23.602 ERROR 4870 --- [nio-8081-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.dubbo.rpc.RpcException: 出错了] with root cause
org.apache.dubbo.rpc.RpcException: 出错了
at com.example.demo.service.impl.HelloServiceImpl.hello(HelloServiceImpl.java:12) ~[na:na]
at org.apache.dubbo.common.bytecode.Wrapper3.invokeMethod(Wrapper3.java) ~[na:na]
at org.apache.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:47) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:84) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:56) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:52) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at com.alibaba.csp.sentinel.adapter.dubbo.SentinelDubboProviderFilter.invoke(SentinelDubboProviderFilter.java:80) ~[sentinel-apache-dubbo-adapter-1.8.0.jar:na]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:89) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:46) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:77) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:129) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:152) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:41) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:145) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:100) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:175) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:57) ~[dubbo-2.7.8.jar:2.7.8]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_211]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_211]
2022-04-06 17:08:23.610 ERROR 4870 --- [nio-8081-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.dubbo.rpc.RpcException: 出错了] with root cause
org.apache.dubbo.rpc.RpcException: 出错了
at com.example.demo.service.impl.HelloServiceImpl.hello(HelloServiceImpl.java:12) ~[na:na]
at org.apache.dubbo.common.bytecode.Wrapper3.invokeMethod(Wrapper3.java) ~[na:na]
at org.apache.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:47) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:84) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:56) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:52) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at com.alibaba.csp.sentinel.adapter.dubbo.SentinelDubboProviderFilter.invoke(SentinelDubboProviderFilter.java:80) ~[sentinel-apache-dubbo-adapter-1.8.0.jar:na]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:89) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:46) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:77) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:129) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:152) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:41) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:145) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:100) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:175) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:57) ~[dubbo-2.7.8.jar:2.7.8]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_211]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_211]
2022-04-06 17:08:23.618 ERROR 4870 --- [nio-8081-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.dubbo.rpc.RpcException: 出错了] with root cause
org.apache.dubbo.rpc.RpcException: 出错了
at com.example.demo.service.impl.HelloServiceImpl.hello(HelloServiceImpl.java:12) ~[na:na]
at org.apache.dubbo.common.bytecode.Wrapper3.invokeMethod(Wrapper3.java) ~[na:na]
at org.apache.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:47) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:84) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:56) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:52) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at com.alibaba.csp.sentinel.adapter.dubbo.SentinelDubboProviderFilter.invoke(SentinelDubboProviderFilter.java:80) ~[sentinel-apache-dubbo-adapter-1.8.0.jar:na]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:89) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:46) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:77) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:129) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:152) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:41) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:145) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:100) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:175) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:57) ~[dubbo-2.7.8.jar:2.7.8]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_211]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_211]
2022-04-06 17:08:23.640 ERROR 4870 --- [nio-8081-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: SentinelBlockException: DegradeException] with root cause
java.lang.RuntimeException: SentinelBlockException: DegradeException
at com.alibaba.csp.sentinel.slots.block.BlockException.block(BlockException:0) ~[sentinel-core-1.8.0.jar:1.8.0]
2022-04-06 17:08:23.644 ERROR 4870 --- [nio-8081-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: SentinelBlockException: DegradeException] with root cause
java.lang.RuntimeException: SentinelBlockException: DegradeException
at com.alibaba.csp.sentinel.slots.block.BlockException.block(BlockException:0) ~[sentinel-core-1.8.0.jar:1.8.0]
2022-04-06 17:08:23.649 ERROR 4870 --- [nio-8081-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: SentinelBlockException: DegradeException] with root cause
java.lang.RuntimeException: SentinelBlockException: DegradeException
at com.alibaba.csp.sentinel.slots.block.BlockException.block(BlockException:0) ~[sentinel-core-1.8.0.jar:1.8.0]
2022-04-06 17:08:23.653 ERROR 4870 --- [nio-8081-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: SentinelBlockException: DegradeException] with root cause
java.lang.RuntimeException: SentinelBlockException: DegradeException
at com.alibaba.csp.sentinel.slots.block.BlockException.block(BlockException:0) ~[sentinel-core-1.8.0.jar:1.8.0]
2022-04-06 17:08:23.658 ERROR 4870 --- [io-8081-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: SentinelBlockException: DegradeException] with root cause
java.lang.RuntimeException: SentinelBlockException: DegradeException
at com.alibaba.csp.sentinel.slots.block.BlockException.block(BlockException:0) ~[sentinel-core-1.8.0.jar:1.8.0]
说明:前5次输出RpcException,后5次由于触发熔断降级,抛出DegradeException
**********
dubbo mock降级
HelloServiceMock
public class HelloServiceMock implements HelloService {
@Override
public String hello() {
return "调用出错,本地降级";
}
}
HelloController
@RestController
public class HelloController {
//@DubboReference
@DubboReference(mock = "true")
private HelloService helloService;
@RequestMapping("/hello")
public String hello(){
System.out.println(helloService.hello());
return "success";
}
}
查看结果树
# 消费端控制台输出
2022-04-06 17:04:08.691 INFO 4800 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 3.907 seconds (JVM running for 4.626)
2022-04-06 17:04:09.611 WARN 4800 --- [client.listener] a.c.d.m.r.DubboServiceMetadataRepository : Current application will subscribe all services(size:2) in registry, a lot of memory and CPU cycles may be used, thus it's strongly recommend you using the externalized property 'dubbo.cloud.subscribed-services' to specify the services
2022-04-06 17:04:11.063 INFO 4800 --- [erverWorker-4-1] o.a.d.r.t.netty4.NettyServerHandler : [DUBBO] The connection of /192.168.5.9:54787 -> /192.168.5.9:20881 is established., dubbo version: 2.7.8, current host: 192.168.5.9
2022-04-06 17:04:11.654 INFO 4800 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-04-06 17:04:11.654 INFO 4800 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-04-06 17:04:11.668 INFO 4800 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 14 ms
调用出错,本地降级
调用出错,本地降级
调用出错,本地降级
调用出错,本地降级
调用出错,本地降级
2022-04-06 17:04:11.777 ERROR 4800 --- [nio-8081-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: SentinelBlockException: DegradeException] with root cause
java.lang.RuntimeException: SentinelBlockException: DegradeException
at com.alibaba.csp.sentinel.slots.block.BlockException.block(BlockException:0) ~[sentinel-core-1.8.0.jar:1.8.0]
2022-04-06 17:04:11.795 ERROR 4800 --- [nio-8081-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: SentinelBlockException: DegradeException] with root cause
java.lang.RuntimeException: SentinelBlockException: DegradeException
at com.alibaba.csp.sentinel.slots.block.BlockException.block(BlockException:0) ~[sentinel-core-1.8.0.jar:1.8.0]
2022-04-06 17:04:11.800 ERROR 4800 --- [nio-8081-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: SentinelBlockException: DegradeException] with root cause
java.lang.RuntimeException: SentinelBlockException: DegradeException
at com.alibaba.csp.sentinel.slots.block.BlockException.block(BlockException:0) ~[sentinel-core-1.8.0.jar:1.8.0]
2022-04-06 17:04:11.805 ERROR 4800 --- [nio-8081-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: SentinelBlockException: DegradeException] with root cause
java.lang.RuntimeException: SentinelBlockException: DegradeException
at com.alibaba.csp.sentinel.slots.block.BlockException.block(BlockException:0) ~[sentinel-core-1.8.0.jar:1.8.0]
2022-04-06 17:04:11.809 ERROR 4800 --- [nio-8081-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: SentinelBlockException: DegradeException] with root cause
java.lang.RuntimeException: SentinelBlockException: DegradeException
at com.alibaba.csp.sentinel.slots.block.BlockException.block(BlockException:0) ~[sentinel-core-1.8.0.jar:1.8.0]
说明:前5次请求服务端抛出RpcExeption,消费端使用mock降级;后5次由于消费端触发sentinel熔断,直接抛出DegradeException