手写RPC框架(九)

RPC框架v2.0netty版

​ v2.0 netty版的话就是实现一个最简单的netty版本 能实现这边的客户端发信息给服务端 服务端能收到并回应

技术选型

后续进行了大量更新

  • 网络传输:netty
  • 序列化:java自带序列化/protobuf/kyro
  • 注册中心:zookeeper(尝试去实现一个单机版zk?)/nacos
  • 注解开发(尝试)补丁选项

客户端

用户客户端启动类

//客户端启动类
public class NettyClientCall {
    public static void main(String[] args) throws InterruptedException {
        NettyClientBootStrap.start("127.0.0.1",6668);
    }
}

客户端引导启动类

package service.netty_bootstrap;


import consumer.bootstrap.netty.NettyConsumerBootStrap20;

public class NettyClientBootStrap {
    public static void start(String address, int port) throws InterruptedException {
        NettyConsumerBootStrap20.main(new String[]{address, String.valueOf(port)});
    }
}

消费者启动类

package consumer.bootstrap.netty;

import consumer.netty.NettyClient20;

/*
    以netty为网络编程框架的消费者端启动类
 */
//进行启动 提供类的方式即可
public class NettyConsumerBootStrap20 {
    public static void main(String[] args) throws InterruptedException {
        NettyClient20.start(args[0], Integer.parseInt(args[1]));
    }
}

消费者实际启动类

package consumer.netty;

import consumer.netty_client_handler.NettyClientHandler01;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;


//实际客户端启动类
public class NettyClient20 {
    public static void start(String hostName, int port) throws InterruptedException {
        Bootstrap bootstrap = new Bootstrap();
        EventLoopGroup workGroup = new NioEventLoopGroup();

        try {
            bootstrap.group(workGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            pipeline.addLast(new NettyClientHandler01());
                        }
                    });

            ChannelFuture channelFuture = bootstrap.connect(hostName, port).sync();

            channelFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    if (channelFuture.isSuccess()) {
                        System.out.println("连接"+hostName+":"+port+"成功");
                    }
                    else
                    {
                        System.out.println("连接"+hostName+":"+port+"失败");
                    }
                }
            });

            //监听关闭事件,本来是异步的,现在转换为同步事件
            channelFuture.channel().closeFuture().sync();
        } finally
        {
            //优雅的关闭 group
            workGroup.shutdownGracefully();
        }
    }
}

消费者处理器

package consumer.netty_client_handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

public class NettyClientHandler01 extends ChannelInboundHandlerAdapter {

    //通道就绪就会发的信息
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(Unpooled.copiedBuffer("你好,服务端", CharsetUtil.UTF_8));
    }

    //这个是收到信息
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf)msg;
        System.out.println("收到来自"+ctx.channel().remoteAddress()+"的消息"+buf.toString(CharsetUtil.UTF_8));
    }
}

服务端

用户服务端启动类

package service.netty_call;

import service.netty_bootstrap.NettyServerBootStrap;

//启动类 给定对应的端口 进行启动并监听
public class NettyServerCall {
    public static void main(String[] args) throws InterruptedException {
        NettyServerBootStrap.start("127.0.0.1",6668);
    }
}

服务端引导启动类

package service.netty_bootstrap;

import provider.bootstrap.netty.NettyProviderBootStrap20;

public class NettyServerBootStrap {
    public static void start(String address,int port) throws InterruptedException {
        NettyProviderBootStrap20.main(new String[]{address, String.valueOf(port)});
    }
}

提供者启动类

package provider.bootstrap.netty;

import provider.netty.NettyServer20;

/*
    以netty为网络编程框架的服务提供端启动类
 */
public class NettyProviderBootStrap20 {
    public static void main(String[] args) throws InterruptedException {
        //传入要绑定的ip和端口
        NettyServer20.start(args[0], Integer.parseInt(args[1]));
    }
}

服务提供者实际启动类

package provider.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import provider.netty_server_handler.NettyServerHandler01;


//简单实现  主要还是进行了一段回想
public class NettyServer20 {
    public static void start(String hostName,int port) throws InterruptedException {
        //该方法完成NettyServer的初始化  好好想想看 该怎么完成这个
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workGroup = new NioEventLoopGroup();
        try {
            serverBootstrap.group(bossGroup,workGroup)
                    .channel(NioServerSocketChannel.class) //自身所实现的通道
                    .option(ChannelOption.SO_BACKLOG,128) //设置线程队列得到的连接个数
                    .childOption(ChannelOption.SO_KEEPALIVE,true) //设置保持活动连接状态
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            //每个用户连接上都会进行初始化
                            System.out.println("客户socketChannel hashcode="+socketChannel.hashCode());
                            ChannelPipeline pipeline = socketChannel.pipeline();//每个通道都对应一个管道 将处理器往管道里放
                            pipeline.addLast(new NettyServerHandler01());
                        }
                    });

            System.out.println("服务器 is ready");

            //连接 同步
            ChannelFuture cf = serverBootstrap.bind(hostName, port).sync();

            cf.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (future.isSuccess()) {
                        System.out.println("监听端口"+port+"成功");
                    }
                    else
                    {
                        System.out.println("监听端口"+port+"失败");
                    }
                }
            });

            //对关闭通道进行监听
            cf.channel().closeFuture().sync();
        } finally {
            //优雅的关闭两个集群
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
}

服务提供者处理器

package provider.netty_server_handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

//实现简单的服务注册和回写
public class NettyServerHandler01 extends ChannelInboundHandlerAdapter {

    //读取数据
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //将信息进行读取 直接这样就可以了
        ByteBuf buf = (ByteBuf) msg;
        System.out.println("客户端发送消息是:"+ buf.toString(CharsetUtil.UTF_8));
        System.out.println("客户端地址:"+ctx.channel().remoteAddress());
    }

    //数据读取完毕
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        //读取完毕进行回显  写回并刷新
        ctx.writeAndFlush(Unpooled.copiedBuffer("success", CharsetUtil.UTF_8));
    }

    //捕获异常
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //异常处理 首先先将通道的上下文关闭  每个ctx对应的就是handler本身
        ctx.close();
        cause.printStackTrace();
    }
}

序列化

还是选用了自带的序列化,之后会对目前的序列化进行优化

依赖引入

<!--引入netty的依赖-->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.66.Final</version>
        </dependency>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值