- 浏览: 949658 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
netty 网络通信示例一 :http://donald-draper.iteye.com/blog/2383326
netty 网络通信示例二:http://donald-draper.iteye.com/blog/2383328
netty 网络通信示例三:http://donald-draper.iteye.com/blog/2383392
netty 网络通信示例四:http://donald-draper.iteye.com/blog/2383472
前面文章我们在编码器与解码器的示例,及基于textline编解码器通信示例,今天我们通过
Netty来时实现一个简单的HTTP服务器:
HTTP服务器:
Http服务器处理器Initializer:
Http服务器处理器:
打开浏览器,访问地址:http://192.168.31.153:8080/
浏览器窗口显示Hello world!
netty 网络通信示例二:http://donald-draper.iteye.com/blog/2383328
netty 网络通信示例三:http://donald-draper.iteye.com/blog/2383392
netty 网络通信示例四:http://donald-draper.iteye.com/blog/2383472
前面文章我们在编码器与解码器的示例,及基于textline编解码器通信示例,今天我们通过
Netty来时实现一个简单的HTTP服务器:
HTTP服务器:
package netty.main.http; import java.net.InetSocketAddress; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.util.SelfSignedCertificate; import netty.initializer.http.HttpHelloWorldServerInitializer; /** * An HTTP server that sends back the content of the received HTTP request * in a pretty plaintext form. * * @author donald * 2017年6月30日 * 上午11:44:43 */ public final class HttpHelloWorldServer { private static final Logger log = LoggerFactory.getLogger(HttpHelloWorldServer.class); static final boolean SSL = System.getProperty("ssl") != null; private static final String ip = "192.168.31.153"; static final int port = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080")); public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_BACKLOG, 1024); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new HttpHelloWorldServerInitializer(sslCtx)); InetSocketAddress inetSocketAddress = new InetSocketAddress(ip,port); Channel ch = b.bind(inetSocketAddress).sync().channel(); log.info("=========HttpServer is start========="); System.err.println("Open your web browser and navigate to " + (SSL? "https" : "http") + "://"+ip+":" + port + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }
Http服务器处理器Initializer:
package netty.initializer.http; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.http.HttpServerExpectContinueHandler; import io.netty.handler.ssl.SslContext; import netty.handler.http.HttpHelloWorldServerHandler; /** * * @author donald * 2017年6月30日 * 上午11:42:49 */ public class HttpHelloWorldServerInitializer extends ChannelInitializer<SocketChannel> { private final SslContext sslCtx; public HttpHelloWorldServerInitializer(SslContext sslCtx) { this.sslCtx = sslCtx; } @Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } p.addLast(new HttpServerCodec()); p.addLast(new HttpServerExpectContinueHandler()); p.addLast(new HttpHelloWorldServerHandler()); } }
Http服务器处理器:
package netty.handler.http; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpUtil; import io.netty.handler.codec.http.HttpRequest; import io.netty.util.AsciiString; import static io.netty.handler.codec.http.HttpResponseStatus.*; import static io.netty.handler.codec.http.HttpVersion.*; public class HttpHelloWorldServerHandler extends ChannelInboundHandlerAdapter { private static final byte[] CONTENT = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' }; private static final AsciiString CONTENT_TYPE = new AsciiString("Content-Type"); private static final AsciiString CONTENT_LENGTH = new AsciiString("Content-Length"); private static final AsciiString CONNECTION = new AsciiString("Connection"); private static final AsciiString KEEP_ALIVE = new AsciiString("keep-alive"); @Override public void channelReadComplete(ChannelHandlerContext ctx) { ctx.flush(); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; boolean keepAlive = HttpUtil.isKeepAlive(req); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT)); response.headers().set(CONTENT_TYPE, "text/plain"); response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.write(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, KEEP_ALIVE); ctx.write(response); } } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } }
打开浏览器,访问地址:http://192.168.31.153:8080/
浏览器窗口显示Hello world!
发表评论
-
netty NioSocketChannel解析
2017-09-29 12:50 1230netty 抽象BootStrap定义:http://dona ... -
netty Pooled字节buf分配器
2017-09-28 13:00 1964netty 字节buf定义:http://donald-dra ... -
netty Unpooled字节buf分配器
2017-09-26 22:01 2354netty 字节buf定义:http://donald-dra ... -
netty 抽象字节buf分配器
2017-09-26 08:43 1255netty 字节buf定义:http:// ... -
netty 复合buf概念
2017-09-25 22:31 1249netty 字节buf定义:http://donald-dra ... -
netty 抽象字节buf引用计数器
2017-09-22 12:48 1517netty 字节buf定义:http://donald-dra ... -
netty 抽象字节buf解析
2017-09-22 09:00 1755netty 通道接口定义:http://donald-drap ... -
netty 资源泄漏探测器
2017-09-21 09:37 1322netty 通道接口定义:http://donald-drap ... -
netty 字节buf定义
2017-09-20 08:31 2747netty 通道接口定义:http://donald-drap ... -
netty 默认通道配置后续
2017-09-18 08:36 2091netty 通道接口定义:http://donald-drap ... -
netty 默认通道配置初始化
2017-09-17 22:51 1936netty 通道接口定义:http://donald-drap ... -
netty 通道配置接口定义
2017-09-17 14:51 1010netty 通道接口定义:http://donald-drap ... -
netty NioServerSocketChannel解析
2017-09-16 13:01 1806netty ServerBootStrap解析:http:// ... -
netty 抽象nio消息通道
2017-09-15 15:30 1157netty 通道接口定义:http:/ ... -
netty 抽象nio字节通道
2017-09-14 22:39 1140netty 通道接口定义:http:/ ... -
netty 抽象nio通道解析
2017-09-14 17:23 888netty 通道接口定义:http://donald-drap ... -
netty 抽象通道后续
2017-09-13 22:40 1234netty Inboudn/Outbound通道Inv ... -
netty 通道Outbound缓冲区
2017-09-13 14:31 2120netty 通道接口定义:http:/ ... -
netty 抽象Unsafe定义
2017-09-12 21:24 992netty 通道接口定义:http:/ ... -
netty 抽象通道初始化
2017-09-11 12:56 1786netty 管道线定义-ChannelPipeline:htt ...
相关推荐
1.该项目基于maven构建,相关博客 https://blog.csdn.net/qq_28898309/article/details/82957490 2.第一个示例 com.echo 一个最简单的netty应用 使用:运行EchoServer,打开cmd命令窗口。输入telnet localhost ...
无论是构建高性能的Web、游戏服务器、推送系统、RPC框架、消息中间件还是分布式大数据处理引擎,都离不开Netty,在整个行业中,Netty广泛而成功的应用,使其成为了Java高性能网络编程的卓绝框架。 Netty的现Tech...
11_Netty实现服务器端与客户端的长连接通信 12_Google Protobuf详解 13_定义Protobuf文件及消息详解 14_Protobuf完整实例详解 15_Protobuf集成Netty与多协议消息传递 16_Protobuf多协议消息支援与工程最佳实践 17_...
第11讲:Netty实现服务器端与客户端的长连接通信 第12讲:Google Protobuf详解 第13讲:定义Protobuf文件及消息详解 第14讲:Protobuf完整实例详解 第15讲:Protobuf集成Netty与多协议消息传递 第16讲:...
Netty允许基于其构建的程序的高性能和良好的可维护性。 Hydra出现在这里。Hydra使用构建器模式来简化套接字设置的过程。 Hydra带有一个方便的数据包系统,可让您轻松创建自己的数据包并通过Hydra为您创建的会话发送...
Netty是一个异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端。 链接 如何建造 有关构建和开发Netty的详细信息,请访问。 该页面仅提供非常基本的信息。 您需要以下内容来构建Netty...
20_通过Apache Thrift实现Java与Python的RPC调用 21_gRPC深入详解 22_gRPC实践 23_Gradle Wrapper在Gradle项目构建中的最佳实践 24_gRPC整合Gradle与代码生成 25_gRPC通信示例与JVM回调钩子 26_gRPC服务器流式调用...
它主要针对服务器端使用而开发,其中速度和低资源使用是关键因素,但也可用于构建客户端应用程序。 建立在 Netty 之上,专为高并发场景而设计,其中多个线程可以使用同一个客户端实例,而无需担心外部或内部同步,...
使用 JSON 通过加密通道进行通信的示例 Netty Spout 和 Netty 生产者的实现。 此存储库中的两个 Maven 项目: [netty-spout] 能够处理来自 Netty 客户端的直接连接的 Storm Spout 的实现。 通信使用 TLS。 在此项目...
http://maven.apache.org干净的编译: mvn -U clean compile运行应用程序(基于Spring Boot,使用netty作为Web服务器): mvn spring-boot:run启动后,该应用程序应该位于以下位置: http://localhost:8080...
注意:Jetty和Netty应该在同一服务器上运行,因为websocket URL是使用Jetty的主机名构建的。 这在客户端javascript(webRtcDemo.js)中进行了硬编码。 同样,Netty服务器的端口也应为9090,因为它也
#用法示例通过命令mvn spring-boot:run运行服务器或使用mvn package构建单个可执行jar文件运行单个jar java -jar wss-server.jar #客户客户端代码可以在#简要描述;简介这只是有关如何在Spring中使用Netty-...
这是一个基于SpringWebFlux和Netty的高性能,响应式API网关。 现在它仅支持HTTP协议转发,仅用于学习通信。 项目结构 Ship-admin:网关管理控制台,支持配置路由规则等。 船舶服务器:网关服务器,核心功能模块。 ...
Trireme 这是一组用于在Java内部运行node.js脚本的库。什么是Trireme? Trireme在JVM中运行Node.js脚本。 这很重要,因为那里有很多用Java构建的软件(包括... (包括使用Netty 4.0构建的示例适配器。) 沙箱支持Rhin
该命令将构建docker image netty-ssl-routing-proxy 。 需要-Pdocker配置文件才能将Netty的特定于平台的JNI切换到linux-x86_64。 配置 参见示例 文件“ keyStore”和“ trustStore”的路径必须是绝对的。 例如,...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
请参阅examples /目录以获取一些示例Wangle服务器和客户端 执照 Wangle是Apache 2.0许可的。 贡献 请参阅CONTRIBUTING文件以获取帮助。 文献资料 Wangle接口是异步的。 接口目前基于 ,但我们也在探索
用于将多个 Minecraft 服务器连接在一起的点对点网络 API。 提出了一个基于发布订阅的系统,以便于数据包交换。 下载 您可以在我的 jenkins 上下载构建: 可以在找到 Maven 存储库。 模块 该项目目前由三个模块...
它基于事件和异步,依托于全异步Java服务器Netty,并扩展了很多其他特性。 github地址:https://github.com/fengzhizi715/NetDiscovery 一. 爬虫框架的功能 爬虫框架包含爬虫引擎(SpiderEngine)和爬虫(Spider)。...
运行应用程序(基于Spring Boot,使用netty作为Web服务器): mvn spring-boot:run 启动后,该应用程序应该位于以下位置: http://localhost:8080 执行中 此应用程序提供了几个URL: 索引页: /或/thymeleaf ...