第二课 Netty服务端
来源:互联网 发布:酒瓶设计软件 编辑:程序博客网 时间:2024/06/03 06:30
netty应用的领域
1分布式进程通信
例如: hadoop、dubbo、akka等具有分布式功能的框架,底层RPC通信都是基于netty实现的,这些框架使用的版本通常都还在用netty3.x
2、游戏服务器开发
最新的游戏服务器有部分公司可能已经开始采用netty4.x 或 netty5.x
代码示例
public class Server { public static void main(String[] args) { ServerBootstrap serverBootstrap = new ServerBootstrap(); ExecutorService boss = Executors.newCachedThreadPool(); ExecutorService worker = Executors.newCachedThreadPool(); //设置NioSocket工厂 serverBootstrap.setFactory(new NioServerSocketChannelFactory(boss,worker)); //设置管道工厂 serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { //创建管道 ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); pipeline.addLast("ServerHandler", new ServerHandler()); return pipeline; } }); serverBootstrap.bind(new InetSocketAddress(8000)); System.out.println("NIOServer started!!!!!!!!!!!"); }}
public class ServerHandler extends SimpleChannelHandler{ @Override public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channelClosed"); super.channelClosed(ctx, e); } @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent event) throws Exception { System.out.println("channelConnected"+event.getChannel().getRemoteAddress()); super.channelConnected(ctx, event); } @Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channelDisconnected"); super.channelDisconnected(ctx, e); } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { System.out.println("exceptionCaught"); super.exceptionCaught(ctx, e); } @Override public void messageReceived(ChannelHandlerContext context, MessageEvent message) throws Exception { String buffer = (String) message.getMessage(); System.out.println("received message:"+buffer); ChannelBuffer buffer2 = ChannelBuffers.copiedBuffer("Hi".getBytes()); context.getChannel().write(buffer2); super.messageReceived(context, message); }}
阅读全文
0 0
- 第二课 Netty服务端
- netty 服务端
- Netty创建upd服务端
- netty的tcp服务端
- dubbo服务端之netty
- netty 服务端连接分析
- Netty系列之Netty 服务端创建
- Netty系列之Netty 服务端创建
- COPY Netty系列之Netty 服务端创建
- Netty初探-netty服务端和客户端demo
- Netty系列之Netty 服务端创建
- Netty系列之Netty 服务端创建
- Netty系列之Netty 服务端创建
- Netty初探-netty服务端和客户端demo
- Netty系列之Netty 服务端创建
- netty 服务端作为客户端跳转请求服务端
- netty的websocket服务端开发
- netty服务端开发-绑定监听
- path
- Python 编程进阶经典算法逻辑编程
- 导向滤波小结:从导向滤波(guided filter)到快速导向滤波(fast guide filter)的原理,应用及opencv实现代码
- shell基础
- 排序算法-nlogn级别
- 第二课 Netty服务端
- shell基础命令
- 线程安全及解决机制简介
- oAuth2 feign 授权模式
- shell 展开
- 拆分窗口
- python中列表的基本操作
- Wind River workbench介绍
- org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletCon