When Feign’s call reports an error, downgrade the fallBackFactory
Project Structure:
Code:
remoteCdsService
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 public resource service * @Date 2022/8/19 10:48 */ @FeignClient(contextId = "remoteRcsService", value = ServiceNameConstants.RCS_SERVICE, fallbackFactory = RemoteRcsFallbackFactory.class) public interface RemoteRcsService {<!-- --> /** * Get duplicate alert configuration * @return */ @GetMapping("/config/v1/getRepeatWarnRuleInfo") public JSONObject getRepeatWarnRuleInfo(); }
remoteCdsFallbackFactory
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 public resource service downgrade processing * @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 service failed:{}", 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; } }; } }
In this way, when an error is reported in the remoteCdsService interface, it will go to the create method in the RemoteCdsFallbackFactory for custom operations.
On my side, when an error is reported, the interface directly returns Null.
ContextId of Feign annotation:
@FeignClient(contextId = "remoteCdsService", value = ServiceNameConstants.CDS_SERVICE, fallbackFactory = RemoteCdsFallbackFactory.class)
-
contextId: used to uniquely identify the scenario when there are multiple FeignClient interfaces in a microservice calling the same service provider (if this property value is not provided, it will fail to start when the program starts, and the following information will be prompted
-
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