今天遇到了一個(gè)廣告網(wǎng)絡(luò)比較現(xiàn)實(shí)的需求,如下:
最為一個(gè)廣告服務(wù)端,可以從publisher的app接收到很多的加載廣告的請(qǐng)求。。。這個(gè)時(shí)候可以將這些請(qǐng)求的數(shù)據(jù)發(fā)給一些中間的機(jī)構(gòu)(exchange),然后由他們返回廣告的數(shù)據(jù)。。。因?yàn)檎?qǐng)求量較大,而且要保證延遲不能太高,所以這里與這些中間機(jī)構(gòu)進(jìn)行通信的時(shí)候就只能采用長(zhǎng)連接的方式了,不能每次請(qǐng)求都生成一次連接來(lái)進(jìn)行http請(qǐng)求。。。
其實(shí)以前一般用netty開(kāi)發(fā)都是作為服務(wù)端來(lái)的大致思路如下:
?。?)創(chuàng)建一個(gè)eventLoopGroup,用于維護(hù)nio的io事件
?。?)創(chuàng)建一個(gè)niosocketchanel,然后將其注冊(cè)到eventLoopGroup上面去,并未channel設(shè)置初始化的handler
(3)調(diào)用channel的connect方法發(fā)起與遠(yuǎn)端的連接請(qǐng)求
?。?)當(dāng)鏈接建立以后,剛剛提到的初始化handler將會(huì)用于響應(yīng),為channel添加http的decode與encode的handler。。
(5)最后就可以開(kāi)始進(jìn)行http通信了。。
當(dāng)然這里就不要主動(dòng)的去斷開(kāi)channel了,斷開(kāi)還是讓對(duì)方的服務(wù)器去做吧,或者超時(shí),或者什么的。。反正客戶端不會(huì)主動(dòng)斷開(kāi)。。。
其實(shí)只要上面的步驟想出來(lái),接下來(lái)寫(xiě)代碼用netty來(lái)實(shí)現(xiàn)一個(gè)基于長(zhǎng)連接的http客戶端還算是很簡(jiǎn)單 的。。直接上代碼吧:
[java] view plain copypackage fjs;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.QueryStringEncoder;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
public class Fjs {
public static AtomicInteger number = new AtomicInteger(0);
public static AtomicLong time = new AtomicLong(0);
public static void doIt(Channel channel) {
if (number.get() 《 50) {
number.incrementAndGet();
time.set(System.currentTimeMillis());
QueryStringEncoder encoder = new QueryStringEncoder(“http://www.baidu.com/oapi/reqAd.jsp?pub=923875870&adspace=65826983&adcount=1&response=HTML&devip=22.56.22.66&user=900&format=IMG&position=top&height=&width=&device=Mozilla%2F5.0%20%28Linux%3B%20Android%204.2.1%3B%20en-us%3B%20Nexus%204%20Build%2FJOP40D%29%20AppleWebKit%2F535.19%20%28KHTML%2C%20like%20Gecko%29%20Chrome%2F18.0.1025.166%20Mobile%20Safari%2F535.19&beacon=TRUE&phpsnip=104”);
URI uriGet = null;
try {
uriGet = new URI(encoder.toString());
} catch (URISyntaxException e) {
System.out.println(“我擦,,,,”);
}
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriGet.toASCIIString());
channel.pipeline().write(request);
channel.flush();
} else {
System.out.println(“over”);
}
}
public static void main(String args[]) throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup();
NioSocketChannel channel = new NioSocketChannel(); //創(chuàng)建一個(gè)channel,待會(huì)用它來(lái)發(fā)起鏈接
channel.pipeline().addFirst(new InitHandler()); //為這個(gè)channel添加一個(gè)初始化的handler,用于響應(yīng)待會(huì)channel建立成功
group.register(channel); //注冊(cè)這個(gè)channel
channel.connect(new InetSocketAddress(“www.baidu.com”, 80)); //調(diào)用connect方法
Thread.currentThread().sleep(Long.MAX_VALUE);
}
public static class InitHandler implements ChannelInboundHandler {
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
}
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
}
public void channelRegistered(ChannelHandlerContext ctx)
throws Exception {
// TODO Auto-generated method stub
}
public void channelUnregistered(ChannelHandlerContext ctx)
throws Exception {
// TODO Auto-generated method stub
}
評(píng)論
查看更多