Netty HTTP on Android

Stella981
• 阅读 675

Netty是一个NIO的客户端服务器框架,它使我们可以快速而简单地开发网络应用程序,比如协议服务器和客户端。它大大简化了网络编程,比如TCP和UDP socket服务器。

“快速而简单”并不意味着开发出来的应用可维护性或性能不好。Netty已经实现了大量的协议,比如FTP,SMTP,HTTP,以及各种基于二进制和文本的传统协议。可以说Netty已经找到了一种方法来实现简单的开发,高性能,稳定性,灵活性而不需要做妥协。

Netty的结构大体如下图这样:

Netty HTTP on Android

就设计而言,Netty给不同的传输类型,不管是阻塞的还是非阻塞的,提供了统一的接口。它基于一个灵活的和可扩展的事件模型,这使得处理不同逻辑的部分可以有效的隔离开来。它具有高度可定制的线程模型 - 单线程,一个或多个线程池,比如SEDA。它还提供无连接的datagram socket支持。

如Netty这般,功能如此强大,性能如此优良的网络库,不用在Android上真是可惜了。这里我们就尝试将Netty用在Android上。

下载Netty

首先是下载Netty。Netty的官网地址,我们可以在这里找到下载Netty的地址,当然还有许许多多的文档。这里使用了当前4.1.x最新版的Netty 4.1.4,下载地址:

http://dl.bintray.com/netty/downloads/netty-4.1.4.Final.tar.bz2

解压之后,为了省事,直接将netty-4.1.4.Final/jar/all-in-one/下的netty-all-4.1.4.Final.jar拷贝进了工程下app module的libs目录下。

Netty的简单使用

不出意外,直接通过编译。接着我们就参考netty/example/src/main/java/io/netty/example/http/snoop中client部分的代码,将Netty用起来,这主要有如下几个类:

package io.netty.example.http.snoop;

import java.net.URI;
import java.net.URISyntaxException;

import javax.net.ssl.SSLException;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;

public class HttpClient {
    public static final String TAG = "NettyClient";

    public static void getResponse(final String url) {
        new Thread() {
            @Override
            public void run() {
                try {
                    URI uri = new URI(url);
                    String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
                    String host = uri.getHost();
                    int port = uri.getPort();

                    if (port == -1) {
                        if ("http".equalsIgnoreCase(scheme)) {
                            port = 80;
                        } else if ("https".equalsIgnoreCase(scheme)) {
                            port = 443;
                        }
                    }
                    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
                        System.err.println("Only HTTP(S) is supported.");
                        return;
                    }

                    final boolean ssl = "https".equalsIgnoreCase(scheme);
                    final SslContext sslCtx;
                    if (ssl) {
                        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
                    } else {
                        sslCtx = null;
                    }

                    EventLoopGroup group = new NioEventLoopGroup();
                    try {
                        Bootstrap bootstrap = new Bootstrap();
                        bootstrap.group(group).channel(NioSocketChannel.class)
                                .handler(new HttpClientInitializer(sslCtx));

                        // Make the connection attempt.
                        Channel channel = bootstrap.connect(host, port).sync().channel();

                        // Prepare the HTTP request.
                        DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
                                HttpMethod.GET, url);
                        request.headers().set(HttpHeaderNames.HOST, host);
                        request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderNames.KEEP_ALIVE);
                        request.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);

                        // Send the HTTP request.
                        channel.writeAndFlush(request);

                        // Wait for the server to close the connection.
                        channel.closeFuture().sync();
                    } finally {
                        group.shutdownGracefully();
                    }
                } catch (URISyntaxException e) {
                } catch (SSLException e) {
                } catch (InterruptedException e) {
                }
            }
        }.start();
    }
}

这个class提供给外部调用的接口。使用者可以传入URL,将借由这个类,通过Netty来访问网络并获取响应。然后来看HttpClientInitializer:

package io.netty.example.http.snoop;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.ssl.SslContext;

public class HttpClientInitializer extends ChannelInitializer<SocketChannel> {

    private final SslContext sslCtx;

    public HttpClientInitializer(SslContext sslCtx) {
        this.sslCtx = sslCtx;
    }

    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline p = ch.pipeline();

        // Enable HTTPS if necessary.
        if (sslCtx != null) {
            p.addLast(sslCtx.newHandler(ch.alloc()));
        }

        p.addLast(new HttpClientCodec());

        // Remove the following line if you don't want automatic content decompression.
        p.addLast(new HttpContentDecompressor());

        // Uncomment the following line if you don't want to handle HttpContents.
        //p.addLast(new HttpObjectAggregator(1048576));

