当前位置: 代码迷 >> Java相关 >> netty/nio到底能做啥 ?netty如何写高性能http客户端
  详细解决方案

netty/nio到底能做啥 ?netty如何写高性能http客户端

热度:549   发布时间:2016-04-22 20:39:04.0
netty/nio到底能做啥 ?netty怎么写高性能http客户端?
本帖最后由 miraclestar 于 2015-03-02 17:36:39 编辑

这几天想写个高性能的http客户端;看了几天还是没搞明白

我改了下netty的demo,结果运行都不对,不知道线程应该写在哪?求大神指教;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpVersion;

import java.net.URI;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NettyClient {

    private static Logger log = LoggerFactory.getLogger(NettyClient.class);
    private static ExecutorService executorService = Executors.newFixedThreadPool(16);
  public static String urlHost = "http://www.baidu.com";
    public static String[] uris =    {"id=288746069&ip=12.13.123.12&"};

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
         urlRequest(urlHost);
        long end = System.currentTimeMillis();
        System.out.println("time use:" + (end - start) + "ms");
    }

    public static void urlRequest(String URL) throws Exception {
        URI uri = new URI(URL);
        // String scheme = "http";
        String host = uri.getHost();
        int port = 80;
     
        // Configure the client.
        EventLoopGroup group = new NioEventLoopGroup();
        try {

            for (int i = 0; i < 5; i++) {
                executorService.execute(new ConnectThread(uri, host, port, group, uris[i]));
            }

        } finally {
            // Shut down executor threads to exit.
            group.shutdownGracefully();
        }
}
    }


线程
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.example.http.snoop.HttpSnoopClientInitializer;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpVersion;

import java.net.URI;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConnectThread implements Runnable {

    private static Logger log = LoggerFactory.getLogger(ConnectThread.class);
    private URI uri;
    private String host;
    private int port;
    private EventLoopGroup group;
    private String urlQuery;

    public ConnectThread(URI uri, String host, int port, EventLoopGroup group, String urlQuery) {
        this.uri = uri;
        this.host = host;
        this.port = port;
        this.group = group;
        this.urlQuery = urlQuery;
    }

    @Override
    public void run() {

        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .option(ChannelOption.SO_TIMEOUT, 1000)
                    .handler(new HttpSnoopClientInitializer(null));
            long start = System.currentTimeMillis();
            // Make the connection attempt.
            Channel ch = b.connect(host, port).sync().channel();
            // Prepare the HTTP request.

            HttpRequest request = new DefaultFullHttpRequest(
                    HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath() + "?" + urlQuery);
            request.headers().set(HttpHeaders.Names.HOST, host);
            request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
            // Send the HTTP request.
            ch.writeAndFlush(request);
            long end = System.currentTimeMillis();
            System.out.println("time use in loop:" + (end - start) + "ms");
            // Wait for the server to close the connection.
            ch.closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

------解决思路----------------------
客户端要什么高性能,客户端又不会有一大堆的并发,谈不上高性能,用bio都没关系。

nio是用在服务器的
  相关解决方案