前言:

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

实现思路:

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

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

实现步骤演示:

  • Feign包装的http请求:
 /**
* 对外暴露接口.
*/
@FeignClient(name = XXXXClientConfig.APPLICATION_NAME, url = "https://xx.xxxx.com")
public interface IEmployService { @RequestMapping(value = "/xx/employee/search",
headers = {"Content-Type=application/json",
"App-Id=xxx",
"App-Secret=xxxx"})
@ResponseBody
ReturnUser getAllUser(@RequestBody GetAllUserForm getAllUserForm); }
  • 员工服务接口:
 @FeignClient(XXXXConfig.APPLICATION_NAME)
public interface IUserService { @RequestMapping(value = "/xxxx/getAll", method = RequestMethod.GET)
List<User> getAll(); @RequestMapping(value = "/xxxx/getUserByIds", method = RequestMethod.POST)
List<User> getUserByIds(@RequestBody List<String> userIds); @RequestMapping(value = "/xxxx/getById", method = RequestMethod.GET)
User getById(@RequestParam(value = "userId") String userId); @RequestMapping(value = "/xxxx/getByQuery", method = RequestMethod.POST)
List<User> getByQuery(@RequestBody UserQuery userQuery);
}
  • 员工服务实现:
 @RestController
public class UserSearchService implements IUserService {
private final static Logger log = LoggerFactory.getLogger("xxx");
private List<User> users = null;
private Long lastTime = null;
@Resource
private IEmployService employService; @Override
public List<User> getAll() {
if(users != null && users.size() > 0 && lastTime != null && (System.currentTimeMillis() - lastTime) <= 1000*60*10){
return users;
}
try {
GetAllUserForm form = new GetAllUserForm();
form.setPageNo(1);
form.setPageSize(99999);
ReturnUser allUser = employService.getAllUser(form);
users = new ArrayList<>();
allUser.getData().getRecords().forEach(returnUser -> {
User user = getUserFromReturnUser(returnUser);
users.add(user);
});
lastTime = System.currentTimeMillis();
return users;
}catch (Exception e){
if(users.size() > 0){
return users;
}else {
throw new RuntimeException("getAll(): user is null");
}
}
} @Override
public List<User> getUserByIds(@RequestBody List<String> userIds) {
List<User> all = getAll();
List<User> users = all.stream().filter(user -> userIds.contains(user.getId())).collect(Collectors.toList());
return users;
} @Override
public User getById(@RequestParam(value = "userId") String userId) {
List<User> all = getAll();
List<User> users = all.stream().filter(user -> user.getId().equals(userId))
.collect(Collectors.toList());
return users.size() == 1? users.get(0):null;
} @Override
public List<User> getByQuery(@RequestBody UserQuery userQuery) {
List<User> list = new ArrayList<>();
getAll().stream().filter(user -> {
if (CommonUtil.isNotBlank(userQuery.getName())) {
if (!userQuery.getName().equals(user.getName()) &&
!userQuery.getName().equals(user.getRealName())) {
return false;
}
}
if (CommonUtil.isNotBlank(userQuery.getKeyword())) {
if (!user.getName().contains(userQuery.getKeyword()) &&
!user.getRealName().contains(userQuery.getKeyword())) {
return false;
}
}
if (userQuery.getIds() != null) {
if (!userQuery.getIds().contains(String.valueOf(user.getNumber()))) {
return false;
}
}
return true;
}).forEach(user -> {
list.add(user);
});
return list;
} private User getUserFromReturnUser(Employee returnUser) {
User user = new User();
user.setName(returnUser.getName());
return user;
} }
  • springboot启动添加

      @EnableFeignClients(basePackages = {XXXXClientConfig.PACKAGE_PATH}) 

