1 Star 1 Fork 0

hubert-樂xx / xchain

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
LGPL-3.0

MVC通用执行模型

汇聚http的上层接口数据路由, 用于rpc数据交换, 事件模型等 到 同一个MVC逻辑处理中心

抽象一个系统所有的公共功能方法, 统一由一个MVC中心管理

不管是http请求还是, rpc跨系统调用, 事件触发等, 都由同一个MVC去派发执行

架构图

安装教程

<dependency>
    <groupId>cn.xnatural</groupId>
    <artifactId>xchain</artifactId>
    <version>1.1.2</version>
</dependency>

@Route: 对应的处理器

@Route("/a/b/{c}")
Object test(String c, String p1, Integer p2) {
    Map<String, Object> resp = new HashMap();
    resp.put("c", c);
    resp.put("p1", p1);
    resp.put("p2", p2);
    return resp;
}

特殊参数

处理器

@Route(path = "arr")
Object arr(Integer[] ids) {
    return Arrays.stream(ids).map(i -> i + 1).collect(Collectors.toList());
}

@Route(path = "map")
Object arr(Map<String, Object> data) {
    return data;
}

@Route(path = "optional")
Object optional(Optional<String> kw) {
    return kw.orElse("optional");
}

// 指定协议
@Route(path = "proto", protocol="test")
Object proto(){
    return "xxx";
}

触发

// 数组参数
mvc.handle("/arr?ids[]=1&ids[]=2");
// map 参数
mvc.handle("/map?data[a]=1&data[b]=2");
// Optional 参数
mvc.handle("/optional");
// 指定协议
mvc.handle("/proto", "test");

@Filter: 过滤器

@Filter.prefix 配置需要过滤拦截的路由前缀

方法返回

  • boolean, false: 则中断往下匹配执行
  • 其他忽略

一个例子

// 创建一个mvc
IMvc<Context> mvc = new IMvc() {
    final Chain<Context> chain = new Chain(this);
    @Override
    public Chain<Context> chain() { return chain; }
    
    @Override
    public void render(Context ctx, Object body, Consumer<Object> afterFn) {
        log.info("render: {}, {}", ctx.path, body);
    }
    
    // 异常统一处理
    @Override
    public void errHandle(Context ctx, Throwable ex) {
        log.error("error: " + ctx.id(), ex);
    }
};

// 添加路由处理
mvc.chain().route("/a/b/{c}", ctx -> {
    ctx.render(ctx.param("c"));
});

// 从对象中解析路由处理器
mvc.chain().resolve(new Object() {

    @Filter(prefix = "/a")
    void f() {
        log.info("=====filter");
    }
    
    @Route(path = "/a/b/d")
    Object test() {
        return "dd";
    }
});

// 手动执行一个路由请求
Map<String, Object> param = new HashMap<>();
param.put("p1", "1");
// 手动执行一个路由请求
mvc.handle("/a/b/" + s + "?p=1", param);

内部结构原理

协议名 -> 节点链

协议名: (非必须)支持哪种协议, 用于处理器分组, 不区分大小写

节点链: 按照节点处理器的优先级排序链成链

排序优先级规则: (优先级越高节点越靠前)

  1. 分片个数越大越优先级越高
  2. 单分片规则
  • 分片是 / 优先级最低
  • 分片字符串越明确(固定字符串)优先级越高
  • 例1: a 高于 {var}
  • 例2: a{var1}b 高于 a{var}
  • 例3: a{var1}b{var2} 高于 a{var1}b
  • 最后分片按字符串a-z顺序比较优先级

匹配执行流程

  1. 找到对应的请求协议, 然后从头往下节点依次匹配执行
  2. 跳跃匹配: 如果节点匹配结果code有对应的跳跃节点

节点匹配结果code对应

  • 0: 匹配
  • -1: 不匹配: 请求路径分片 少于 当前节点路径分片
  • -2: 不匹配: method
  • -3: 不匹配: consume
  • -4: 不匹配: produce
  • >0: 不匹配: 是路由的第几个分片不匹配

数据结构示例