        p.addLast(new HttpClientHandler());
    }
}

这个class负责对Channel的Pipeline进行初始化,这其中最关键的是HttpClientHandler:

package io.netty.example.http.snoop;

import android.util.Log;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.CharsetUtil;

public class HttpClientHandler extends SimpleChannelInboundHandler<HttpObject> {

    @Override
    public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
        if (msg instanceof HttpResponse) {
            HttpResponse response = (HttpResponse) msg;

            Log.i(HttpClient.TAG, "STATUS: " + response.status());
            Log.i(HttpClient.TAG, "VERSION: " + response.protocolVersion());

            if (!response.headers().isEmpty()) {
                for (CharSequence name: response.headers().names()) {
                    for (CharSequence value: response.headers().getAll(name)) {
                        Log.i(HttpClient.TAG, "HEADER: " + name + " = " + value);
                    }
                }
            }
        }
        if (msg instanceof HttpContent) {
            HttpContent content = (HttpContent) msg;
            String responseContent = content.content().toString(CharsetUtil.UTF_8);
            Log.i(HttpClient.TAG, responseContent);

            if (content instanceof LastHttpContent) {
                ctx.close();
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}

我们正是通过实现HttpClientHandler,而获取到Netty返回给我们的响应的。

在我们的Android应用代码中调用HttpClient来通过Netty从网络获取响应:

package io.netty.example.http.snoop;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private TextView mTextScreen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnGetIpInfo = (Button) findViewById(R.id.btn_get_ip_info_with_netty);
        btnGetIpInfo.setOnClickListener(mBtnClickListener);

        mTextScreen = (TextView) findViewById(R.id.text_screen);
    }

    View.OnClickListener mBtnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String url = "http://ip.taobao.com/service/getIpInfo.php?ip=123.58.191.68";
            mTextScreen.setText("To access " + url);
            Log.i(TAG, "To access " + url);
            if (R.id.btn_get_ip_info_with_netty == v.getId()) {
                HttpClient.getResponse(url);
            }
        }
    };
}

当然不能忘记了在AndroidManifest.xml中添加对INTERNET权限的请求:

    <uses-permission android:name="android.permission.INTERNET"/>

做完了所有这些之后,Netty基本上就可以跑起来了。不过意外还是发生了:

08-10 10:13:33.670 17720-17720/io.netty.example.http.snoop6.myapplication I/MainActivity: To access http://ip.taobao.com/service/getIpInfo.php?ip=123.58.191.68
08-10 10:13:33.818 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err: java.lang.NoClassDefFoundError: com.jcraft.jzlib.Inflater
08-10 10:13:33.818 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.handler.codec.compression.JZlibDecoder.<init>(JZlibDecoder.java:27)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.handler.codec.compression.ZlibCodecFactory.newZlibDecoder(ZlibCodecFactory.java:122)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.handler.codec.http.HttpContentDecompressor.newContentDecoder(HttpContentDecompressor.java:57)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.handler.codec.http.HttpContentDecoder.decode(HttpContentDecoder.java:87)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.handler.codec.http.HttpContentDecoder.decode(HttpContentDecoder.java:46)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:88)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:435)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:280)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:396)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:248)
08-10 10:13:33.826 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:250)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:129)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:571)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:474)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:428)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:398)
08-10 10:13:33.834 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:877)
08-10 10:13:33.841 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
08-10 10:13:33.841 17720-18708/io.netty.example.http.snoop6.myapplication W/System.err:     at java.lang.Thread.run(Thread.java:841)
08-10 10:13:33.841 17720-18708/io.netty.example.http.snoop6.myapplication I/NettyClient: --------- beginning of /dev/log/system
08-10 10:35:02.162 17720-17720/io.netty.example.http.snoop6.myapplication I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@41c10f28 time:1282964865

有一个class com.jcraft.jzlib.Inflater找不到。这还需要添加对jzlib的依赖:

    compile 'com.jcraft:jzlib:1.1.2'

至此顺利地将Netty跑起来:

{
    "code":0,
    "data":{
        "country":"中国",
        "country_id":"CN",
        "area":"华东",
        "area_id":"300000",
        "region":"浙江省",
        "region_id":"330000",
        "city":"杭州市",
        "city_id":"330100",
        "county":"",
        "county_id":"-1",
        "isp":"网易网络",
        "isp_id":"1000119",
        "ip":"123.58.191.68"
    }
}

Netty的裁剪

Netty很强大,all-in-one jar用起来很方便,这很不错。但all-in-one jar有点大,其中包含的一些诸如对memcache,redis,stomp,sctp和udt等的支持,我们在移动端并不会用掉。因而需要对它做一点裁剪。

既然不能用all-in-one包,那就把需要的几个jar文件单独copy进我们的工程好了。对于android而言,目测netty-4.1.4.Final/jar/下我们需要拷贝的jar文件主要有下面这些:

netty-codec-4.1.4.Final.jar
netty-codec-http-4.1.4.Final.jar
netty-codec-http2-4.1.4.Final.jar
netty-codec-socks-4.1.4.Final.jar
netty-common-4.1.4.Final.jar
netty-handler-4.1.4.Final.jar
netty-transport-4.1.4.Final.jar

用这些文件来替换之前的netty-all-4.1.4.Final.jar。遇到了编译错误:

:app:compileDebugJavaWithJavac
Full recompilation is required because at least one of the classes of removed jar 'netty-all-4.1.4.Final.jar' requires it. Analysis took 0.364 secs.
/media/data/MyProjects/MyApplication/app/src/main/java/io/netty/example/http/snoop/myapplication/HttpClient.java:67: 错误: 无法访问ByteBufHolder
                        request.headers().set(HttpHeaderNames.HOST, host);
                               ^
  找不到io.netty.buffer.ByteBufHolder的类文件
/media/data/MyProjects/MyApplication/app/src/main/java/io/netty/example/http/snoop/myapplication/HttpClientInitializer.java:39: 错误: 无法访问ByteBufAllocator
            p.addLast(sslCtx.newHandler(ch.alloc()));
                                                ^
  找不到io.netty.buffer.ByteBufAllocator的类文件
/media/data/MyProjects/MyApplication/app/src/main/java/io/netty/example/http/snoop/myapplication/HttpClientHandler.java:48: 错误: 找不到符号
            String responseContent = content.content().toString(CharsetUtil.UTF_8);
                                            ^
  符号:   方法 content()
  位置: 类型为HttpContent的变量 content
注: /media/data/MyProjects/MyApplication/app/src/main/java/io/netty/example/http/snoop/myapplication/HttpClient.java使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
3 个错误
:app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

看来是少了一些东西了,ByteBufHolder。那就把netty-buffer-4.1.4.Final.jar也加进工程里。再次编译,继续出错,这是在产生APK时遇到了麻烦:

:app:transformResourcesWithMergeJavaResForDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/INDEX.LIST
      File1: /media/data/MyProjects/MyApplication/app/libs/netty-codec-4.1.4.Final.jar
      File2: /media/data/MyProjects/MyApplication/app/libs/netty-transport-4.1.4.Final.jar
      File3: /media/data/MyProjects/MyApplication/app/libs/netty-buffer-4.1.4.Final.jar
      File4: /media/data/MyProjects/MyApplication/app/libs/netty-codec-socks-4.1.4.Final.jar
      File5: /media/data/MyProjects/MyApplication/app/libs/netty-handler-4.1.4.Final.jar
      File6: /media/data/MyProjects/MyApplication/app/libs/netty-codec-http2-4.1.4.Final.jar
      File7: /media/data/MyProjects/MyApplication/app/libs/netty-codec-http-4.1.4.Final.jar


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 15.352 secs
Duplicate files copied in APK META-INF/INDEX.LIST
    File1: /media/data/MyProjects/MyApplication/app/libs/netty-codec-4.1.4.Final.jar
    File2: /media/data/MyProjects/MyApplication/app/libs/netty-transport-4.1.4.Final.jar
    File3: /media/data/MyProjects/MyApplication/app/libs/netty-buffer-4.1.4.Final.jar
    File4: /media/data/MyProjects/MyApplication/app/libs/netty-codec-socks-4.1.4.Final.jar
    File5: /media/data/MyProjects/MyApplication/app/libs/netty-handler-4.1.4.Final.jar
    File6: /media/data/MyProjects/MyApplication/app/libs/netty-codec-http2-4.1.4.Final.jar
    File7: /media/data/MyProjects/MyApplication/app/libs/netty-codec-http-4.1.4.Final.jar

11:03:39: External task execution finished 'assembleDebug'.

总是报Duplicate files copied in APK META-INF/INDEX.LIST的错误。这主要是因为这些jar文件里,不同的jar文件中的META-INF/INDEX.LIST包含了相同的内容所致。这需要在build.gradle中的android元素里添加如下的配置:

    packagingOptions {
        exclude 'META-INF/INDEX.LIST'
    }

