手写一个轻量级的网关API

以HTTP接口形式的应用,是目前大部分中小型企业最常见的微服务夸语言交互的实现方式

即:定义多个接口,外部调用,经网关解析进行分发,小编遇到的这种情况是,有多个服务,每个服务都需要单独有网关开墙,很是头疼,每上线一个服务都需要网关配置,极其头疼,再次实现一种暴露一个接口,通过参数来实现调用不同的方法的案例,

注意:改方案只适合学习,不适合线上项目

GITHUB项目地址

目录

思路分析

流程图

实现方案:

    1. 自定义注解 APiMapping
    2. 自定义ApiGateWayServlet
    3. 利用 Spring IOC 拆分方法并与 ApiMaping 做绑定由 ApiStore中HashMap维护

注解定义及利用IOC绑定注解与方法

api注解: APIMapping

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface APIMapping {
    String value();
    RequestMethod method();
}

通过注解对业务方法标记

  @APIMapping(value = "biz.api.order",method = RequestMethod.GET)
    public OrderInfo getOrderInfo(String orderId) {
        OrderInfo orderInfo = OrderInfo.builder().id(orderId).name("测试订单").price(12.2).build();
        return orderInfo;
    }
    @APIMapping(value = "biz.api.order2",method = RequestMethod.POST)
    public OrderInfo getOrderDo(OrderInfo orderInfo){
        return orderInfo;
    }

利用Spring 上下文对标记的方法进行绑定
初始化时候,扫描APIMapping接口

 String[] names = applicationContext.getBeanDefinitionNames();
        Class<?> type;
        for (String name : names) {
            type = applicationContext.getType(name);
            for (Method method : type.getDeclaredMethods()) {
                APIMapping apiMapping = method.getDeclaredAnnotation(APIMapping.class);
                if (apiMapping!=null){
                    addApiItem(apiMapping,name,method);
                }
            }
        }

重写自定义Servlet方法中的POST和GET

public class ApiGateWayServlet extends HttpServlet {
    private ApplicationContext applicationContext;

    private ApiGateWayHandler apiGateWayHandler;

    @Override
    public void init() throws ServletException {
        super.init();
        applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        apiGateWayHandler = applicationContext.getBean(ApiGateWayHandler.class);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        apiGateWayHandler.handle(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        apiGateWayHandler.handle(req,resp);
    }
}

根据接口绑定获取到执行的方法,利用反射执行

 public class ApiRunnable {
        private String apiName;
        private Method targetMethod;
        private String targetName;
        private Object target;
        private String Method;
 Object result = null;
        Object target = apiRunable.getTarget();
        Method targetMethod = apiRunable.getTargetMethod();
        Set<Method> methods = ReflectionUtils.getMethods(target.getClass(), ReflectionUtils.withName(targetMethod.getName()));
        Iterator<Method> iterator = methods.iterator();
        Method method = null;
        while (iterator.hasNext()) {
            method = iterator.next();
        }
        Class<?>[] parameterTypes = method.getParameterTypes();
        try {
            Class<?> aClass = Class.forName(parameterTypes[0].getName());
            Class<String> stringClass = String.class;
            if (stringClass == aClass) {
                result = apiRunable.run(parameter);
                
        ....
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,680评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,284评论 6 342
  • spring官方文档:http://docs.spring.io/spring/docs/current/spri...
    牛马风情阅读 1,856评论 0 3
  • 关键词 滤镜 GPUImage 颜色 Filter colorDistance 相机 识别 框架 纹理 渲染 本文...
    Willie_阅读 16,334评论 10 53
  • 夜深雨未眠,闻者心中愁。 忧思难相忘,莫敢登高楼。 但愿饮酒醉,一人独相守。 梦里赴旧乡,悠悠泛轻舟。
    来之不易的梦阅读 441评论 4 7

友情链接更多精彩内容