网络编程 -- RPC实现原理 -- Netty -- 迭代版本V1 -- 入门应用
啦啦啦
V1——Netty入门应用
Class : NIOServerBootStrap
package lime.pri.limeNio.netty.netty01.server; import java.net.InetSocketAddress; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; public class NIOServerBootStrap { public static void main(String[] args) throws InterruptedException {
ServerBootstrap bootstrap = new ServerBootstrap();
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
bootstrap.group(boss, worker);
bootstrap.channel(NioServerSocketChannel.class); bootstrap.childHandler(new CustomServerChannelInitializer()); ChannelFuture channelFuture = bootstrap.bind(new InetSocketAddress(9999)).sync();
channelFuture.channel().closeFuture().sync();
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
Class : CustomServerChannelInitializer
package lime.pri.limeNio.netty.netty01.server; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel; public class CustomServerChannelInitializer extends ChannelInitializer<SocketChannel>{ @Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new CustomServerChannelHandlerAdapter());
} }
Class : CustomServerChannelHandlerAdapter
package lime.pri.limeNio.netty.netty01.server; import java.util.Date; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil; public class CustomServerChannelHandlerAdapter extends ChannelHandlerAdapter { /**
* 服务器和客户端会话异常
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// TODO Auto-generated method stub
super.exceptionCaught(ctx, cause);
} /**
* 当服务器与客户端联通时,通道被激活。
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
super.channelActive(ctx);
} /**
* 当服务器与客户端断开时,该方法被调用。
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
super.channelInactive(ctx);
} /**
* 通道读操作就绪,读取客户端消息
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buffer = (ByteBuf)msg;
String request = buffer.toString(CharsetUtil.UTF_8);
System.out.println("客户端请求数据:" + request); buffer.clear();
buffer.writeBytes(new Date().toString().getBytes());
ChannelFuture channelFuture = ctx.writeAndFlush(buffer);
channelFuture.addListener(ChannelFutureListener.CLOSE);
} }
Class :
package lime.pri.limeNio.netty.netty01.client; import java.io.IOException;
import java.net.InetSocketAddress; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel; public class NIOBootStrap { public static void main(String[] args) throws IOException, InterruptedException {
Bootstrap bootstrap = new Bootstrap();
EventLoopGroup worker = new NioEventLoopGroup();
bootstrap.group(worker);
bootstrap.channel(NioSocketChannel.class);
bootstrap.handler(new CustomClientChannelInitializer());
ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress("127.0.0.1", 9999)).sync();
channelFuture.channel().closeFuture().sync();
worker.shutdownGracefully();
}
}
Class : CustomClientChannelInitializer
package lime.pri.limeNio.netty.netty01.client; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel; public class CustomClientChannelInitializer extends ChannelInitializer<SocketChannel>{ @Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new CustomClientChannelHandlerAdapter());
} }
Class : CustomClientChannelHandlerAdapter
package lime.pri.limeNio.netty.netty01.client; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil; public class CustomClientChannelHandlerAdapter extends ChannelHandlerAdapter { /**
* 服务器和客户端会话异常
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// TODO Auto-generated method stub
super.exceptionCaught(ctx, cause);
} /**
* 当服务器与客户端联通时,通道被激活。
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ByteBuf buffer = Unpooled.buffer();
ctx.writeAndFlush(buffer.writeBytes("Query Date".getBytes()));
} /**
* 当服务器与客户端断开时,该方法被调用。
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
super.channelInactive(ctx);
} /**
* 通道多操作就绪,读取服务端消息
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("服务端响应数据:" + ((ByteBuf) msg).toString(CharsetUtil.UTF_8));
// 会话关闭操作由服务端启动,客户端不主动关闭会话。
} }
Console : Server
客户端请求数据:Query Date
Console : Client
服务端响应数据:Sat Jun 24 17:43:41 CST 2017
啦啦啦
网络编程 -- RPC实现原理 -- Netty -- 迭代版本V1 -- 入门应用的更多相关文章
- 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V2 -- 对象传输
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——Netty -- 使用序列化和反序列化在网络上传输对象:需要实现 java.io.Serializable 接口 只能传输( ByteBuf ...
- 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V3 -- 编码解码
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——Netty -- pipeline.addLast(io.netty.handler.codec.MessageToMessageCodec ...
- 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V4 -- 粘包拆包
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——Netty -- new LengthFieldPrepender(2) : 设置数据包 2 字节的特征码 new LengthFieldB ...
- 网络编程 -- RPC实现原理 -- 目录
-- 啦啦啦 -- 网络编程 -- RPC实现原理 -- NIO单线程 网络编程 -- RPC实现原理 -- NIO多线程 -- 迭代版本V1 网络编程 -- RPC实现原理 -- NIO多线程 -- ...
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V1 -- 本地方法调用
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V1——RPC -- 本地方法调用:不通过网络 入门 1. RPCObjectProxy rpcObjectProxy = new RPCObjec ...
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V2 -- 本地方法调用 整合 Spring
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——RPC -- 本地方法调用 + Spring 1. 配置applicationContext.xml文件 注入 bean 及 管理 bean ...
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V3 -- 远程方法调用 整合 Spring
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V3——RPC -- 远程方法调用 及 null的传输 + Spring 服务提供商: 1. 配置 rpc03_server.xml 注入 服务提供 ...
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V4 -- 远程方法调用 整合 Spring 自动注册
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V4——RPC -- 远程方法调用 + Spring 自动注册 服务提供商: 1. 配置 rpc04_server.xml 注入 服务提供商 rpc ...
- 网络编程 -- RPC实现原理 -- NIO多线程 -- 迭代版本V1
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V1——设置标识变量selectionKey.attach(true);只处理一次(会一直循环遍历selectionKeys,占用CPU资源). ( ...
随机推荐
- unity操作Hierarchy视图下同名的对象
上周遇到了一个令我尴尬的问题,在同一个场景内有了两个名字相同的对象,给个形象化的栗子: 场景内有橱窗,橱窗是模型,窗户是可以打开的[点击控制],窗户可以控制打开和关闭的.然后我就选用了一个保守的方式进 ...
- java合并单元格同时导出excel
POI进行跨行需要用到对象HSSFSheet对象,现在就当我们程序已经定义了一个HSSFSheet对象sheet. 跨第1行第1个到第2个单元格的操作为 sheet.addMergedRegion(n ...
- C# Queue 和Stack的实现
Queue 和Stack的使用就不用多说吧,一个是先进先出,一个是后进先出. 这里我主要关注其实现原理. queue的实现如下: public class Queue<T> : IEnum ...
- windows多线程同步--临界区
推荐参考博客:秒杀多线程第五篇 经典线程同步 关键段CS 关于临界区的观念,一般操作系统书上面都有. 适用范围:它只能同步一个进程中的线程,不能跨进程同步.一般用它来做单个进程内的代码快同步,效率 ...
- 以太坊(Ethereum)智能合约NodeJS/Web3 使用
一.概述 运行环境:Node.js.npm.Truffle.Solidity等 root@keke:~/go-ethereum# node -v v8.9.4 root@keke:~/go-ether ...
- [k8s]kube-dns/dashboard排错历险记(含sa加载用法/集群搭建)
kube-dns原理 参考: 组件架构看这个就够了 http://cizixs.com/2017/04/11/kubernetes-intro-kube-dns 设置细节看这个就够了 http://b ...
- 使用ffmpeg 推流
1.编译ffmpeg http://www.linuxidc.com/Linux/2014-11/109840.htm http://www.linuxidc.com/Linux/2013-02/78 ...
- 不同局域网中同一IP地址的计算机怎么通信的
1.IP地址在192.--.255之内的是私有地址,即192.168.1.56的电脑a是不能直接与192.168.1.56的电脑b进行通信的.他们需要用到NAT技术,即网络地址转换.2.NAT的作用是 ...
- win10安装windows live writer 错误:OnCatalogResult:0x80190194
到官网下载了一个在线安装程序,可是一运行就提示无法安装,显式错误"OnCatalogResult:0x80190194",如下图所示 找到windows live安装程序的安装日志 ...
- linux每日命令(17):which命令
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate 配合数据库查看文件位置. f ...