当前位置:首页 > 后端开发 > netty 请求 转发http

netty 请求 转发http

6个月前 (05-22)49

packagecom.pt.utils;importio.netty.bootstrap.Bootstrap;importio.netty.channel.ChannelFuture;importio.netty.channel.ChannelInitializer;importio.netty.channel.ChannelOption;importio.netty.channel.EventLoopGroup;importio.netty.channel.nio.NioEventLoopGroup;importio.netty.channel.socket.SocketChannel;importio.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.http.*;importio.netty.util.concurrent.DefaultEventExecutorGroup;importio.netty.util.concurrent.EventExecutorGroup;importjava.net.URI;importjava.util.Map;/***@authorpanteng

* @description

* @date 17-3-20.*/

public classNonBlockHttpClient {public static EventLoopGroup workerGroup = new NioEventLoopGroup(1);public static Bootstrap b = newBootstrap();public static final EventExecutorGroup executor = new DefaultEventExecutorGroup(2);static{

b.group(workerGroup);

b.channel(NioSocketChannel.class);

b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,1000);

}public static Object lock = newObject();/*** 异步GET请求

*

*@paramurl

*@paramhead

*@paramhandler

*@return

*/

public static Boolean get(String url, Map head, finalHttpHandler handler) {try{

URI uri= newURI(url);

String domain=uri.getHost();

Integer port= uri.getPort() < 0 ? 80: uri.getPort();

DefaultFullHttpRequest request= newDefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString());if (head == null) {

request.headers().add("Host", domain);

request.headers().add("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0");

request.headers().add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

request.headers().add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");

request.headers().add("Connection", "keep-alive");

request.headers().add("Cache-Control", "max-age=0");

}else{for(Map.Entry entry : head.entrySet()) {

request.headers().add((String) entry.getKey(), entry.getValue());

}

}

ChannelInitializer channelInitializer= new ChannelInitializer() {

@Overrideprotected void initChannel(SocketChannel socketChannel) throwsException {//客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码

socketChannel.pipeline().addLast(newHttpResponseDecoder());//客户端发送的是httprequest,所以要使用HttpRequestEncoder进行编码

socketChannel.pipeline().addLast(newHttpRequestEncoder());

socketChannel.pipeline().addLast(executor,newGeneralHandler(handler));

}

};

ChannelFuture f;synchronized(lock) {

b.handler(channelInitializer);

f=b.connect(domain, port).sync();

}

f.channel().writeAndFlush(request);

}catch(Exception e) {

e.printStackTrace();

}return false;

}public static voidclose() {try{

executor.shutdownGracefully();

workerGroup.shutdownGracefully();

}catch(Exception e) {

e.printStackTrace();

}

}

}

作者:郭小闲
来源链接:https://blog.csdn.net/weixin_42472941/article/details/113007443

标签: Netty

“netty 请求 转发http” 的相关文章

Netty权威指南(三)Netty入门应用

Netty入门应用目录 回顾NIO开发步骤 一、依赖 二、Netty TimeServer...

netty系列之:请netty再爱UDT一次

文章目录 简介 netty对UDT的支持 搭建一个支持UDT的nett...

Netty封装NIO

Netty封装NIO

基于netty-4.1.40.Final-SNAPSHOT,梳理了下netty中几个核心的组件,便于以后快速回忆主线逻辑。 作者:cjbaxb 来...

SpringBoot集成Netty采集数据

近日,项目上有这么一个需求:需要从设备上采集数据,采用TCP协议传输。 因为采集频率和采集设备数量略带一点不确定性,以及NIO模式下的类比较繁杂,所...

Netty 实现HTTP文件服务器

一,需求 文件服务器使用HTTP协议对外提供服务。用户通过浏览器访问文件服务器,首先对URL进行检查,若失败返回403错误;若通过校验,以链接的方式打开当前目录,每个目录或文件都以...

Netty 框架学习 —— ByteBuf

Netty 框架学习 —— ByteBuf

概述 网络数据的基本单位总是字节,Java NIO 提供了 ByteBuffer 作为它的字节容器,但这个类的使用过于复杂。Netty 的 ByteBuf 具有卓越的功能性...

springboot集成netty使用udp协议实现消息接收与转发

springboot集成netty使用udp协议实现消息接收与转发

一、转发服务 1、创建NettyServer,使用线程池实现异步处理 ** * udp服务 */ public class Nett...

SuperSocket与Netty之实现protobuf协议,包括服务端和客户端

SuperSocket与Netty之实现protobuf协议,包括服务端和客户端

今天准备给大家介绍一个c#服务器框架(SuperSocket)和一个c#客户端框架(SuperSocket.ClientEngine)。这两个框架的作者是园区里面的江大渔。 首先感谢他的...

基于Netty实现UDP双向通信

基于Netty实现UDP双向通信

1、Channel继承关系 关于ChannelPipeline原理可参考:https://blog.csdn.net/qq_21033663/...

Netty 常用Api简单梳理

Netty 常用Api简单梳理

Bootstrap,ServerBootStrap 1.引导 一个Netty应用通常由一个BootStrap开始,主要作用是配置整个Ne...