加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

宜人贷蜂巢API网关技能解密之Netty行使实践

发布时间:2019-04-25 16:59:06 所属栏目:教程 来源:蜂巢团队
导读:宜人贷蜂巢团队,由Michael创建于2013年,通过行使互联网科技本领助力金融生态调和康健成长。自创立起一向致力于多维度数据闭环平台建树。今朝团队局限高出百人,涵盖征信、电商、金融、交际、五险一金和保险等用户授信数据的抓取理会营业,辅以先辈的数据

API网干体系行使netty自带的线程池,共有三组线程池,别离为bossGroup、workerGroup和executorGroup(行使在channelInitializer中,本文暂不作先容)。个中,bossGroup用于吸取客户端的TCP毗连,workerGroup用于处理赏罚I/O、执行体系task和按时使命,executorGroup用于处理赏罚网关营业加解密、限流、路由,及将哀求转发给后端的抓取处事等营业操纵。

六、Channel与线程池的绑定

ServerBootstrap初始化后,通过挪用bind(port)要领启动Server,bind的挪用链如下:

  1. AbstractBootstrap.bind ->AbstractBootstrap.doBind -> AbstractBootstrap.initAndRegister 

个中,ChannelFuture regFuture = config().group().register(channel);中的group()要领返回bossGroup,而channel在serverBootstrap的初始化进程指定channel为NioServerSocketChannel.class,至此将NioServerSocketChannel与bossGroup绑定到一路,bossGroup认真客户端毗连的成立。那么NioSocketChannel是怎样与workerGroup绑定到一路的?

挪用链AbstractBootstrap.initAndRegister -> AbstractBootstrap. init-> ServerBootstrap.init ->ServerBootstrapAcceptor.ServerBootstrapAcceptor ->ServerBootstrapAcceptor.channelRead:

  1. public void channelRead(ChannelHandlerContext ctx, Object msg) {  
  2.     final Channel child = (Channel) msg;  
  3.     child.pipeline().addLast(childHandler);  
  4.     for (Entry<ChannelOption<?>, Object> e: childOptions) {  
  5.         try {  
  6.             if (!child.config().setOption((ChannelOption<Object>) e.getKey(), e.getValue())) {  
  7.                 logger.warn("Unknown channel option: " + e);  
  8.             }  
  9.         } catch (Throwable t) {  
  10.             logger.warn("Failed to set a channel option: " + child, t); 
  11.         }  
  12.     }  
  13.     for (Entry<AttributeKey<?>, Object> e: childAttrs) {  
  14.         child.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());  
  15.     } 
  16.  
  17.     try {  
  18.         childGroup.register(child).addListener(new ChannelFutureListener() {  
  19.             @Override  
  20.             public void operationComplete(ChannelFuture future) throws Exception {  
  21.                 if (!future.isSuccess()) { 
  22.                      forceClose(child, future.cause());  
  23.                 }  
  24.             }  
  25.         });  
  26.     } catch (Throwable t) {  
  27.         forceClose(child, t);  
  28.     }  

个中,childGroup.register(child)就是将NioSocketChannel与workderGroup绑定到一路,那又是什么触发了ServerBootstrapAcceptor的channelRead要领?

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读