After completing the current chapter, the code effect is demonstrated as follows: You can get the project source code and information in the video introduction link to the effect demo
This request will be redirected to our backend localhost:8080/http-demo/ping address. This is the address of our backend service.
Netty architecture
The Netty website philosophy helps us build scalable, high-performance, and maintainable web applications. Here are some of the core concepts of Netty:
Channel: A channel is an abstraction of data communication that can represent underlying network connections such as sockets.Netty provides several types of channels for different transport protocols (e.g., NIO, OIO, local transport).
EventLoop: EventLoop is a loop for handling events and is a core component in Netty. Each Channel has an EventLoop associated with it, which is responsible for handling all the events of the Channel, such as receiving data, processing data, sending data, etc.
ChannelHandler: ChannelHandler is a component that handles channel events and can be used to implement protocol encoding and decoding, business logic processing, etc. A ChannelHandler chain is a sequence of operations performed on a channel.
Bootstrap: Bootstrap is a tool used to start and configure a web application, which is usually used to create and connect to a Channel. it helps to set the configuration of EventLoopGroup, Channel type, ChannelHandler, etc.
ChannelPipeline: ChannelPipeline is a data structure used to maintain and process the ChannelHandler chain. It is used to specify the order in which events are processed and the direction in which they flow in order to perform specific operations on the Channel.
ByteBuffer: The ByteBuffer is the basic data structure for processing data in Netty. It provides read and write operations on the data, and supports the zero-copy feature.
Codecs: Codecs are components used to convert raw data into protocol-specific messages and messages into bytes of data; Netty provides a number of built-in codecs and also supports custom codecs.
Promise: Promise is an abstraction for handling the results of asynchronous operations. It can be used to monitor and get the result or state of an asynchronous operation.
Future: A Future is also an abstraction for handling asynchronous operations and represents an operation that has not yet completed; asynchronous operations in Netty typically return a Future that can be retrieved when the operation completes.
ByteBuf: ByteBuf is a byte data container in Netty that provides efficient read and write operations and supports reference counting for improved performance and reduced memory overhead. We will develop based on Netty Reactor working architecture, below is the architecture diagram (from web):
Implementing NettyHttpServer
Above a brief understanding of the Netty architecture, in this point I will implement a client based on Netty. There are roughly the following steps that we need to implement to do: 1: encapsulate properties: 2: implementation of the construction and init methods 3: epoll optimization 4: implementation of the start method 5: implementation of the shutdown method
In this part we need to Write the finished code is as follows:
publicvoidshutdown() { if (eventLoopGroupBoss!=null){ eventLoopGroupBoss.shutdownGracefully(); } if (eventLoopGroupWorker!=null){ eventLoopGroupWorker.shutdownGracefully(); } } }
Implementing NettyHttpServerHandler
Our NettyHttpServerHandler inherits from ChannelInboundHandlerAdapter. Inheriting from ChannelInboundHandlerAdapter is to implement custom inbound data handling logic. ChannelInboundHandlerAdapter is an abstract class provided by Netty that implements the ChannelInboundHandler interface, which provides a set of default methods that allow developers to easily handle inbound events. The exact functionality can be searched on the web. In this module, we probably need to achieve the function and implementation steps are as follows: 1: inherit ChannelInboundHandlerAdapter 2: implement channelRead 3: delegate the logic to NettyProcessor
This interface as the core interface, we need to implement the following functions: 1: define the interface 2: the minimum usable version of the implementation 3: routing function implementation 4: get asynchronous configuration, the implementation of complete method 5: exception handling 6: write back the response information and release resources
Above we have implemented the Server server, now start to implement the client. The approximate steps are as follows: 1: implementation of LifeCyclke interface 2: encapsulation of properties 3: implementation of the init method 4: implementation of the start method 5: implementation of the shutdown method can be found in fact, the implementation of the client and the implementation of the server are more or less the same.
In the above we have completed the implementation of Netty-related code, we are going to implement the core container. The approximate steps are as follows: 1: implementation of the LifeCyclke interface 2: encapsulation of properties 3: implementation of the init method 4: implementation of the start method 5: implementation of the shutdown method The implementation of the core container can be used to start our Netty clients and servers, as long as the completion of the step, we have a simple request forwarding and receiving has been completed.