array(2) { ["docs"]=> array(10) { [0]=> array(10) { ["id"]=> string(3) "428" ["text"]=> string(77) "Visual Studio 2017 单独启动MSDN帮助(Microsoft Help Viewer)的方法" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(8) "DonetRen" ["tagsname"]=> string(55) "Visual Studio 2017|MSDN帮助|C#程序|.NET|Help Viewer" ["tagsid"]=> string(23) "[401,402,403,"300",404]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400964" ["_id"]=> string(3) "428" } [1]=> array(10) { ["id"]=> string(3) "427" ["text"]=> string(42) "npm -v;报错 cannot find module "wrapp"" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "zzty" ["tagsname"]=> string(50) "node.js|npm|cannot find module "wrapp“|node" ["tagsid"]=> string(19) "[398,"239",399,400]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400760" ["_id"]=> string(3) "427" } [2]=> array(10) { ["id"]=> string(3) "426" ["text"]=> string(54) "说说css中pt、px、em、rem都扮演了什么角色" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(12) "zhengqiaoyin" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400640" ["_id"]=> string(3) "426" } [3]=> array(10) { ["id"]=> string(3) "425" ["text"]=> string(83) "深入学习JS执行--创建执行上下文(变量对象,作用域链,this)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "Ry-yuan" ["tagsname"]=> string(33) "Javascript|Javascript执行过程" ["tagsid"]=> string(13) "["169","191"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511399901" ["_id"]=> string(3) "425" } [4]=> array(10) { ["id"]=> string(3) "424" ["text"]=> string(30) "C# 排序技术研究与对比" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "vveiliang" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(8) ".Net Dev" ["catesid"]=> string(5) "[199]" ["createtime"]=> string(10) "1511399150" ["_id"]=> string(3) "424" } [5]=> array(10) { ["id"]=> string(3) "423" ["text"]=> string(72) "【算法】小白的算法笔记:快速排序算法的编码和优化" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "penghuwan" ["tagsname"]=> string(6) "算法" ["tagsid"]=> string(7) "["344"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511398109" ["_id"]=> string(3) "423" } [6]=> array(10) { ["id"]=> string(3) "422" ["text"]=> string(64) "JavaScript数据可视化编程学习(二)Flotr2,雷达图" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "chengxs" ["tagsname"]=> string(28) "数据可视化|前端学习" ["tagsid"]=> string(9) "[396,397]" ["catesname"]=> string(18) "前端基本知识" ["catesid"]=> string(5) "[198]" ["createtime"]=> string(10) "1511397800" ["_id"]=> string(3) "422" } [7]=> array(10) { ["id"]=> string(3) "421" ["text"]=> string(36) "C#表达式目录树(Expression)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "wwym" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(4) ".NET" ["catesid"]=> string(7) "["119"]" ["createtime"]=> string(10) "1511397474" ["_id"]=> string(3) "421" } [8]=> array(10) { ["id"]=> string(3) "420" ["text"]=> string(47) "数据结构 队列_队列实例:事件处理" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "idreamo" ["tagsname"]=> string(40) "C语言|数据结构|队列|事件处理" ["tagsid"]=> string(23) "["246","247","248",395]" ["catesname"]=> string(12) "数据结构" ["catesid"]=> string(7) "["133"]" ["createtime"]=> string(10) "1511397279" ["_id"]=> string(3) "420" } [9]=> array(10) { ["id"]=> string(3) "419" ["text"]=> string(47) "久等了,博客园官方Android客户端发布" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(3) "cmt" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511396549" ["_id"]=> string(3) "419" } } ["count"]=> int(200) } 222 netty(五)http服务 - 爱码网

 

总体思路:

netty(五)http服务

 

netty(五)http服务

 

pipline总线:

    @Override
    public void initChannel(SocketChannel ch) throws Exception {

        ChannelPipeline pipeline = ch.pipeline();

        // decode 1 byte2http解码器
        pipeline.addLast(new HttpRequestDecoder());

        // encode last 1 http2byte编码器
        pipeline.addLast(new HttpResponseEncoder());
        pipeline.addLast(new HttpObjectAggregator(65535));

        // endoe last 2 body2full编码器
        pipeline.addLast(new BodyToResponseEncoder());

        // decode 2 full2body编码器
        pipeline.addLast(new RequestToBodyDecoder());

        // 业务处理Handler
        pipeline.addLast("httpServerHandler", handler);
    }

  

public class RequestToBodyDecoder extends MessageToMessageDecoder<FullHttpRequest> {

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest, List<Object> list) throws Exception {
// 内存泄漏 fullHttpRequest不再往下传递了,传递的是json
// ByteBuf msg = fullHttpRequest.content().retain();
ByteBuf msg = fullHttpRequest.content();

byte[] bs = new byte[msg.readableBytes()];
msg.readBytes(bs);
String json = new String(bs);

JSONObject jsonObject = null;

try {
jsonObject = JSONObject.parseObject(json);
} catch (Exception e) {
System.out.println("json error:" + json);

ChannelFuture channelFuture = channelHandlerContext.writeAndFlush(ResponseMessage.genFail());
channelFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if(!future.isSuccess()) {
future.cause().printStackTrace();
future.channel().close();
}
}
});
return;
}

if(null == jsonObject)
jsonObject = new JSONObject();

// 这样逻辑更通些
// channelHandlerContext.fireChannelRead(jsonObject);
// 这样也行,但是逻辑和性能都不如前者
// super.channelRead(channelHandlerContext, jsonObject);

list.add(jsonObject);
}

  

这个handler接收HttpRequestDecoder传送而来的FullHttpRequest,截取content部分byte(http body),反序列化为json字符串和json对象,传给下一个handler,如果参数不合法,则直接出站

 

@Sharable
public class HttpServerHandler extends SimpleChannelInboundHandler<JSONObject> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, JSONObject msg) throws Exception {

