赞
踩
通过fallbackFactory进行容错处理,调用后判断是否返回失败,失败就抛异常,通过Throwable透传异常message,报错到前端页面。
注意:客户端要熔断降级必须在yaml文件里进行配置。例如:
- feign:
- hystrix:
- enabled:true
SysUserController :
- package com.ruoyi.system.controller;
-
- import java.io.IOException;
- import java.util.List;
- import java.util.Set;
- import java.util.stream.Collectors;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.lang3.ArrayUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.DeleteMapping;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.PutMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
- import com.ruoyi.common.core.constant.UserConstants;
- import com.ruoyi.common.core.domain.R;
- import com.ruoyi.common.core.utils.StringUtils;
- import com.ruoyi.common.core.utils.poi.ExcelUtil;
- import com.ruoyi.common.core.web.controller.BaseController;
- import com.ruoyi.common.core.web.domain.AjaxResult;
- import com.ruoyi.common.core.web.page.TableDataInfo;
- import com.ruoyi.common.log.annotation.Log;
- import com.ruoyi.common.log.enums.BusinessType;
- import com.ruoyi.common.security.annotation.InnerAuth;
- import com.ruoyi.common.security.annotation.RequiresPermissions;
- import com.ruoyi.common.security.utils.SecurityUtils;
- import com.ruoyi.system.api.domain.SysDept;
- import com.ruoyi.system.api.domain.SysRole;
- import com.ruoyi.system.api.domain.SysUser;
- import com.ruoyi.system.api.model.LoginUser;
- import com.ruoyi.system.service.ISysConfigService;
- import com.ruoyi.system.service.ISysDeptService;
- import com.ruoyi.system.service.ISysPermissionService;
- import com.ruoyi.system.service.ISysPostService;
- import com.ruoyi.system.service.ISysRoleService;
- import com.ruoyi.system.service.ISysUserService;
-
- /**
- * 用户信息
- *
- * @author ruoyi
- */
- @RestController
- @RequestMapping("/user")
- public class SysUserController extends BaseController
- {
- @Autowired
- private ISysUserService userService;
-
- @Autowired
- private ISysRoleService roleService;
-
- @Autowired
- private ISysDeptService deptService;
-
- @Autowired
- private ISysPostService postService;
-
- @Autowired
- private ISysPermissionService permissionService;
-
- @Autowired
- private ISysConfigService configService;
-
-
- /**
- * 获取当前用户信息
- */
- @InnerAuth
- @GetMapping("/info/{username}")
- public R<LoginUser> info(@PathVariable("username") String username)
- {
- SysUser sysUser = userService.selectUserByUserName(username);
- if (StringUtils.isNull(sysUser))
- {
- return R.fail("用户名或密码错误");
- }
- // 角色集合
- Set<String> roles = permissionService.getRolePermission(sysUser);
- // 权限集合
- Set<String> permissions = permissionService.getMenuPermission(sysUser);
- LoginUser sysUserVo = new LoginUser();
- sysUserVo.setSysUser(sysUser);
- sysUserVo.setRoles(roles);
- sysUserVo.setPermissions(permissions);
- return R.ok(sysUserVo);
- }
-
- /**
- * 注册用户信息
- */
- @InnerAuth
- @PostMapping("/register")
- public R<Boolean> register(@RequestBody SysUser sysUser)
- {
- String username = sysUser.getUserName();
- if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
- {
- return R.fail("当前系统没有开启注册功能!");
- }
- if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(sysUser)))
- {
- return R.fail("保存用户'" + username + "'失败,注册账号已存在");
- }
- return R.ok(userService.registerUser(sysUser));
- }
-
-
- }

RemoteUserService:
- package com.ruoyi.system.api;
-
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestHeader;
- import com.ruoyi.common.core.constant.SecurityConstants;
- import com.ruoyi.common.core.constant.ServiceNameConstants;
- import com.ruoyi.common.core.domain.R;
- import com.ruoyi.system.api.domain.SysUser;
- import com.ruoyi.system.api.factory.RemoteUserFallbackFactory;
- import com.ruoyi.system.api.model.LoginUser;
-
- /**
- * 用户服务
- *
- * @author ruoyi
- */
- @FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.SYSTEM_SERVICE, fallbackFactory = RemoteUserFallbackFactory.class)
- public interface RemoteUserService
- {
- /**
- * 通过用户名查询用户信息
- *
- * @param username 用户名
- * @param source 请求来源
- * @return 结果
- */
- @GetMapping("/user/info/{username}")
- public R<LoginUser> getUserInfo(@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
-
- /**
- * 注册用户信息
- *
- * @param sysUser 用户信息
- * @param source 请求来源
- * @return 结果
- */
- @PostMapping("/user/register")
- public R<Boolean> registerUserInfo(@RequestBody SysUser sysUser, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
- }

RemoteUserFallbackFactory:
- package com.ruoyi.system.api.factory;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.cloud.openfeign.FallbackFactory;
- import org.springframework.stereotype.Component;
- import com.ruoyi.common.core.domain.R;
- import com.ruoyi.system.api.RemoteUserService;
- import com.ruoyi.system.api.domain.SysUser;
- import com.ruoyi.system.api.model.LoginUser;
-
- /**
- * 用户服务降级处理
- *
- * @author ruoyi
- */
- @Component
- public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserService>
- {
- private static final Logger log = LoggerFactory.getLogger(RemoteUserFallbackFactory.class);
-
- @Override
- public RemoteUserService create(Throwable throwable)
- {
- log.error("用户服务调用失败:{}", throwable.getMessage());
- return new RemoteUserService()
- {
- @Override
- public R<LoginUser> getUserInfo(String username, String source)
- {
- //throwable会把报错带过来
- return R.fail("获取用户失败:" + throwable.getMessage());
- }
-
- @Override
- public R<Boolean> registerUserInfo(SysUser sysUser, String source)
- {
- return R.fail("注册用户失败:" + throwable.getMessage());
- }
- };
- }
- }

- /**
- * 登录
- */
- public LoginUser login(String username, String password)
- {
- // 查询用户信息
- R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
-
- if (R.FAIL == userResult.getCode())
- {
- //判断如果失败,就抛异常,打出远程调用的报错message
- throw new ServiceException(userResult.getMsg());
- }
-
- if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
- {
- recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
- throw new ServiceException("登录用户:" + username + " 不存在");
- }
- LoginUser userInfo = userResult.getData();
- SysUser user = userResult.getData().getSysUser();
- if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
- {
- recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
- throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
- }
- if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
- {
- recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
- throw new ServiceException("对不起,您的账号:" + username + " 已停用");
- }
- if (!SecurityUtils.matchesPassword(password, user.getPassword()))
- {
- recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码错误");
- throw new ServiceException("用户不存在/密码错误");
- }
- recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
- return userInfo;
- }

博客1:
feign异常传递的两种方式 fallbackfactory和全局处理 获取服务端自定义异常
博客2:
SpringCloud feign微服务调用之间的异常处理_zf12178的博客-CSDN博客_feign微服务调用之间的异常处理方式
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。