package com.gsafety.framework.api;
import com.alibaba.fastjson.JSONObject;
import com.gsafety.framework.api.constant.ServiceNameConstants;
import com.gsafety.framework.api.factory.RemoteRcsFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author wzx
* @Description 公共资源服务
* @Date 2022/8/19 10:48
*/
@FeignClient(contextId = "remoteRcsService", value = ServiceNameConstants.RCS_SERVICE, fallbackFactory = RemoteRcsFallbackFactory.class)
public interface RemoteRcsService {
/**
* 获取重复警情配置
* @return
*/
@GetMapping("/config/v1/getRepeatWarnRuleInfo")
public JSONObject getRepeatWarnRuleInfo();
}
package com.gsafety.framework.api.factory;
import com.alibaba.fastjson.JSONObject;
import com.gsafety.framework.api.RemoteRcsService;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* @author wzx
* @Description 公共资源服务降级处理
* @Date 2022/8/19 10:53
*/
@Component
public class RemoteRcsFallbackFactory implements FallbackFactory<RemoteRcsService> {
private static final Logger log = LoggerFactory.getLogger(RemoteRcsFallbackFactory.class);
@Override
public RemoteRcsService create(Throwable throwable) {
log.error("rcs服务失败:{}", throwable.getMessage());
return new RemoteRcsService() {
@Override
public JSONObject getRepeatWarnRuleInfo() {
return null;
}
@Override
public JSONObject getDisposePlanListForOrganDispose(String warnType, String warnLevel, String zhId, String yhqk, Integer lc, String bkrs) {
return null;
}
@Override
public JSONObject queryListAddressBook() {
return null;
}
@Override
public JSONObject getAddressBooksByParentId(String itemParentId, String userId) {
return null;
}
@Override
public JSONObject queryAddressBookByIds(String ids) {
return null;
}
@Override
public JSONObject getParentSysDictDataDetail(String dictValue, String dictType) {
return null;
}
@Override
public JSONObject getDisposePlanListForWarnLevenUp(String warnType, String warnLevel, String zhId, String yhqk, Integer lc, String bkrs, String rswz, Integer rsmj) {
return null;
}
@Override
public JSONObject getImportantWarnNoticeList() {
return null;
}
@Override
public JSONObject getCarDutyRecord(String carNumber, String startTime, String endTime) {
return null;
}
@Override
public JSONObject getResImportantUnitByName(String name) {
return null;
}
@Override
public JSONObject addSysNoticeNew(String json) {
return null;
}
};
}
}
这样子在remoteCdsService接口里面调用报错的时候,就会转到RemoteCdsFallbackFactory里面的create方法进行自定义操作。
我这边是当报错的时候,接口直接返回Null.
@FeignClient(contextId = "remoteCdsService", value = ServiceNameConstants.CDS_SERVICE, fallbackFactory = RemoteCdsFallbackFactory.class)
contextId:用来唯一标识当一个微服务中存在多个FeignClient接口调用同一个服务提供者时的场景(若是不提供该属性值,则在程序启动时会启动失败,并提示如下信息
APPLICATION FAILED TO START
Description:
The bean ‘service-provider8000.FeignClientSpecification’ could not be registered. A bean with that name has already been defined and overriding is disabled.
Action:
n ‘service-provider8000.FeignClientSpecification’ could not be registered. A bean with that name has already been defined and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true