1.feign整合hystrix的降级
2.hystrix本身的降级
3.进行熔断的处理
package com.mcloud.nis.knowledge.feign; import com.alibaba.fastjson.JSONObject; import com.mcloud.nis.knowledge.feign.Impl.BasicSignFallBack; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; /** * Created by 周建鹏 * $ on 2019/10/18 $ 14:04 * <p> * 调用体征基础服务信息 */ 第一种: @FeignClient(name = "nis-signs", fallback = BasicSignFallBack.class) //依赖的服务 public interface BasicSignManager { /** * 获取生命体征的基础信息 */ @RequestMapping(value = "/confs/basics/signs", method = RequestMethod.GET) JSONObject getSignCode(@RequestParam String organCode); }
package com.mcloud.nis.knowledge.feign.Impl; import com.alibaba.fastjson.JSONObject; import com.mcloud.nis.knowledge.constant.BizErrorCodeEnum; import com.mcloud.nis.knowledge.feign.BasicSignManager; import com.mcloud.nis.knowledge.service.impl.CatalogueLibraryServiceImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; /** * Created by 周建鹏 * $ on 2019/11/5 $ 17:55 * <p> * 生命体征服务降级处理类,一定要实现服务类的接口 * <p/> */ @Component public class BasicSignFallBack implements BasicSignManager { private static Logger logger = LoggerFactory.getLogger(BasicSignFallBack.class); @Override public JSONObject getSignCode(String organCode) { logger.warn("生命体征接口的降级处理:{}", JSONObject.toJSONString(organCode)); JSONObject jsonObject = new JSONObject(); jsonObject.put("code", "10001"); jsonObject.put("error", BizErrorCodeEnum.NO_SERVICE); return jsonObject; } } 第二种:
@ApiOperation(value = "获取生命体征", notes = "获取生命体征") @ResponseBody @GetMapping(value = "/getBasicSign") @HystrixCommand(fallbackMethod = "getBasicSignByError") public ServiceResult getBasicSign(@RequestParam String code) { try { CheckParameterUtls.checkNotNull(code, BizErrorCodeEnum.PARAMETER_IS_NULL); JSONObject signCode = basicSignManager.getSignCode(code); return ServiceResult.success(signCode); } catch (BizException e) { return ServiceResult.fail(e.getErrorCode().getErrorCode(), e.getErrorCode().getDescription()); } } //https://blog.csdn.net/HUXU981598436/article/details/89316880 private ServiceResult getBasicSignByError(String code) { logger.info("生命体征入口controller中的降级方法:{}", JSONObject.toJSONString(code)); JSONObject jsonObject = new JSONObject(); jsonObject.put("code", code); jsonObject.put("error", BizErrorCodeEnum.NO_SERVICE.getDescription()); return ServiceResult.fail(Integer.parseInt(BizErrorCodeEnum.NO_SERVICE.getErrorCode()), BizErrorCodeEnum.NO_SERVICE.getDescription(), jsonObject); }