JAVA RPC (九) netty服务端解析
源码地址:https://gitee.com/a1234567891/koalas-rpc
企业生产级百亿日PV高可用可拓展的RPC框架。理论上并发数量接近服务器带宽,客户端采用thrift协议,服务端支持netty和thrift的TThreadedSelectorServer半同步半异步线程模型,支持动态扩容,服务上下线,权重动态,可用性配置,页面流量统计,支持trace跟踪等,天然接入cat支持数据大盘展示等,持续为个人以及中小型公司提供可靠的RPC框架技术方案,贴上
@Override
public void run() {
try {
if (Epoll.isAvailable ()) {
bossGroup = new EpollEventLoopGroup (serverPublisher.bossThreadCount==0?AbstractKoalsServerPublisher.DEFAULT_EVENT_LOOP_THREADS:serverPublisher.bossThreadCount);
workerGroup = new EpollEventLoopGroup ( serverPublisher.workThreadCount==0? AbstractKoalsServerPublisher.DEFAULT_EVENT_LOOP_THREADS*2:serverPublisher.workThreadCount);
} else {
bossGroup = new NioEventLoopGroup (serverPublisher.bossThreadCount==0?AbstractKoalsServerPublisher.DEFAULT_EVENT_LOOP_THREADS:serverPublisher.bossThreadCount);
workerGroup = new NioEventLoopGroup ( serverPublisher.workThreadCount==0? AbstractKoalsServerPublisher.DEFAULT_EVENT_LOOP_THREADS*2:serverPublisher.workThreadCount );
}
executorService = KoalasThreadedSelectorWorkerExcutorUtil.getWorkerExecutorWithQueue (serverPublisher.koalasThreadCount==0?AbstractKoalsServerPublisher.DEFAULT_KOALAS_THREADS:serverPublisher.koalasThreadCount,serverPublisher.koalasThreadCount==0?AbstractKoalsServerPublisher.DEFAULT_KOALAS_THREADS:serverPublisher.koalasThreadCount,serverPublisher.workQueue,new KoalasDefaultThreadFactory (serverPublisher.serviceInterface.getName ())); ServerBootstrap b = new ServerBootstrap ();
b.group ( bossGroup, workerGroup ).channel ( workerGroup instanceof EpollEventLoopGroup ? EpollServerSocketChannel.class : NioServerSocketChannel.class )
.handler ( new LoggingHandler ( LogLevel.INFO ) )
.childHandler ( new NettyServerInitiator (serverPublisher,executorService))
.option ( ChannelOption.SO_BACKLOG, 1024 )
.option ( ChannelOption.SO_REUSEADDR, true )
.option ( ChannelOption.SO_KEEPALIVE, true );
Channel ch = b.bind ( serverPublisher.port ).sync ().channel ();
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run(){
logger.info ( "Shutdown by Runtime" );
if(zookeeperServer != null){
zookeeperServer.destroy ();
}
logger.info ( "wait for service over 3000ms" );
try {
Thread.sleep ( 3000 );
} catch (Exception e) {
}
if(executorService!=null){
executorService.shutdown ();
}
if(bossGroup != null) bossGroup.shutdownGracefully ();
if(workerGroup != null) workerGroup.shutdownGracefully ();
}
}); if(StringUtils.isNotEmpty ( serverPublisher.zkpath )){
ZookServerConfig zookServerConfig = new ZookServerConfig ( serverPublisher.zkpath,serverPublisher.serviceInterface.getName (),serverPublisher.env,serverPublisher.port,serverPublisher.weight,"netty" );
zookeeperServer = new ZookeeperServer ( zookServerConfig );
zookeeperServer.init ();
}
} catch ( Exception e){
logger.error ( "NettyServer start faid !",e );
if(bossGroup != null) bossGroup.shutdownGracefully ();
if(workerGroup != null) workerGroup.shutdownGracefully ();
} logger.info("netty server init success server={}",serverPublisher); }
首先开启NIO服务,由系统内核来判断是否支持epoll-EpollEventLoopGroup,如果不支持epoll采用IO多路复用的方式EpollEventLoopGroup,然后声明一个用户自定义线程池,这里有不清楚的读者肯定会问,netty本身支持连接线程和IO线程,为什么还要自定义声明自定义线程池,原因是假设在IO线程池中做的业务非常复杂,大量耗时,这样就会阻塞了netty线程的IO处理速度,影响吞吐量,这也就是reactor模型的设计理念,不让业务干扰连接线程和IO读写线程。NettyServerInitiator就是实际处理的业务handle了。
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run(){
logger.info ( "Shutdown by Runtime" );
if(zookeeperServer != null){
zookeeperServer.destroy ();
}
logger.info ( "wait for service over 3000ms" );
try {
Thread.sleep ( 3000 );
} catch (Exception e) {
}
if(executorService!=null){
executorService.shutdown ();
}
if(bossGroup != null) bossGroup.shutdownGracefully ();
if(workerGroup != null) workerGroup.shutdownGracefully ();
}
});
手动关闭钩子,服务关闭的时候要主动关闭节点信息。下面来看一下hander拦截器
package netty.initializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import netty.hanlder.KoalasDecoder;
import netty.hanlder.KoalasEncoder;
import netty.hanlder.KoalasHandler;
import org.apache.thrift.TProcessor;
import server.config.AbstractKoalsServerPublisher; import java.util.concurrent.ExecutorService;
/**
* Copyright (C) 2018
* All rights reserved
* User: yulong.zhang
* Date:2018年11月23日11:13:33
*/
public class NettyServerInitiator extends ChannelInitializer<SocketChannel> { private ExecutorService executorService; private AbstractKoalsServerPublisher serverPublisher; public NettyServerInitiator(AbstractKoalsServerPublisher serverPublisher,ExecutorService executorService){
this.serverPublisher = serverPublisher;
this.executorService = executorService;
} @Override
protected void initChannel(SocketChannel ch) {
ch.pipeline ().addLast ( "decoder",new KoalasDecoder () );
ch.pipeline ().addLast ( "encoder",new KoalasEncoder ());
ch.pipeline ().addLast ( "handler",new KoalasHandler (serverPublisher,executorService) );
} }
decode负责拆包。encoder负责装包,handler是真正业务处理的逻辑,所有的业务处理都在这里的线程池中运行,结果通过ChannelHandlerContext 异步的返回给client端,通过这种方式真正的实现了reactor
下面我们看看拆包处理
package netty.hanlder; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import server.KoalasServerPublisher; import java.util.List;
/**
* Copyright (C) 2018
* All rights reserved
* User: yulong.zhang
* Date:2018年11月23日11:13:33
*/
public class KoalasDecoder extends ByteToMessageDecoder { private final static Logger logger = LoggerFactory.getLogger ( KoalasDecoder.class ); @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { try {
if (in.readableBytes () < 4) {
return;
} in.markReaderIndex ();
byte[] b = new byte[4];
in.readBytes ( b ); int length = decodeFrameSize ( b ); if (in.readableBytes () < length) {
//reset the readerIndex
in.resetReaderIndex ();
return;
} in.resetReaderIndex ();
ByteBuf fream = in.readRetainedSlice ( 4 + length );
in.resetReaderIndex (); in.skipBytes ( 4 + length );
out.add ( fream );
} catch (Exception e) {
logger.error ( "decode error",e );
} } public static final int decodeFrameSize(byte[] buf) {
return (buf[0] & 255) << 24 | (buf[1] & 255) << 16 | (buf[2] & 255) << 8 | buf[3] & 255;
}
}
通过读取四个字节的长度来决定消息体长度,然后根据消息体长度来读取所有的字节流数据。decodeFrameSize方法将四个字节流转成int类型。KoalasHandler处理器逻辑比较复杂,我们只看核心的内容,首先通过thrift解析字节流来获取transport
ByteArrayInputStream inputStream = new ByteArrayInputStream ( b );
ByteArrayOutputStream outputStream = new ByteArrayOutputStream ( ); TIOStreamTransport tioStreamTransportInput = new TIOStreamTransport ( inputStream);
TIOStreamTransport tioStreamTransportOutput = new TIOStreamTransport ( outputStream); TKoalasFramedTransport inTransport = new TKoalasFramedTransport ( tioStreamTransportInput,2048000 );
inTransport.setReadMaxLength_ ( maxLength );
TKoalasFramedTransport outTransport = new TKoalasFramedTransport ( tioStreamTransportOutput,2048000,ifUserProtocol );
最终扔到线程池里去执行,将当前IO线程释放给下一个任务。
try {
executorService.execute ( new NettyRunable ( ctx,in,out,outputStream,localTprocessor,b,privateKey,publicKey,className,methodName,koalasTrace,cat));
} catch (RejectedExecutionException e){
logger.error ( e.getMessage ()+ErrorType.THREAD+",className:" +className,e );
handlerException(b,ctx,e,ErrorType.THREAD,privateKey,publicKey,thriftNative);
}
RejectedExecutionException来负责当线程池不够用的时候返回给client端异常,因为server端的业务处理能力有限,所以这里适当的做了一下服务端保护防止雪崩的问题。当发现server端有大量的
RejectedExecutionException抛出,说明单机已经无法满足业务请求了,需要横向拓展机器来进行负载均衡。用户实际的业务执行是在Runable里,我们看看他到底做了什么
try {
tprocessor.process ( in,out );
ctx.writeAndFlush (outputStream);
if(transaction!=null && cat)
transaction.setStatus ( Transaction.SUCCESS );
} catch (Exception e) {
if(transaction!=null && cat)
transaction.setStatus ( e );
logger.error ( e.getMessage () + ErrorType.APPLICATION+",className:"+className,e );
handlerException(this.b,ctx,e,ErrorType.APPLICATION,privateKey,publicKey,thriftNative);
}
通过thrift的process来执行业务逻辑,将结果通过ctx.writeAndFlush (outputStream),返回给client端。在catch里处理当出现异常之后返回给client端异常结果。这样netty server的实现就全部结束了,thrift服务端解析相关内容我们下一篇来说,里面当中有很多细节需要读者是跟着源码阅读,如果有问题欢迎加群825199617来交流,更多spring,spring mvc,aop,jdk等源码交流等你来!
JAVA RPC (九) netty服务端解析的更多相关文章
- JAVA RPC (十) nio服务端解析
源码地址:https://gitee.com/a1234567891/koalas-rpc 企业生产级百亿日PV高可用可拓展的RPC框架.理论上并发数量接近服务器带宽,客户端采用thrift协议,服务 ...
- netty服务端启动--ServerBootstrap源码解析
netty服务端启动--ServerBootstrap源码解析 前面的第一篇文章中,我以spark中的netty客户端的创建为切入点,分析了netty的客户端引导类Bootstrap的参数设置以及启动 ...
- Netty之旅三:Netty服务端启动源码分析,一梭子带走!
Netty服务端启动流程源码分析 前记 哈喽,自从上篇<Netty之旅二:口口相传的高性能Netty到底是什么?>后,迟迟两周才开启今天的Netty源码系列.源码分析的第一篇文章,下一篇我 ...
- Netty 服务端启动过程
在 Netty 中创建 1 个 NioServerSocketChannel 在指定的端口监听客户端连接,这个过程主要有以下 个步骤: 创建 NioServerSocketChannel 初始化并注 ...
- Netty 服务端创建
参考:http://blog.csdn.net/suifeng3051/article/details/28861883?utm_source=tuicool&utm_medium=refer ...
- Netty服务端NioEventLoop启动及新连接接入处理
一 Netty服务端NioEventLoop的启动 Netty服务端创建.初始化完成后,再向Selector上注册时,会将服务端Channel与NioEventLoop绑定,绑定之后,一方面会将服务端 ...
- Netty服务端的启动源码分析
ServerBootstrap的构造: public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, Serve ...
- ASP.NET Core中间件(Middleware)实现WCF SOAP服务端解析
ASP.NET Core中间件(Middleware)进阶学习实现SOAP 解析. 本篇将介绍实现ASP.NET Core SOAP服务端解析,而不是ASP.NET Core整个WCF host. 因 ...
- java开源即时通讯软件服务端openfire源码构建
java开源即时通讯软件服务端openfire源码构建 本文使用最新的openfire主干代码为例,讲解了如何搭建一个openfire开源开发环境,正在实现自己写java聊天软件: 编译环境搭建 调试 ...
随机推荐
- (一)SpringMvc简介以及第一个springmvc工程
一.SpringMVC是什么? springmvc是Spring的一个模块,提供web层解决方案(就与MVC设计架构) 如上图, DispatcherServlet:前端控制器,由SpringMVC提 ...
- python爬视频实例
例:抓取PhotoShop视频教程 网址http://www.mxiaobei.com/?id=424 import requests import re from bs4 import Beauti ...
- 路由器开源系统openwrt配置页面定制
1. 新建虚拟机,百度文库有一篇<使用VMware安装OpenWrt>,地址:http://wenku.baidu.com/link?url=NkvaQpTf2dR8FpYn7JD9A7- ...
- H5各种头部meta标签功能大全
<!DOCTYPE html> H5标准声明,使用 HTML5 doctype,不区分大小写 <head lang=”en”> 标准的 lang 属性写法 <meta ...
- css border-raidus 百分比和数值设置效果不同
1.水平方向和竖直方向半径相等:设置数值和百分比的效果是一样的: 2.水平方向和竖直方向半径不相同:则效果不一致,具体参见:http://www.zhangxinxu.com/wordpress/20 ...
- python采集websocket实时数据
之前大部分的数据采集基本都是http的,也一直对如何采集websocket的实时数据有疑问,不知道入从哪里入手,一筹莫展,今天在知乎上看到了一篇采集websocket的文章,讲的很透彻 终于把这个疑问 ...
- 【2017-06-29】在登录页面自动返回上次请求页面、Js获取table中的行数与列数
一.在登录页面自动返回上次请求页面 Request.UrlReferrer比如 if (Request.UrlReferrer != null) { //如果能获取来路地址 Response.Redi ...
- notify()和 notifyAll()有什么区别?(未完成)
notify()和 notifyAll()有什么区别?(未完成)
- linux上构建ftp服务器
linux上构建ftp服务器 服务器搭建 https://help.aliyun.com/knowledge_detail/60152.html,可以参考这篇博文. 配置文件详解 进入/etc/vsf ...
- 部署WCF Lib到IIS
打开VS2013,新建项目,选择WCF|WCF服务库,取名WCFWithIIS 2. 我们用演示一个计算加法的服务.简单起见,直接在IService接口中添加加法接口 3. 实现这个加法 4. 生成项 ...