SparkRPC源码分析之Netty基础知识扫盲
在上面三篇文章中,我们介绍了Spark如何创建一个客户端和如何创建一个服务端。那么这些东西是什么呢?这就需要了解netty,而我本人是对netty不了解的。因此简单地看了一下netty需要的东西,写了以下简单的netty入门级程序。仅供参考
服务端代码:
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @ClassName MyTestServer
* @Author LWS
* @Date 2019/5/10 15:45
* @Version 1.0
**/
public class MyTestServer {
public static void main(String[] args) {
ServerBootstrap bootstrap = new ServerBootstrap();
ExecutorService bossGroup = Executors.newCachedThreadPool();
ExecutorService workGroup = Executors.newCachedThreadPool();
bootstrap.setFactory(new NioServerSocketChannelFactory(bossGroup,workGroup));
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("encode",new StringEncoder());
pipeline.addLast("decode",new StringDecoder());
pipeline.addLast("MyHandeler",new MyHandeler());
return pipeline;
}
});
bootstrap.bind(new InetSocketAddress("127.0.0.1",8000));
}
}
class MyHandeler extends SimpleChannelHandler{
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
System.out.println("服务端收到消息"+e.getMessage());
super.messageReceived(ctx, e);
}
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("服务端管道已连接");
super.channelConnected( ctx, e);
}
@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("服务端管道已断开");
super.channelDisconnected( ctx, e);
}
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("服务端管道已关闭");
super.channelClosed( ctx, e);
}
}
客户端代码
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import java.net.InetSocketAddress;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @ClassName MyTestClient
* @Author LWS
* @Date 2019/5/10 16:12
* @Version 1.0
**/
public class MyTestClient {
public static void main(String[] args) {
ClientBootstrap bootstrap = new ClientBootstrap();
ExecutorService bossGroup = Executors.newCachedThreadPool();
ExecutorService workGroup = Executors.newCachedThreadPool();
bootstrap.setFactory(new NioClientSocketChannelFactory(bossGroup,workGroup));
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("encode",new StringEncoder());
pipeline.addLast("decode",new StringDecoder());
pipeline.addLast("MyClientHandeler",new MyClientHandeler());
return pipeline;
}
});
ChannelFuture connect = bootstrap.connect(new InetSocketAddress("127.0.0.1", 8000));
Channel channel = connect.getChannel();
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("请输入需要发送的内容");
ChannelFuture write = channel.write(scanner.next());
if(write.awaitUninterruptibly(10000)){
System.out.println("发送完成!");
}
}
}
}
class MyClientHandeler extends SimpleChannelHandler {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
System.out.println("客户端收到消息"+e.getMessage());
super.messageReceived(ctx, e);
}
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("客户端管道已连接");
super.channelConnected( ctx, e);
}
@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("客户端管道已断开");
super.channelDisconnected( ctx, e);
}
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("客户端管道已关闭");
super.channelClosed( ctx, e);
}
}
这里呢可以看出以下的一些内容
- ServerBootStrap和ClientBootstrap是引导器,相当于一个容器,可以在里面配置通信时的各种参数
- Channel用于提供一些网络的IO操作,包含一些读、写、绑定等操作
- ChannelHandler这个组件内为channel的各种事件提供了处理逻辑,也就是主要业务逻辑写在该组建内。
- ChannelFuture 预先给一个返回的结果,等到处理完毕再调用监听器的回到函数处理这个结果。
好了由于本文呢仅仅是对netty进行基础扫盲,并不是要深入地去解析netty所以就到此结束。
ade~