• 基于FeignClient提供简单的用户查询服务


    前言:

      由于系统升级,之前的员工数据库(mongo库)被弃用,改为用python维护的mysql库,其他系统访问通过http请求,表结构对外不可见,其他系统之前对员工mongo库的依赖要解除。每套系统都去写请求的接口太麻烦,所以打算写一个员工查询服务,http请求用原生的FeignClient实现。

    实现思路:

      场景:python端只提供了查询所有员工,其他系统请求多种多样。

      解决:请求接口 -->  查询所有员工  -->  FeignClient包装的http请求  -->  过滤条件

    实现步骤演示:

    • Feign包装的http请求:
     1 /**
     2  * 对外暴露接口.
     3  */
     4 @FeignClient(name = XXXXClientConfig.APPLICATION_NAME, url = "https://xx.xxxx.com")
     5 public interface IEmployService {
     6 
     7     @RequestMapping(value = "/xx/employee/search",
     8             headers = {"Content-Type=application/json",
     9             "App-Id=xxx",
    10             "App-Secret=xxxx"})
    11     @ResponseBody
    12     ReturnUser getAllUser(@RequestBody GetAllUserForm getAllUserForm);
    13 
    14 }
    • 员工服务接口:
     1 @FeignClient(XXXXConfig.APPLICATION_NAME)
     2 public interface IUserService {
     3 
     4     @RequestMapping(value = "/xxxx/getAll", method = RequestMethod.GET)
     5     List<User> getAll();
     6 
     7     @RequestMapping(value = "/xxxx/getUserByIds", method = RequestMethod.POST)
     8     List<User> getUserByIds(@RequestBody List<String> userIds);
     9 
    10     @RequestMapping(value = "/xxxx/getById", method = RequestMethod.GET)
    11     User getById(@RequestParam(value = "userId") String userId);
    12 
    13     @RequestMapping(value = "/xxxx/getByQuery", method = RequestMethod.POST)
    14     List<User> getByQuery(@RequestBody UserQuery userQuery);
    15 }
    • 员工服务实现:
     1 @RestController
     2 public class UserSearchService implements IUserService {
     3     private final static Logger log = LoggerFactory.getLogger("xxx");
     4     private List<User> users = null;
     5     private Long lastTime = null;
     6     @Resource
     7     private IEmployService employService;
     8 
     9     @Override
    10     public List<User> getAll() {
    11         if(users != null && users.size() > 0 && lastTime != null && (System.currentTimeMillis() - lastTime) <= 1000*60*10){
    12             return users;
    13         }
    14         try {
    15             GetAllUserForm form = new GetAllUserForm();
    16             form.setPageNo(1);
    17             form.setPageSize(99999);
    18             ReturnUser allUser = employService.getAllUser(form);
    19             users = new ArrayList<>();
    20             allUser.getData().getRecords().forEach(returnUser -> {
    21                 User user = getUserFromReturnUser(returnUser);
    22                 users.add(user);
    23             });
    24             lastTime = System.currentTimeMillis();
    25             return users;
    26         }catch (Exception e){
    27             if(users.size() > 0){
    28                 return users;
    29             }else {
    30                 throw new RuntimeException("getAll(): user is null");
    31             }
    32         }
    33     }
    34 
    35     @Override
    36     public List<User> getUserByIds(@RequestBody List<String> userIds) {
    37         List<User> all = getAll();
    38         List<User> users = all.stream().filter(user -> userIds.contains(user.getId())).collect(Collectors.toList());
    39         return users;
    40     }
    41 
    42     @Override
    43     public User getById(@RequestParam(value = "userId") String userId) {
    44         List<User> all = getAll();
    45         List<User> users = all.stream().filter(user -> user.getId().equals(userId))
    46                 .collect(Collectors.toList());
    47         return users.size() == 1? users.get(0):null;
    48     }
    49 
    50     @Override
    51     public List<User> getByQuery(@RequestBody UserQuery userQuery) {
    52         List<User> list = new ArrayList<>();
    53         getAll().stream().filter(user -> {
    54             if (CommonUtil.isNotBlank(userQuery.getName())) {
    55                 if (!userQuery.getName().equals(user.getName()) &&
    56                         !userQuery.getName().equals(user.getRealName())) {
    57                     return false;
    58                 }
    59             }
    60             if (CommonUtil.isNotBlank(userQuery.getKeyword())) {
    61                 if (!user.getName().contains(userQuery.getKeyword()) &&
    62                         !user.getRealName().contains(userQuery.getKeyword())) {
    63                     return false;
    64                 }
    65             }
    66             if (userQuery.getIds() != null) {
    67                 if (!userQuery.getIds().contains(String.valueOf(user.getNumber()))) {
    68                     return false;
    69                 }
    70             }
    71             return true;
    72         }).forEach(user -> {
    73             list.add(user);
    74         });
    75         return list;
    76     }
    77 
    78 
    79     private User getUserFromReturnUser(Employee returnUser) {
    80         User user = new User();
    81         user.setName(returnUser.getName());
    82         return user;
    83     }
    84 
    85 }
    •  springboot启动添加
       1 @EnableFeignClients(basePackages = {XXXXClientConfig.PACKAGE_PATH}) 
  • 相关阅读:
    Git出现error: Your local changes to the following files would be overwritten by merge: ... Please, commit your changes or stash them before you can merge.的问题解决(Git代码冲突)
    JDK内置工具jstack(Java Stack Trace)(转)
    Java 5/Java 6/Java7/Java 8新特性收集
    Linux使用screen实现关闭ssh连接的情况下,让程序继续在后台运行
    Linux出现cannot create temp file for here-document: No space left on device的问题解决
    解决树莓派8G的SD卡只能识别3.3G,SD卡扩容
    Windows下拷贝Linux的文件到本地(Putty)
    Linux下运行Java项目时,出现No X11 DISPLAY variable was set, but this program performed an operation which requires it.的问题解决
    Maven错误 diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)问题解决
    Ubuntu下安装Maven
  • 原文地址:https://www.cnblogs.com/handsomecui/p/10347833.html
Copyright © 2020-2023  润新知