Web服务器
在第一章配置Stallion时,我们使用了IIS,这可不大好,咱们要有自己的Web服务器,而且是即支持Tcp又支持Udp的那种,不再搞代理转发。
现在有了,我亲手捏的,名字叫cattle,我已经打算把他开源。现在先就着代码略作介绍。启动。
public void start(String pHost, int pPort) {
Thread stoppableThread = new Thread() {
@Override
public void run() {
try {
bootstrap.group(boss, worker)
.handler(new LoggingHandler(LogLevel.DEBUG))
.channel(NioServerSocketChannel.class)
.childHandler(new HttpServerInitializer());
ChannelFuture f = bootstrap.bind(pHost, pPort).sync();
logger.info(" server start up on port : " + pPort);
f.channel().closeFuture().sync();
} catch (Exception ex) {
logger.error(ex.getMessage());
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
};
stoppableThread.start();
}
Cattle是基于netty实现的,通过HttpServerInitializer类引入Netty内置的HttpServerCodec对http数据包做解码,和我自己写的HttpRequestHandler处理http请求。
请求数据被解析为请求对象,放到Http处理上下文对象HttpHandleContext中,然后使用HttpAnswererRunner类进行处理,处理完成后将响应结果写回。
Web处理过程
- 从cookie里取得SessionID,初始化Session到处理上下文中,Session于Web服务十分重要,只是现在有的同学用得不多。
- 运行前向拦截器集合。
- 运行响应程序。
- 运行后向拦截器集合。
- 回写响应结果,状态码,异常信息等。
请求程序
- 先找到正确的响应器,对于静态文件以mime类型查找合适的响应器,无扩展名的请求则视为动态的程序性请求,content-type也被应于用响应器的匹配。
- 对于静态文件请求,实现了html,css,javascript和图片。而音视频,大文件上传下载目前还没有实现。
- 动态程序性请求,无非将url转变为包、类、方法,将提交的数据转化为请求参数,然后反射运行,再将结果序列化后回写,具体可参看代码。
使用实例
启动,十分简单。
@ControllerPackage("org.baseman.cattle_test_contract")
public class Main {
public static void main(String[] args) {
CattleApplication.run(Main.class,"127.0.0.1",38080);
}
}
启动时需要初始化Controller集合,初始化时要扫描jar包,指定包名可以提高扫描速度。在启动类上打上@ControllerPackage标注,在参数里指定包名。
标注Controller和Action
@Controller("/Test1")
public interface Test1 {
@Action(value = "/getOneInteger", httpMethod = "get")
int getOneInteger();
@Action(value = "/getOneList", httpMethod = "get")
ArrayList<String> getOneList();
@Action(value = "/getOneListList", httpMethod = "get")
ArrayList<ArrayList<String>> getOneListList();
@Action(value = "/postOneInteger", httpMethod = "post")
int postOneInteger(@ParameterInBody int postedInt);
@Action(value = "/postOneList", httpMethod = "post")
ArrayList<String> getOneList(@ParameterInBody ArrayList<String> oneList);
@Action(value = "/postOneListList", httpMethod = "post")
ArrayList<ArrayList<String>> getOneListList(@ParameterInBody ArrayList<ArrayList<String>> oneListList);
@Action(value = "/postTwoListList3", httpMethod = "post")
ArrayList<ArrayList<String>> postTwoListList(
@ParameterInBody(value = "oneListList1") ArrayList<ArrayList<String>> oneListList,
@ParameterInBody(value = "twoListList2") ArrayList<ArrayList<String>> twoListList,
@ParameterInHead(value = "dd") int id);
}
支持配置Url片段,HttpMethod,参数位置(在Search串里?在body里?在head里?),Spring Boot 的 Post方法将整个Body解析为一个参数,这很不优雅。咱们不学他,咱可以解析成多个。瞧上面最后一个postTwoListList,不光有两个参数在Body里,泛型也玩得很花的。
cattle支持使用拦截器。
cattle略作改动也能注册到Spring Cloud的注册中心(nacos,eureka)。
更多内容往后细写一点文章,也可以直接看代码。
UDP版的Cattle
支持UDP,做成UDP版的Restful编程框架。继而成为以区块链为基础的web3.0的编程框架。不过开源的版本暂时只支持TCP。
远程调用框架
有了Web服务器还得有远程调用框架,不然有公无母也不太和谐。所以又给整了一个,命名为foreign,名字取的是外交官的意思而不是外国人。
咱们用foreign来调一下前面代码里的postTwoListList。
ArrayList<ArrayList<String>> list1 = new ArrayList<ArrayList<String>>();
ArrayList<String> list11 = new ArrayList<>();
list11.add("11");
ArrayList<String> list12 = new ArrayList<>();
list12.add("12");
list1.add(list11);
list1.add(list12);
ArrayList<ArrayList<String>> list2 = new ArrayList<ArrayList<String>>();
ArrayList<String> list21 = new ArrayList<>();
list21.add("21");
ArrayList<String> list22 = new ArrayList<>();
list22.add("22");
list2.add(list21);
list2.add(list22);
ArrayList<ArrayList<String>> result2vList = RestfulRequester
.proxy(Test1.class, "http://127.0.0.1:38080")
.postTwoListList(list1, list2, 8);
非常的优雅。0配置。
尤其这一句proxy(Test1.class, "http://127.0.0.1:38080"),Test1.class表示你调的什么,“http://xxxx”表示你调的哪儿。后面的postTwoListList(list1, list2, 8)确定方法和参数,使远程调用和本地调用几乎一样。
TCP版的Foreign支持Spring cloud的各种服务发现。
UDP版暂不开源。
源码地址
cattle:https://gitee.com/limingshan1989/cattle
foreign:foreign: restful调用框架