SparkRPC源码分析之Netty基础知识扫盲

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);
    }
}

这里呢可以看出以下的一些内容

  1. ServerBootStrap和ClientBootstrap是引导器,相当于一个容器,可以在里面配置通信时的各种参数
  2. Channel用于提供一些网络的IO操作,包含一些读、写、绑定等操作
  3. ChannelHandler这个组件内为channel的各种事件提供了处理逻辑,也就是主要业务逻辑写在该组建内。
  4. ChannelFuture 预先给一个返回的结果,等到处理完毕再调用监听器的回到函数处理这个结果。

好了由于本文呢仅仅是对netty进行基础扫盲,并不是要深入地去解析netty所以就到此结束。
ade~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天心有情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值