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

miracleliu 2015-03-02 05:31:33

这几天想写个高性能的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();
}
}
}
...全文
168 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
miracleliu 2015-03-03
  • 打赏
  • 举报
回复
引用 1 楼 ygycomon 的回复:
客户端要什么高性能,客户端又不会有一大堆的并发,谈不上高性能,用bio都没关系。 nio是用在服务器的
现在有个需求就是要搞个高性能的客户端,请求一个url;
致知Fighting 2015-03-02
  • 打赏
  • 举报
回复
客户端要什么高性能,客户端又不会有一大堆的并发,谈不上高性能,用bio都没关系。 nio是用在服务器的

50,535

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