        System.out.println("收到客户端http请求: " + msg);

        msg.put("res", "收到了");

        ResponseMessage message = ResponseMessage.genSuccess().setResult(msg);

        ChannelFuture channelFuture = ctx.writeAndFlush(message);
     //   channelFuture.addListener(ChannelFutureListener.CLOSE);
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if(!future.isSuccess()) {
                    future.cause().printStackTrace();
                    future.channel().close();
                }
            }
        });
    }

  

主业务处理handler:接收参数json对象,简单处理后,形成ResponseMessage,出站

public class BodyToResponseEncoder extends MessageToMessageEncoder<ResponseMessage> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ResponseMessage responseMessage, List<Object> list) throws Exception {

        String json = JSON.toJSONString(responseMessage);

        FullHttpResponse response = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1,
                HttpResponseStatus.OK,
                Unpooled.wrappedBuffer(json.getBytes("utf-8")));
        response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json;charset=UTF-8");
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
        response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);

        // 下面2个支持跨域
        response.headers().set(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
        response.headers().set(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaders.Names.CONTENT_TYPE);

        list.add(response);
    }
}

  

接收主handler的ResponseMessage,组合成HttpResponseEncoder需要的FullHttpResponse

 

调试一下:

curl  -H "Content-type: application/json" -X POST -d '{"orderId":"11112479","account":"DU984141"}' http://localhost:8990/

netty(五)http服务

 

注意点:

1. 为什么BodyToResponseEncoder要在RequestToBodyDecoder之前?

RequestToBodyDecoder如果碰到不合法参数,直接返回出站,从该节点往上找OutBountHandler,此时如果没有BodyToResponseEncoder,ResponseMessage直接送到HttpResponseEncoder,它认不出报异常:

netty(五)http服务

 这个问题在https://www.cnblogs.com/silyvin/articles/9504587.html 中已有讨论

 

4. 跨域相关结合:https://www.cnblogs.com/silyvin/p/9565896.html

 

5. HttpServerHandler使用了Sharable,具体参考:https://www.cnblogs.com/silyvin/p/9593368.html

 

6. 启动后第一次访问,用chrome时read被调用了2次,同样的情况:https://segmentfault.com/q/1010000009831091,

用postman  和 safari 时1次,正常

 

7 抓包:关于tcp delayedack实践(二)http

 

补充:

可以用

pipeline.addLast(new HttpServerCodec());

代替:

// decode 1 byte2http解码器
pipeline.addLast(new HttpRequestDecoder());

// encode last 1 http2byte编码器
pipeline.addLast(new HttpResponseEncoder());

来看一下HttpServerCodec:

public final class HttpServerCodec extends CombinedChannelDuplexHandler<HttpRequestDecoder, HttpResponseEncoder>

组合了HttpRequestDecoder和HttpResponseEncoder

netty(五)http服务

相关文章:

  • 2021-11-18
  • 2021-11-20
  • 2021-11-20
  • 2021-11-17
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
  • 2021-11-17
猜你喜欢
  • 2021-12-09
  • 2021-11-18
  • 2021-11-20
  • 2021-11-20
  • 2021-11-17
  • 2021-11-18
  • 2021-11-20
  • 2021-12-10
相关资源
相似解决方案