备注:以后时间充裕时,可以学习 cwdtom/gateway ,里面实现了秦老师作业中提出的各种功能。
作业要求:整合你上次作业的 httpclient/okhttp。
答案:(凭感觉实现的,估计还有问题。)
作业要求:使用Netty实现http访问。
答案:(凭感觉实现的,估计还有问题。)
作业要求:实现过滤器。
答案:io.github.kimmking.gateway.filter.HttpRequestHeaderAppenderFilter
增加了HTTP Header: nio = du feng
io.github.kimmking.gateway.inbound.HttpInboundHandler class 是核心,它指向filter、router和handler。
private HttpOutBoundHandler handler;
private final HttpRequestFilter filter;
private final HttpEndpointRouter router;
三种 HttpOutBoundHandler 是由 system property outBoundHandlerType 决定的:
private void initHandler() {
String outBoundHandlerType = System.getProperty("outBoundHandlerType", NettyServerApplication.HTTPCLIENT_MODE);
if (outBoundHandlerType.equalsIgnoreCase(NettyServerApplication.HTTPCLIENT_MODE)) {
handler = new HttpClientOutboundHandler(router.route(proxyServers));
} else if (outBoundHandlerType.equalsIgnoreCase(NettyServerApplication.OKHTTP_MODE)) {
handler = new OkHttpOutboundHandler(router.route(proxyServers));
} else {
handler = new NettyHttpClientOutboundHandler(router.route(proxyServers));
}
}
system property outBoundHandlerType 是在io.github.kimmking.gateway.NettyServerApplication 中设置的。
System.setProperty("outBoundHandlerType", NETTY_MODE);
类图如下:
作业要求:实现路由。
答案:io.github.kimmking.gateway.router.RandomEndpointRouter
随机选取服务,对应于 io.github.kimmking.gateway.NettyServerApplication 的初始Server列表:
String[] servers = {"http://localhost:8801",
"http://localhost:8802",
"http://localhost:8803",
"http://localhost:8804",
"http://localhost:8805"};