{
	"": {
		Route[/a/b], 
	}	
	"x-ap": {
		Route[(GET)/key/{key}/{dataKey}]{1=Route[(POST)/dataVersion]}, 
		Route[(GET)/key/{key}], 
		Route[(POST)/dataVersion], 
	}
	"websocket": {
		WS[(websocket)/test/msg], 
		WS[(websocket)/ws], 
	}
	"x-cluster": {
		Route[(POST)/nodeDown/{infect}]{-1=Route[(GET)/apps]}, 
		Route[(GET)/nodes/{appName}], 
		
		Route[(GET)/apps], 
		Route[(GET)/group], 
		Route[(POST)/nodeUp], 
		Route[(POST)/nodeUpdate], 
		Route[/sync], 
	}
	"x-piecefile": {
		Route[/test/upload], 
	}
	"http": {
		Filter[(0)/], 
		Filter[(1)/test/], 
		Filter[(0)/test/]{0=Route[/test/async]}, 
		
		Route[/view/policy/collector/{fName}]{-1=Route[(GET)/collector/collectResult/{id}], 1=Route[(POST)/collector/{id}/enable/{enabled}], 2=Route[/view/data/{fName}]}, 
		Route[/view/policy/field/{fName}], 
		Route[(POST)/collector/{id}/enable/{enabled}], 
		Route[(PUT)/user/{uId}/addPrivilege/{privilegeId}]{1=Route[(GET)/collector/collectResult/{id}]}, 
		Route[(DELETE)/user/{uId}/delPrivilege/{privilegeId}], 
		Route[(GET)/user/{uId}/hasPrivilege/{privilegeId}], 
		Route[(GET)/user/{uId}/privilegePaging/{page}], 
		
		Route[(GET)/collector/collectResult/{id}]{-1=Route[/decision/clean], 1=Route[/css/fonts/{fName}]}, 
		Route[/collector/collectResultPaging/{page}], 
		Route[(GET)/collector/paging/{page}]{2=Route[/collector/{id}/test]}, 
		Route[/css/fonts/{fName}]{1=Route[(GET)/decision/decideResult/{decideId}]}, 
		Route[/css/lib/{fName}]{2=Route[/css/{fName}]}, 
		Route[(GET)/decision/decideResult/{decideId}]{1=Route[(GET)/field/paging/{page}]}, 
		Route[(GET)/decision/decideResultDetail/{decideId}], 
		Route[/decision/decideResultPaging/{page}], 
		Route[/decision/paging/{page}]{2=Route[/decision/{id}/decide]}, 
		Route[(GET)/field/paging/{page}]{2=Route[(POST)/field/{id}/updateType]}, 
		Route[/js/lib/{fName}]{2=Route[/js/{fName}]}, 
		Route[(GET)/user/paging/{page}]{2=Route[(POST)/user/{uId}/changePwd]}, 
		Route[/view/data/{fName}]{2=Route[/view/{fName}]}, 
		Route[/collector/{id}/test], 
		Route[/decision/{id}/decide]{1=Route[(POST)/field/{id}/updateType]}, 
		Route[(POST)/decision/{id}/updateApiConfig], 
		Route[(POST)/decision/{id}/updateDsl], 
		Route[(POST)/field/{id}/updateType], 
		Route[(POST)/user/{uId}/changePwd]{1=Route[/decision/clean]}, 
		Route[(POST)/user/{uId}/restPassword], 
		
		Route[/decision/clean]{-1=Route[(PUT)/collector/], 1=Route[/test/async]}, 
		Route[(GET)/decision/countDecideResult], 
		Route[(GET)/decision/countRuleResult]{2=Route[(DELETE)/decision/{id}]}, 
		Route[/test/async]{1=Route[(application/json)/api-doc/{fName}.json]}, 
		Route[/test/auth], 
		Route[/test/cus], 
		Route[/test/dao], 
		Route[(application/json)/test/error], 
		Route[(application/x-www-form-urlencoded)/test/form], 
		Route[(application/json)/test/json], 
		Route[/test/login], 
		Route[/test/remote], 
		Route[/test/string], 
		Route[/test/timeout], 
		Route[/test/upload], 
		Route[(application/json)/api-doc/{fName}.json]{1=Route[(POST)/collector/{id}]}, 
		Route[/api-doc/{fName}], 
		Route[(POST)/collector/{id}]{1=Route[/component/{fName}]}, 
		Route[(DELETE)/collector/{id}], 
		Route[/component/{fName}], 
		Route[/css/{fName}], 
		Route[(DELETE)/decision/{id}], 
		Route[(POST)/field/{id}]{1=Route[/file/{fName}]}, 
		Route[(DELETE)/field/{id}], 
		Route[/file/{fName}], 
		Route[/img/{fName}], 
		Route[/js/{fName}], 
		Route[/opHistoryPaging/{page}], 
		Route[(DELETE)/user/{uId}], 
		Route[/view/{fName}], 
		
		Route[(PUT)/collector/], 
		Route[(GET)/currentUser], 
		Route[(PUT)/decision/], 
		Route[/favicon.ico], 
		Route[(PUT)/field/], 
		Route[/health], 
		Route[/index.html], 
		Route[/login], 
		Route[/logout], 
		Route[/test.html], 
		Route[(PUT)/user/], 
		Route[(GET)/webConfig], 
		Route[/], 
	}
	"dataversion": {
		Route[/collector], 
		Route[/decision], 
		Route[/field], 
		Route[/user], 
	}
}
GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

简介

MVC 通用执行模型 展开 收起
Java
LGPL-3.0
取消

发行版 (3)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/xnat/xchain.git
git@gitee.com:xnat/xchain.git
xnat
xchain
xchain
master

搜索帮助