企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 简介 * 由于SpringCloud从2020.*版本开始不再支持Hystrix,故JPower也做了调整,跟随spring版本后feign的熔断机制采用了阿里巴巴的sentinel。 * sentinel具体文档可查看[sentinel流控](./sentinel流控.md) ## 代码示例 1. 在`system-api`中新建熔断类,命名为`SystemClientFallback` ![](https://img.kancloud.cn/9f/61/9f616a360d0c2eda0896a6c4ec377433_824x730.png) 2. 实现`SystemClient`接口,此时只需返回对应数据即可,不需要再定义为 Controller 3. 代码如下,**以下代码已在工程中实现** ~~~ @Component public class SystemClientFallback implements SystemClient { @Override public ResponseData<List<String>> queryChildOrgById(String id) { return ReturnJsonUtil.fail("查询失败"); } @Override public ResponseData<TbCoreOrg> queryOrgById(String orgId) { return ReturnJsonUtil.fail("查询失败"); } @Override public ResponseData<TbCoreClient> getClientByClientCode(String clientCode) { return ReturnJsonUtil.fail("查询失败"); } @Override public ResponseData<List<Object>> getUrlsByRoleIds(List<String> roleIds) { return ReturnJsonUtil.fail("查询失败"); } @Override public ResponseData<TbCoreTenant> getTenantByCode(String tenantCode) { return ReturnJsonUtil.fail("查询失败"); } @Override public ResponseData<List<TbCoreFunction>> getMenuListByRole(List<String> roleIds) { return ReturnJsonUtil.fail("查询失败"); } @Override public ResponseData<List<TbCoreDataScope>> getAllRoleDataScope() { return ReturnJsonUtil.fail("查询失败"); } @Override public ResponseData<List<TbCoreDataScope>> getDataScopeByRole(List<String> roleIds) { return ReturnJsonUtil.fail("查询失败"); } } ~~~ 4. 修改SystemClient,增加熔断配置 ![](https://img.kancloud.cn/31/6e/316e55b38434d3ed700324dc5d8252e7_2160x1478.png) 5. 这是我们再使用feign进行服务调用时,如果服务发生异常,就会返回查询失败的提示 6. 有时我们服务调用发生错误时我们想知道究竟发生什么错误,这时我们需要打印这些错误 7. 我们这里以`user-api`为例,看下`UserClientFallback`代码,可以看到继承`FallbackFactory`并实现`UserClient`泛型 ~~~ @Component @Slf4j public class UserClientFallback implements FallbackFactory<UserClient> { @Override public UserClient create(Throwable cause) { return new UserClient() { @Override public ResponseData<TbCoreUser> queryUserByLoginId(String loginId, String tenantCode) { log.error("调用queryUserByLoginIdPwd失败,参数:loginId={},e={}",loginId,cause); return ReturnJsonUtil.fail("查询失败"); } @Override public ResponseData<List<String>> getRoleIds(String userId) { return ReturnJsonUtil.fail("查询失败"); } @Override public ResponseData updateUserLoginInfo(TbCoreUser user) { log.error("调用updateUserLoginInfo失败,参数:{},e={}", JSON.toJSONString(user),cause); return ReturnJsonUtil.fail("更新失败"); } @Override public ResponseData<TbCoreUser> queryUserByCode(String otherCode, String tenantCode) { return ReturnJsonUtil.fail("查询失败"); } @Override public ResponseData<TbCoreUser> get(String id) { return ReturnJsonUtil.fail("查询失败"); } @Override public ResponseData<TbCoreUser> queryUserByPhone(String phone, String tenantCode) { return ReturnJsonUtil.fail("查询失败"); } @Override public ResponseData saveAdmin(TbCoreUser user,String roleId) { log.error("调用saveAdmin失败,参数:user={},roleId={} ,e={}",user,roleId,cause); return ReturnJsonUtil.printJson(ConstantsReturn.RECODE_API, cause.getMessage(),false); } }; } } ~~~ 8. `UserClient`修改如下 ![](https://img.kancloud.cn/a7/ab/a7ab7d36f395976c6de67debc982e734_2428x1094.png)