基于FeignClient提供简单的用户查询服务的更多相关文章

  1. 九、frp对外提供简单的文件访问服务

    通过 static_file 插件可以对外提供一个简单的基于 HTTP 的文件访问服务.类似于http的文件索引! 服务端frps.ini配置[common]bind_addr = 0.0.0.0bi ...

  2. C++ 容器的综合应用的一个简单实例——文本查询程序

    C++ 容器的综合应用的一个简单实例——文本查询程序 [0. 需求] 最近在粗略学习<C++ Primer 4th>的容器内容,关联容器的章节末尾有个很不错的实例.通过实现一个简单的文本查 ...

  3. GraphQL介绍&使用nestjs构建GraphQL查询服务

    GraphQL介绍&使用nestjs构建GraphQL查询服务(文章底部附demo地址) GraphQL一种用为你 API 而生的查询语言.出自于Facebook,GraphQL非常易懂,直接 ...

  4. 基于Servlet、JSP、JDBC、MySQL的一个简单的用户注冊模块(附完整源代码)

    近期看老罗视频,做了一个简单的用户注冊系统.用户通过网页(JSP)输入用户名.真名和password,Servlet接收后通过JDBC将信息保存到MySQL中.尽管是个简单的不能再简单的东西,但麻雀虽 ...

  5. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.10.配置用户NTF服务

    2.10.配置用户NTF服务 2.10.1.配置节点RAC1 1) [root@linuxrac1 sysconfig]#sed -i 's/OPTIONS/#OPTIONS/g' /etc/sysc ...

  6. 【分布式】-- 基于Nacos、OpenFeign搭建的微服务抽奖系统后台小案例

    1.项目介绍 最近入项目之前要求熟悉一下SpringCloud Nacos微服务基于Feign接口调用并整合Swagger2进行接口文档展示给前端,所以自己按照要求来编写并整合了一套基于SpringC ...

  7. MaxCompute,基于Serverless的高可用大数据服务

    摘要:2019年1月18日,由阿里巴巴MaxCompute开发者社区和阿里云栖社区联合主办的“阿里云栖开发者沙龙大数据技术专场”走近北京联合大学,本次技术沙龙上,阿里巴巴高级技术专家吴永明为大家分享了 ...

  8. 用.net访问电子枢纽信用中心的数据查询服务

    概要说明 电子枢纽全称国家交通运输物流公共信息平台,主要提供物流及生产企业进行物流相关数据交换的标准和API,详细介绍可参考其官网www.logink.org,本文假定阅读者对该平台已有了解,并已成功 ...

  9. 基于Solr的HBase多条件查询测试

    背景: 某电信项目中采用HBase来存储用户终端明细数据,供前台页面即时查询.HBase无可置疑拥有其优势,但其本身只对rowkey支持毫秒级 的快 速检索,对于多字段的组合查询却无能为力.针对HBa ...

随机推荐

  1. share_ptr_c++11

    C++智能指针 shared_ptr shared_ptr 是一个标准的共享所有权的智能指针, 允许多个指针指向同一个对象. 定义在 memory 文件中(非memory.h), 命名空间为 std. ...

  2. 前端工程化系列[06]-Yeoman脚手架核心机制

    在前端工程化系列[05] Yeoman脚手架使用入门这边文章中,对Yeoman的使用做了简单的入门介绍,这篇文章我们将接着探讨Yeoman这个脚手架工具内部的核心机制,主要包括以下内容 ❏ Yeoma ...

  3. docker中的数据库

      注:自从开始使用docker,部署方面的事情就简单多了.使用docker构建的数据库容器不用直接安装,开启后就可以使用,也比以前方便很多.下面将一些要点记录下来. 下面的例子使用以下环境: - 系 ...

  4. 00、Word Count

    1.开发环境 1.eclipse-jee-neon-3 2.scala-ide:http://download.scala-ide.org/sdk/lithium/e46/scala212/stabl ...

  5. HIVE开发总结

    基本数据类型 查看所有函数 搜索函数 搜索表 查看函数使用方法 关键字补全 显示表头 SET环境变量 查看建表语句.数据文件置 执行外部命令 NVL CONCAT IF CASE TRIM SUBST ...

  6. IntelliJ IDEA 下载安装(含注册码)

    https://blog.csdn.net/mashuai720/article/details/79389314

  7. 【TCP ZeroWindow】与【TCP window Full】

    1.作为接收方,有接收窗口,也就是接收缓冲区,win=xxx 告诉对方,我的接收窗口大小. 2.当我的接收窗口满了,也就是win=0,Wireshark显示[TCP ZeroWindow],这个时候, ...

  8. dubbo常见错误

    1.dubbo zookeeper注册中心provider的ip地址为内网ip,导致consumer连不上 我用的阿里云的服务器,host默认配置了内网ip,注销或删除即可 vim /etc/host ...

  9. HDU 1022.Train Problem I【栈的应用】【8月19】

    Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...

  10. PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers

    微擎出错信息: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2054] Server s ...