Netty learning (2) TCP Gateway Comments

Attention, private letters presented netty source code analysis study video!

 What is netty

Introduction

Gateway technology things device architecture (Session Management, management heartbeat, uplink data, downlink data)

Explanation

NioEventLoop is Netty's Reactor thread, its role:

  1. Group BOSS : as a server Acceptor threads for client accept the link, and forwarded to WorkerGroup in the thread.
  2. Group Worker : As the IO thread is responsible for reading and writing IO reads or writes the message packet to the SocketChannel from SocketChannel in.
  3. Queue the Task / the Task Queue Delay : thread as the timing task, the task execution timing, for example, the detection link is idle and sends a heartbeat message or the like.

Summary Description

  • TcpServer: TCP provides connection services
  • TcpSessionManager: You can add an event listener for listening TCP session creation, destruction, etc.
  • LogSessionListener: a log listener, it tcpSessionManager association, the listener must first SessionListener
  • TcpSender: TCP sender, user sends a message to the notification client, achieve downlink logic
  • ServerConfig: TCP Configuration Management
  • TcpConnector: TCP connections container, and management services for clients
  • NotifyProxy: notification is sent to the proxy class

These are the default configuration, you can not modify, but you may need to change the TCP port

Network gateway structure .TCP

Construction of the long container is connected on Netty TCP gateway, serving as a gateway access layer API call request.

The client domain name + port access to TCP gateway, different domain operators corresponding to different VIP, VIP posted on LVS, LVS forwards the request HAProxy to the back end, and then forwards the HAProxy request to Netty's back-end IP + Port.

LVS转发给后端的HAProxy,请求经过LVS,但是响应是HAProxy直接反馈给客户端的,这也就是LVS的DR模式。

 输入图片说明

TCP网关执行时序图

输入图片说明

其中步骤一至步骤九是 Netty 服务端的创建时序,步骤十至步骤十三是 TCP 网关容器创建的时序。

  • 步骤一:创建 ServerBootstrap 实例,ServerBootstrap 是 Netty 服务端的启动辅助类。
  • 步骤二:设置并绑定 Reactor 线程池,EventLoopGroup 是 Netty 的 Reactor 线程池,EventLoop 负责所有注册到本线程的 Channel。
  • 步骤三:设置并绑定服务器 Channel,Netty Server 需要创建 NioServerSocketChannel 对象。
  • 步骤四:TCP 链接建立时创建 ChannelPipeline,ChannelPipeline 本质上是一个负责和执行 ChannelHandler 的职责链。
  • 步骤五:添加并设置 ChannelHandler,ChannelHandler 串行的加入 ChannelPipeline 中。
  • 步骤六:绑定监听端口并启动服务端,将 NioServerSocketChannel 注册到 Selector 上。
  • 步骤七:Selector 轮训,由 EventLoop 负责调度和执行 Selector 轮询操作。
  • 步骤八:执行网络请求事件通知,轮询准备就绪的 Channel,由 EventLoop 执行 ChannelPipeline。
  • 步骤九:执行 Netty 系统和业务 ChannelHandler,依次调度并执行 ChannelPipeline 的 ChannelHandler。
  • 步骤十:通过 Proxy 代理调用后端服务,ChannelRead 事件后,通过发射调度后端 Service。
  • 步骤十一:创建 Session,Session 与 Connection 是相互依赖关系。
  • 步骤十二:创建 Connection,Connection 保存 ChannelHandlerContext。
  • 步骤十三:添加 SessionListener,SessionListener 监听 SessionCreate 和 SessionDestory 等事件。

 

转载:https://gitee.com/ibyte/icloud-tcp-gateway#%E8%AF%B4%E6%98%8E

发布了109 篇原创文章 · 获赞 191 · 访问量 42万+

Guess you like

Origin blog.csdn.net/wuzhiwei549/article/details/104248196