基於FeignClient提供簡單的用戶查詢服務

前言:python

  因爲系統升級,以前的員工數據庫(mongo庫)被棄用,改成用python維護的mysql庫,其餘系統訪問經過http請求,表結構對外不可見,其餘系統以前對員工mongo庫的依賴要解除。每套系統都去寫請求的接口太麻煩,因此打算寫一個員工查詢服務,http請求用原生的FeignClient實現。mysql

實現思路:spring

  場景:python端只提供了查詢全部員工,其餘系統請求多種多樣。sql

  解決:請求接口 -->  查詢全部員工  -->  FeignClient包裝的http請求  -->  過濾條件數據庫

實現步驟演示:json

  • 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}) 
相關文章
相關標籤/搜索