diff --git a/algorithm/algorithm-api/src/main/java/com/njcn/algorithm/pojo/liteflow/LiteFlowFeignClient.java b/algorithm/algorithm-api/src/main/java/com/njcn/algorithm/pojo/liteflow/LiteFlowFeignClient.java new file mode 100644 index 0000000..6d25c44 --- /dev/null +++ b/algorithm/algorithm-api/src/main/java/com/njcn/algorithm/pojo/liteflow/LiteFlowFeignClient.java @@ -0,0 +1,43 @@ +package com.njcn.algorithm.pojo.liteflow; + + +import com.njcn.algorithm.pojo.bo.BaseParam; +import com.njcn.algorithm.pojo.liteflow.fallback.LiteFlowFeignClientFallbackFactory; +import com.njcn.common.pojo.constant.ServerInfo; +import io.swagger.annotations.ApiOperation; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +/** + * @author xuyang + */ +@FeignClient( + value = ServerInfo.DATA_PLATFORM,//对应模块名 + path = "/executor",//对应controller请求类 + fallbackFactory = LiteFlowFeignClientFallbackFactory.class//服务降级处理类 +) +public interface LiteFlowFeignClient { + + @ApiOperation("监测点算法执行链") + @PostMapping("/measurementPointExecutor") + void measurementPointExecutor(@RequestBody BaseParam baseParam); + + @ApiOperation("监测点算法执行链(按小时执行的任务)") + @PostMapping("/measurementPointExecutorByHour") + void measurementPointExecutorByHour(@RequestBody BaseParam baseParam); + + @ApiOperation("单位监测点算法执行链") + @PostMapping("/orgPointExecutor") + void orgPointExecutor(@RequestBody BaseParam baseParam); + + @ApiOperation("装置算法执行链") + @PostMapping("/deviceExecutor") + void deviceExecutor(@RequestBody BaseParam baseParam); + + @ApiOperation("变电站算法执行链") + @PostMapping("/substationExecutor") + void substationExecutor(@RequestBody BaseParam baseParam); + + +} diff --git a/algorithm/algorithm-api/src/main/java/com/njcn/algorithm/pojo/liteflow/fallback/LiteFlowFeignClientFallbackFactory.java b/algorithm/algorithm-api/src/main/java/com/njcn/algorithm/pojo/liteflow/fallback/LiteFlowFeignClientFallbackFactory.java new file mode 100644 index 0000000..fb3f5d2 --- /dev/null +++ b/algorithm/algorithm-api/src/main/java/com/njcn/algorithm/pojo/liteflow/fallback/LiteFlowFeignClientFallbackFactory.java @@ -0,0 +1,57 @@ +package com.njcn.algorithm.pojo.liteflow.fallback; + +import com.njcn.algorithm.pojo.bo.BaseParam; +import com.njcn.algorithm.pojo.liteflow.LiteFlowFeignClient; +import com.njcn.algorithm.pojo.util.PrepareEnumUtil; +import com.njcn.common.pojo.enums.response.CommonResponseEnum; +import com.njcn.common.pojo.exception.BusinessException; +import feign.hystrix.FallbackFactory; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class LiteFlowFeignClientFallbackFactory implements FallbackFactory { + @Override + public LiteFlowFeignClient create(Throwable throwable) { + Enum exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK; + if (throwable.getCause() instanceof BusinessException) { + BusinessException businessException = (BusinessException)throwable.getCause(); + exceptionEnum = PrepareEnumUtil.getExceptionEnum(businessException.getResult()); + } + + Enum finalExceptionEnum = exceptionEnum; + return new LiteFlowFeignClient() { + @Override + public void measurementPointExecutor(BaseParam baseParam) { + log.error("{}异常,降级处理,异常为:{}", "监测点算法执行链: ", throwable.toString()); + throw new BusinessException(finalExceptionEnum); + } + + @Override + public void measurementPointExecutorByHour(BaseParam baseParam) { + log.error("{}异常,降级处理,异常为:{}", "监测点算法(按小时)执行链: ", throwable.toString()); + throw new BusinessException(finalExceptionEnum); + } + + @Override + public void orgPointExecutor(BaseParam baseParam) { + log.error("{}异常,降级处理,异常为:{}", "单位监测点算法执行链: ", throwable.toString()); + throw new BusinessException(finalExceptionEnum); + } + + @Override + public void substationExecutor(BaseParam baseParam) { + log.error("{}异常,降级处理,异常为:{}", "变电站算法执行链: ", throwable.toString()); + throw new BusinessException(finalExceptionEnum); + } + + @Override + public void deviceExecutor(BaseParam baseParam) { + log.error("{}异常,降级处理,异常为:{}", "装置算法执行链: ", throwable.toString()); + throw new BusinessException(finalExceptionEnum); + } + + }; + } +} diff --git a/algorithm/algorithm-api/src/main/java/com/njcn/algorithm/pojo/util/PrepareEnumUtil.java b/algorithm/algorithm-api/src/main/java/com/njcn/algorithm/pojo/util/PrepareEnumUtil.java new file mode 100644 index 0000000..442af18 --- /dev/null +++ b/algorithm/algorithm-api/src/main/java/com/njcn/algorithm/pojo/util/PrepareEnumUtil.java @@ -0,0 +1,46 @@ +package com.njcn.algorithm.pojo.util; + +import cn.hutool.core.util.StrUtil; +import com.njcn.algorithm.pojo.enums.PrepareResponseEnum; +import com.njcn.common.pojo.enums.response.CommonResponseEnum; +import com.njcn.common.pojo.exception.BusinessException; +import com.njcn.common.pojo.response.HttpResult; +import com.njcn.common.utils.EnumUtils; + +import javax.validation.constraints.NotNull; +import java.util.Objects; + +/** + * @author hongawen + * @version 1.0.0 + * @date 2021年05月26日 17:17 + */ +public class PrepareEnumUtil { + + /** + * 获取PrepareResponseEnum实例 + */ + public static PrepareResponseEnum getPrepareResponseEnumByMessage(@NotNull Object value) { + PrepareResponseEnum prepareResponseEnum; + try { + String message = value.toString(); + if(message.indexOf(StrUtil.C_COMMA)>0){ + value = message.substring(message.indexOf(StrUtil.C_COMMA)+1); + } + prepareResponseEnum = EnumUtils.valueOf(PrepareResponseEnum.class, value, PrepareResponseEnum.class.getMethod(BusinessException.GET_MESSAGE_METHOD)); + return Objects.isNull(prepareResponseEnum) ? PrepareResponseEnum.PREPARE_INNER_ERROR : prepareResponseEnum; + } catch (NoSuchMethodException e) { + throw new BusinessException(CommonResponseEnum.INTERNAL_ERROR); + } + } + + + public static Enum getExceptionEnum(HttpResult result){ + //如果返回错误,且为内部错误,则直接抛出异常 + CommonResponseEnum commonResponseEnum = EnumUtils.getCommonResponseEnumByCode(result.getCode()); + if (commonResponseEnum == CommonResponseEnum.PREPARE_RESPONSE_ENUM) { + return getPrepareResponseEnumByMessage(result.getMessage()); + } + return commonResponseEnum; + } +}