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

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 

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是用在服务器的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值