码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-02-02 11:25:02      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:接口   control   简单   过滤   param   演示   前言   public   .com   

前言:

  由于系统升级,之前的员工数据库(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}) 

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

标签:接口   control   简单   过滤   param   演示   前言   public   .com   

原文地址:https://www.cnblogs.com/handsomecui/p/10347833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!