diff --git a/cs-device/cs-device-api/src/main/java/com/njcn/csdevice/api/CsLogsFeignClient.java b/cs-device/cs-device-api/src/main/java/com/njcn/csdevice/api/CsLogsFeignClient.java new file mode 100644 index 0000000..e084707 --- /dev/null +++ b/cs-device/cs-device-api/src/main/java/com/njcn/csdevice/api/CsLogsFeignClient.java @@ -0,0 +1,20 @@ +package com.njcn.csdevice.api; + +import com.njcn.common.pojo.constant.ServerInfo; +import com.njcn.common.pojo.dto.DeviceLogDTO; +import com.njcn.common.pojo.response.HttpResult; +import com.njcn.csdevice.api.fallback.CsLogsClientFallbackFactory; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +/** + * @author xy + */ +@FeignClient(value = ServerInfo.CS_DEVICE_BOOT, path = "/cslog", fallbackFactory = CsLogsClientFallbackFactory.class,contextId = "cslog") +public interface CsLogsFeignClient { + + @PostMapping("/add") + HttpResult addUserLog(@RequestBody DeviceLogDTO deviceLogDTO); + +} diff --git a/cs-device/cs-device-api/src/main/java/com/njcn/csdevice/api/fallback/CsLogsClientFallbackFactory.java b/cs-device/cs-device-api/src/main/java/com/njcn/csdevice/api/fallback/CsLogsClientFallbackFactory.java new file mode 100644 index 0000000..cb9210e --- /dev/null +++ b/cs-device/cs-device-api/src/main/java/com/njcn/csdevice/api/fallback/CsLogsClientFallbackFactory.java @@ -0,0 +1,36 @@ +package com.njcn.csdevice.api.fallback; + +import com.njcn.common.pojo.dto.DeviceLogDTO; +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.csdevice.api.CsLogsFeignClient; +import feign.hystrix.FallbackFactory; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +/** + * @author xy + */ +@Slf4j +@Component +public class CsLogsClientFallbackFactory implements FallbackFactory { + @Override + public CsLogsFeignClient create(Throwable cause) { + //判断抛出异常是否为解码器抛出的业务异常 + Enum exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK; + if (cause.getCause() instanceof BusinessException) { + BusinessException businessException = (BusinessException) cause.getCause(); + } + Enum finalExceptionEnum = exceptionEnum; + return new CsLogsFeignClient() { + + @Override + public HttpResult addUserLog(DeviceLogDTO deviceLogDTO) { + log.error("{}异常,降级处理,异常为:{}","新增日志",cause.toString()); + throw new BusinessException(finalExceptionEnum); + } + + }; + } +}