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

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

发布时间:2019-04-27 15:22:34 所属栏目:教程 来源:蜂巢团队
导读:宜人贷蜂巢团队,由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方法?

(编辑:威海站长网)

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

热点阅读