设为首页 加入收藏

TOP

使用Netty3或Netty4发布Http协议服务(一)
2018-01-17 13:05:18 】 浏览:444
Tags:使用 Netty3 Netty4 发布 Http 协议 服务

今天给大家简单的介绍一下Netty,让大家以后在使用到Netty的时候能够有一定的了解和基础,这样深入学习Netty以及以后灵活应用这门技术也就不在话下了,万丈高楼平地起,程序猿们平时还是要注重积累,多花些时间在技术上面,如果实在对代码提不起兴趣就早点规划好自己的发展路线,死磕着也没什么必要,对自己没啥好处,但是如果你至少不讨厌编程,那么还是多多学习吧!


 


Netty是什么


Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。


 


Netty的架构



 


Netty的特性


设计:


------统一的API,适用于不同的协议(阻塞和非阻塞)


------基于灵活、可扩展的事件驱动模型


------高度可定制的线程模型


------可靠的无连接数据Socket支持(UDP)


性能:


------更好的吞吐量,低延迟


------更省资源


------尽量减少不必要的内存拷贝


安全:


------完整的SSL/TLS和STARTTLS的支持


健壮性:


------不再因过快、过慢或超负载连接导致OutOfMemoryError


------不再有在高速网络环境下NIO读写频率不一致的问题


易用:


------完善的Java doc,用户指南和样例


------简洁简单


------仅依赖于JDK1.5


 


Netty怎么用


小Alan教大家使用Netty3或Netty4发布Http协议服务接口,来引导大家进入Netty的世界。


Netty3实现Http协议服务接口步骤:


第一步:创建Http业务处理服务类,代码如下


package com.alanlee.http;


import static org.jboss.netty.handler.codec.http.HttpResponseStatus.OK;
import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1;


import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.DynamicChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * HTTP服务业务处理类.
 *
 * @author AlanLee
 * @version 2018/01/11
 *
 */
public class HttpServerHandler extends SimpleChannelUpstreamHandler
{


    /**
    * 日志类.
    */
    protected static final Logger LOGGER = LoggerFactory.getLogger(HttpServerHandler.class);


    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception
    {


        HttpRequest request = (HttpRequest) e.getMessage();
        String method = request.getMethod().getName();
        String url = request.getUri().toLowerCase();
        System.out.println(method);
        System.out.println(url);
       
        // 接收请求内容并打印
        ChannelBuffer buffer = request.getContent();
        String requestStr = new String(buffer.array(), "UTF-8");
        System.out.println(requestStr);


        // 处理响应消息
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        ChannelBuffer responseBuffer = new DynamicChannelBuffer(2048);
        responseBuffer.writeBytes("你是猪吗?".getBytes("UTF-8"));
        response.setContent(responseBuffer);
        // 返回内容的MIME类型
        response.setHeader("Content-Type", "text/html; charset=UTF-8");
        // 响应体

首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇使用Netty3或Netty4发布Http协议.. 下一篇Spring Web项目spring配置文件随..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目