新建对外接口

This commit is contained in:
2023-08-28 16:20:37 +08:00
parent 993712afb4
commit 5a68b0f29b
2 changed files with 56 additions and 0 deletions

View File

@@ -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);
}

View File

@@ -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<CsLogsFeignClient> {
@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);
}
};
}
}