网络编程 -- 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资源). ( ...
随机推荐
- JDBC(3)—ResultSet结果集
简介:ResultSet:结果集.封装了使用JDBC进行查询的结果.Statement只能进行更新操作,所以使用ResultSet进行查询操作. 1.调用Statement对象的executeQuer ...
- Hessian学习总结(二)——使用hessian上传文件
hessian较早版本通过 byte[] 进行文件传输:4.0之后支持 InputStream 作为参数或返回值进行传输. 注意:hessian会读取整个文件,如果文件过大,会导致JVM内存溢出.可以 ...
- WordPress基础:极简手动安装教程
1.下载WordPress 2.将解压后的文件夹,放到网站根目录,并重命名为你喜欢的目录如:w, 3.重命名文件wp-config-sample.php 为 wp-config.php,并进行配置 4 ...
- tomcat支持https的server.xml配置
访问地址:https://127.0.0.1/testWeb/mySevlet?url=123&action=aaa server.xml: <?xml version='1.0' en ...
- Docker修改daemon.json后无法启动的问题
本文的运行环境为Centos 7.3,Docker与Kubernetes的安装方式见kubeadm安装kubernetes V1.11.1 集群 最近在整理Docker和Kubernetes中的日志与 ...
- “正在注册字体”问题解决
在win7下安装老软件,卡在"正在注册字体"了,检查发现是ocx注册有问题. 重写一个ocx注册的批处理就好了. 如: regsvr32 "C:\Program File ...
- AI通过了艺术创作图灵测试,你根本分不出来作者是不是人
各位geek朋友们,今年不用再看画了:近年来最大的艺术成就已经发生了. 这项艺术成就的诞生地,不是北京.新加坡.柏林郊区颜料四溅的画室中,不是威尼斯双年展上.请记住它出现的地点:美国新泽西州新布朗斯维 ...
- QueryRunner类实战
先上一个登录代码---判断登录是否成功 1.c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?&g ...
- 【NIO】Java NIO之缓冲
一.前言 在笔者打算学习Netty框架时,发现很有必要先学习NIO,因此便有了本博文,首先介绍的是NIO中的缓冲. 二.缓冲 2.1 层次结构图 除了布尔类型外,其他基本类型都有相对应的缓冲区类,其继 ...
- dyld`__abort_with_payload:
dyld`__abort_with_payload: 0x1030422f0 <+0>: mov x16, #0x209 0x1030422f4 <+4>: svc ...