基于Netty3的RPC架构笔记2之服务端与客户端
来源:互联网 发布:删除旧的windows文件夹 编辑:程序博客网 时间:2024/06/15 14:29
netty版本大致分为 netty3.x 和 netty4.x、netty5.x
netty可以运用在以下领域?
1分布式进程通信
例如: hadoop、dubbo、akka等具有分布式功能的框架,底层RPC通信都是基于netty实现的,这些框架使用的版本通常都还在用netty3.x
2、游戏服务器开发
最新的游戏服务器有部分公司可能已经开始采用netty4.x 或 netty5.x
1、netty服务端hello world案例
SimpleChannelHandler 处理消息接收和写
{
messageReceived接收消息
channelConnected新连接,通常用来检测IP是否是黑名单
channelDisconnected链接关闭,可以再用户断线的时候清楚用户的缓存数据等
}
2、netty客户端hello world案例
channelDisconnected与channelClosed的区别?
channelDisconnected只有在连接建立后断开才会调用
netty可以运用在以下领域?
1分布式进程通信
例如: hadoop、dubbo、akka等具有分布式功能的框架,底层RPC通信都是基于netty实现的,这些框架使用的版本通常都还在用netty3.x
2、游戏服务器开发
最新的游戏服务器有部分公司可能已经开始采用netty4.x 或 netty5.x
1、netty服务端hello world案例
SimpleChannelHandler 处理消息接收和写
{
messageReceived接收消息
channelConnected新连接,通常用来检测IP是否是黑名单
channelDisconnected链接关闭,可以再用户断线的时候清楚用户的缓存数据等
}
2、netty客户端hello world案例
channelDisconnected与channelClosed的区别?
channelDisconnected只有在连接建立后断开才会调用
channelClosed无论连接是否成功都会调用关闭资源
下面是服务端代码
package com.server;import java.net.InetSocketAddress;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.jboss.netty.bootstrap.ServerBootstrap;import org.jboss.netty.channel.ChannelPipeline;import org.jboss.netty.channel.ChannelPipelineFactory;import org.jboss.netty.channel.Channels;import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;import org.jboss.netty.handler.codec.string.StringDecoder;import org.jboss.netty.handler.codec.string.StringEncoder;/** * netty服务端入门 */public class Server {public static void main(String[] args) {//服务类ServerBootstrap bootstrap = new ServerBootstrap();//boss线程监听端口,worker线程负责数据读写ExecutorService boss = Executors.newCachedThreadPool();ExecutorService worker = Executors.newCachedThreadPool();//设置niosocket工厂bootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker));//设置管道的工厂bootstrap.setPipelineFactory(new ChannelPipelineFactory() {@Overridepublic ChannelPipeline getPipeline() throws Exception {ChannelPipeline pipeline = Channels.pipeline();pipeline.addLast("decoder", new StringDecoder());pipeline.addLast("encoder", new StringEncoder());pipeline.addLast("helloHandler", new HelloHandler());return pipeline;}});bootstrap.bind(new InetSocketAddress(10101));System.out.println("start!!!");}}上面是server信息,下面这个是处理绑定信息类
package com.server;import org.jboss.netty.channel.ChannelHandlerContext;import org.jboss.netty.channel.ChannelStateEvent;import org.jboss.netty.channel.ExceptionEvent;import org.jboss.netty.channel.MessageEvent;import org.jboss.netty.channel.SimpleChannelHandler;/** * 消息接受处理类 */public class HelloHandler extends SimpleChannelHandler {/** * 接收消息 */@Overridepublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {String s = (String) e.getMessage();System.out.println(s);//回写数据ctx.getChannel().write("hi");super.messageReceived(ctx, e);}/** * 捕获异常 */@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {System.out.println("exceptionCaught");super.exceptionCaught(ctx, e);}/** * 新连接 */@Overridepublic void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {System.out.println("channelConnected");super.channelConnected(ctx, e);}/** * 必须是链接已经建立,关闭通道的时候才会触发 */@Overridepublic void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {System.out.println("channelDisconnected");super.channelDisconnected(ctx, e);}/** * channel关闭的时候触发 */@Overridepublic void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {System.out.println("channelClosed");super.channelClosed(ctx, e);}}接下来是客户端服务代码
package com.client;import java.net.InetSocketAddress;import java.util.Scanner;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.jboss.netty.bootstrap.ClientBootstrap;import org.jboss.netty.channel.Channel;import org.jboss.netty.channel.ChannelFuture;import org.jboss.netty.channel.ChannelPipeline;import org.jboss.netty.channel.ChannelPipelineFactory;import org.jboss.netty.channel.Channels;import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;import org.jboss.netty.handler.codec.string.StringDecoder;import org.jboss.netty.handler.codec.string.StringEncoder;/** * netty客户端入门 * */public class Client {public static void main(String[] args) {//服务类ClientBootstrap bootstrap = new ClientBootstrap();//线程池ExecutorService boss = Executors.newCachedThreadPool();ExecutorService worker = Executors.newCachedThreadPool();//socket工厂bootstrap.setFactory(new NioClientSocketChannelFactory(boss, worker));//管道工厂bootstrap.setPipelineFactory(new ChannelPipelineFactory() {@Overridepublic ChannelPipeline getPipeline() throws Exception {ChannelPipeline pipeline = Channels.pipeline();pipeline.addLast("decoder", new StringDecoder());pipeline.addLast("encoder", new StringEncoder());pipeline.addLast("hiHandler", new HiHandler());return pipeline;}});//连接服务端ChannelFuture connect = bootstrap.connect(new InetSocketAddress("127.0.0.1", 10101));Channel channel = connect.getChannel();System.out.println("client start");Scanner scanner = new Scanner(System.in);while(true){System.out.println("请输入");channel.write(scanner.next());}}}客户端处理代码
package com.client;import org.jboss.netty.channel.ChannelHandlerContext;import org.jboss.netty.channel.ChannelStateEvent;import org.jboss.netty.channel.ExceptionEvent;import org.jboss.netty.channel.MessageEvent;import org.jboss.netty.channel.SimpleChannelHandler;/** * 消息接受处理类 * @author -琴兽- * */public class HiHandler extends SimpleChannelHandler {/** * 接收消息 */@Overridepublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {String s = (String) e.getMessage();System.out.println(s);super.messageReceived(ctx, e);}/** * 捕获异常 */@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {System.out.println("exceptionCaught");super.exceptionCaught(ctx, e);}/** * 新连接 */@Overridepublic void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {System.out.println("channelConnected");super.channelConnected(ctx, e);}/** * 必须是链接已经建立,关闭通道的时候才会触发 */@Overridepublic void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {System.out.println("channelDisconnected");super.channelDisconnected(ctx, e);}/** * channel关闭的时候触发 */@Overridepublic void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {System.out.println("channelClosed");super.channelClosed(ctx, e);}}
1 0
- 基于Netty3的RPC架构笔记2之服务端与客户端
- 基于Netty3的RPC架构笔记1之NIO
- 基于Netty3的RPC架构笔记3之线程模型源码分析
- Netty学习5-Netty3.X服务端与客户端
- 基于Netty5的RPC架构笔记4之案例讲解
- 基于Netty的RPC架构笔记5之心跳
- 客户端与服务端架构
- 基于Netty的RPC架构笔记6之protocol buff学习
- 基于Netty的RPC架构笔记7之自定义序列化协议
- 基于Netty的RPC架构笔记8之自定义数据包协议
- Fresco之客户端与服务端的交互
- 基于TCP的服务端/客户端
- Java网络编程基于TCP的客户端与服务端
- 基于tcp的socket通信(单个客户端与服务端)
- 基于socket的简单tcp客户端与服务端程序
- 【java基础:net】基于TCP的客户端与服务端Demonstration
- python笔记----客户端与服务端
- 2、netty3服务端入门代码示例
- Redis数据库连接类分布式版
- Redis- 对象的空转时长(总结)
- 树的统计count
- CentOS7 安装Node.js
- GRE-C
- 基于Netty3的RPC架构笔记2之服务端与客户端
- HDU 1597 二分+数学
- javaSE的几个基本问题
- CentOS里上传下载rzsz
- ThinkPhP 学习笔记 1.介绍及安装
- TensorFlow手写数字识别mnist example源码分析
- maya布尔运算遇到的问题
- Android界面UI控件的夜间模式及隐藏
- 多态