Spring Cloud - Gateway exception handling through Gateway webflux programming

1. Webflux programming implements gateway exception handling


We know that if an exception occurs in a certain service, we can use @ControllerAdvice + @ExceptionHandler to unify exception handling. Even in a microservice architecture, we can also put the above unified exception handling into a public microservice, so which one If microservices are needed, just introduce the module directly. But what should we do if an exception occurs in our gateway microservice?

Proceed as follows:

  1. Create a class in the gateway microservice and add the @Configuration annotation to make it a configuration class.
  2. Implement the custom ErrorWebExceptionHandler interface and override the handle method.
  3. Implement unified exception handling in the handle method.

The code comments are as follows:

@Slf4j
@Configuration
public class GlobalExceptionConfig implements ErrorWebExceptionHandler {


    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 全局异常处理
     * @param exchange 交换器(request, response)
     * @param ex 出现异常时的异常对象
     * @return
     */
    @Override
    public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
        Map<String, String> result = new HashMap<>();
        //1.获取响应对象
        ServerHttpResponse response = exchange.getResponse();

        //2. response 是否结束(一般不用,可以用来处理多个异常的场景)
        if(response.isCommitted()) {
            return Mono.error(ex);
        }

        //3.设置响应头类型(JSON)
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);

        //4.设置响应状态码
        if (ex instanceof IllegalTokenException) {
            //是我们自定义的异常
            response.setStatusCode(HttpStatus.FORBIDDEN);
        } else {
            //不是我们自定义的异常,就通过 "服务器异常" 来处理
            response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        }

        //5.处理统一异常响应
        return response.writeWith(Mono.fromSupplier(() -> {
            DataBufferFactory bufferFactory = response.bufferFactory();
            result.put("msg", ex.getMessage());
            try {
                return bufferFactory.wrap(objectMapper.writeValueAsBytes(result));
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }));
    }

}
  • IllegalTokenException: It is our custom exception. Usually in the fourth step of the above code, we need to judge which custom exception ex belongs to us, and then process the corresponding status code.
  • Mono.fromSupplier(): It is a functional interface. The parameters are implemented here through a lamada expression (no parameters, the return value is a Mono) generic. The logic to be implemented here is to encapsulate the exception response.
  • bufferFactory.wrap: This is based on the webflux programming method, the parameters are in json format (converted through ObjectMapper), and a Mono object is obtained.

Then, if an exception is thrown in the gateway, as follows:

Guess you like

Origin blog.csdn.net/CYK_byte/article/details/134267726