0af3ce8ad4e5516f06ab28724b9deb30ef52ce70.svn-base 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. package org.jeecg.handler;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.http.MediaType;
  5. import org.springframework.stereotype.Component;
  6. import org.springframework.web.reactive.function.BodyInserters;
  7. import org.springframework.web.reactive.function.server.HandlerFunction;
  8. import org.springframework.web.reactive.function.server.ServerRequest;
  9. import org.springframework.web.reactive.function.server.ServerResponse;
  10. import reactor.core.publisher.Mono;
  11. import java.util.Optional;
  12. import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR;
  13. /**
  14. * @author scott
  15. * @date 2020/05/26
  16. * Hystrix 降级处理
  17. */
  18. @Slf4j
  19. @Component
  20. public class HystrixFallbackHandler implements HandlerFunction<ServerResponse> {
  21. @Override
  22. public Mono<ServerResponse> handle(ServerRequest serverRequest) {
  23. Optional<Object> originalUris = serverRequest.attribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
  24. originalUris.ifPresent(originalUri -> log.error("网关执行请求:{}失败,hystrix服务降级处理", originalUri));
  25. return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
  26. .header("Content-Type","text/plain; charset=utf-8").body(BodyInserters.fromObject("访问超时,请稍后再试"));
  27. }
  28. }