Skip to content

Commit

Permalink
增加sentinel限流相关异常处理,调整统一异常处理优先级
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoutaoo committed Feb 4, 2024
1 parent 84e96bd commit 3f23d5d
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import io.github.opensabre.common.core.entity.vo.Result;
import io.github.opensabre.common.core.exception.SystemErrorType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@Order(100)
@RestControllerAdvice
public class PersistenceExceptionHandlerAdvice {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.opensabre.rpc.sentinel.config;

import io.github.opensabre.rpc.sentinel.exception.SentinelExceptionHandlerAdvice;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Import;

@AutoConfiguration
@Import({SentinelExceptionHandlerAdvice.class})
public class OpensabreSentinelConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.github.opensabre.rpc.sentinel.exception;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import io.github.opensabre.common.core.entity.vo.Result;
import io.github.opensabre.common.core.exception.SystemErrorType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
*
*/
@Slf4j
@RestControllerAdvice
@Order(100)
public class SentinelExceptionHandlerAdvice {

@ExceptionHandler(BlockException.class)
public Result blockException(BlockException e) {
log.error("block exception:{}", e.getMessage());
return Result.fail(SystemErrorType.SYSTEM_BUSY);
}

@ExceptionHandler(FlowException.class)
public Result flowException(FlowException e) {
log.error("flow exception:{}", e.getMessage());
return Result.fail(SystemErrorType.SYSTEM_BUSY);
}

@ExceptionHandler(DegradeException.class)
public Result degradeException(DegradeException e) {
log.error("degrade exception:{}", e.getMessage());
return Result.fail(SystemErrorType.SYSTEM_BUSY);
}

@ExceptionHandler(ParamFlowException.class)
public Result paramFlowException(ParamFlowException e) {
log.error("param flow exception:{}", e.getMessage());
return Result.fail(SystemErrorType.SYSTEM_BUSY);
}

@ExceptionHandler(SystemBlockException.class)
public Result systemBlockException(SystemBlockException e) {
log.error("system block exception:{}", e.getMessage());
return Result.fail(SystemErrorType.SYSTEM_BUSY);
}

@ExceptionHandler(AuthorityException.class)
public Result authorityException(AuthorityException e) {
log.error("authority exception:{}", e.getMessage());
return Result.fail(SystemErrorType.SYSTEM_BUSY);
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
io.github.opensabre.rpc.openfeign.config.OpensabreFeignConfig
io.github.opensabre.rpc.openfeign.config.OpensabreLoadBalancerConfig
io.github.opensabre.rpc.openfeign.config.OpensabreLoadBalancerConfig
io.github.opensabre.rpc.sentinel.config.OpensabreSentinelConfig
4 changes: 3 additions & 1 deletion opensabre-starter-rpc/src/main/resources/opensabre-rpc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
feign:
circuitbreaker:
enabled: true
sentinel:
enabled: true
httpclient:
Expand All @@ -14,7 +16,7 @@ feign:
default:
connectTimeout: 5000
readTimeout: 10000
loggerLevel: full
loggerLevel: FULL
spring:
cloud:
sentinel:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.opensabre.common.core.exception.BaseException;
import io.github.opensabre.common.core.exception.SystemErrorType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
Expand All @@ -18,6 +19,7 @@
* 默认全局异常处理类
*/
@Slf4j
@Order
@RestControllerAdvice
public class DefaultGlobalExceptionHandlerAdvice {

Expand Down

0 comments on commit 3f23d5d

Please sign in to comment.