再次编译,这次则是包Duplicate files copied in APK META-INF/io.netty.versions.properties。这证明了前面的方法是行之有效的,于是把META-INF/io.netty.versions.properties也加进packagingOptions的exclude列表:

    packagingOptions {
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/io.netty.versions.properties'
    }

再次编译,编译通过。但是在运行的时候遇到了一点小麻烦:

11:29:59.685 9005-9050/com.example.hanpfei0306.myapplication W/dalvikvm: threadid=11: thread exiting with uncaught exception (group=0x41959ce0)
08-11 11:29:59.685 9005-9050/com.example.hanpfei0306.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-1197
                                                                                     Process: com.example.hanpfei0306.myapplication, PID: 9005
                                                                                     java.lang.NoClassDefFoundError: io.netty.resolver.DefaultAddressResolverGroup
                                                                                         at io.netty.bootstrap.Bootstrap.<clinit>(Bootstrap.java:53)
                                                                                         at com.example.hanpfei0306.myapplication.HttpClient$1.run(HttpClient.java:57)

提示找不到class io.netty.resolver.DefaultAddressResolverGroup,看来我们的jar文件是加少了,把netty-resolver-4.1.4.Final.jar也加进来。终于,我们前面编写的HttpClient能够正常地跑起来了。经过一番裁剪,Netty的大小大概从3.4MB减小到2.9MB。

总结一下,若不用体型巨大的netty-all jar文件,则我们需要导入如下的这些jar文件以编译和运行Netty:

netty-buffer-4.1.4.Final.jar
netty-codec-4.1.4.Final.jar
netty-codec-http-4.1.4.Final.jar
netty-codec-http2-4.1.4.Final.jar
netty-codec-socks-4.1.4.Final.jar
netty-common-4.1.4.Final.jar
netty-handler-4.1.4.Final.jar
netty-resolver-4.1.4.Final.jar
netty-transport-4.1.4.Final.jar

TODO

Netty的诸多抽象,比如Bootstrap,Channel,EventLoopGroup,Handler,codec,Buffer等诸多高级特性,以及它的NIO接口,这里都没有涉及,线程模型也没有仔细厘清。这里只是最最简单的一个使用范例,要把Netty很好地应用在实际的项目中,还需要对Netty本身更深入的研究。

同时,对Netty的裁剪可能也过于粗糙,或许还有更多的东西可以裁剪掉,以减小最终的APP的大小。

要使用Netty来支持HTTP/2也还需要做更多的事情。

但窥探到Netty的灵活强大,还是让我们对这个库充满期待。

参考文档

多个jar包的合并

Netty4.x中文教程系列

netty-4-user-guide

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
威尔we 威尔we
3年前
Netty 高性能网络协议服务器开发
本文通过一个实例来讲解如何使用框架来开发网络协议服务器,项目使用工具来构建和运行,并且支持部署。项目代码已在GitHub开源,。Netty简介Netty是一个异步、事件驱动的网络应用框架,使用它可以快速开发出可维护良好的、高性能的网络协议服务器。它大幅简化和流程化了网络编程,比如TCP和UDP套接字服务器开发。难能
Stella981 Stella981
2年前
Netty序章之BIO NIO AIO演变
Netty序章之BIONIOAIO演变Netty是一个提供异步事件驱动的网络应用框架,用以快速开发高性能、高可靠的网络服务器和客户端程序。Netty简化了网络程序的开发,是很多框架和公司都在使用的技术。更是面试的加分项。Netty并非横空出世,它是在BIO,NIO,AIO演变中的产物,是一种
Stella981 Stella981
2年前
BIO、NIO、AIO系列二:Netty
一、概述Netty是一个Java的开源框架。提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。Netty是一个NIO客户端,服务端框架。允许快速简单的开发网络应用程序。例如:服务端和客户端之间的协议,它简化了网络编程规范。二、NIO开发的问题
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Stella981 Stella981
2年前
Netty 入门初体验
Netty简介Netty是一款异步的事件驱动的网络应用程序框架,支持快速开发可维护的高性能的面向协议的服务器和客户端。Netty主要是对java的nio包进行的封装为什么要使用Netty上面介绍到Netty是一款高性能的网络通讯框架,那么我们为什么要使用Netty,换句话说,
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Netty堆外内存泄露排查与总结
导读Netty是一个异步事件驱动的网络通信层框架,用于快速开发高可用高性能的服务端网络框架与客户端程序,它极大地简化了TCP和UDP套接字服务器等网络编程。Netty底层基于JDK的NIO,我们为什么不直接基于JDK的NIO或者其他NIO框架:1.使用JDK自带的NIO需要了解太多的概念,编程复杂。2
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这