Goodbye, Netty! Hello, smart-socket!

goodbye netty

Netty, currently the most popular NIO framework, is the best choice for communication development. After years of development, it has been widely used in the Internet field, big data distributed computing field, game industry, communication industry, etc. Some well-known open source components in the industry are also built based on Netty's NIO framework. At present, there are almost no comparable communication frameworks of the same type on the market, but as a communication technology enthusiast, I have never been able to feel close to it.

I do not deny that Netty is indeed an excellent framework. I once wrote the NIO framework myself and knew how difficult it was to implement, so I finally chose to give up. The emergence of Netty provides convenience for the majority of programmers and greatly reduces the difficulty of developing efficient and stable services. As for my dislike of it, the reasons are as follows (first of all, I have only used netty briefly and did not study it in depth):

  • Complicated; the following code is excerpted from Netty's official website . At first glance at the code, most people should be puzzled by the run method. EventLoopGroupWhat is it for? bossGroupAnd workerGroupwhat is the relationship? childHandleEntry is so complicated! ch.pipeline().addLast(new DiscardServerHandler())What function is implemented? etc.... In short, this method will cause you a lot of doubts.
public class DiscardServer {
    
    private int port;
    
    public DiscardServer(int port) {
        this.port = port;
    }
    
    public void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap(); // (2)
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class) // (3)
             .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                 @Override
                 public void initChannel(SocketChannel ch) throws Exception {
                     ch.pipeline().addLast(new DiscardServerHandler());
                 }
             })
             .option(ChannelOption.SO_BACKLOG, 128)          // (5)
             .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
    
            // Bind and start to accept incoming connections.
            ChannelFuture f = b.bind(port).sync(); // (7)
    
            // Wait until the server socket is closed.
            // In this example, this does not happen, but you can do that to gracefully
            // shut down your server.
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
    
    public static void main(String[] args) throws Exception {
        int port;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        } else {
            port = 8080;
        }
        new DiscardServer(port).run();
    }
}
  • Bloated; after downloading the Netty source code, you will see that there are more than two dozen modules in its project. After so many years of development, it has become a behemoth. Coupled with its advanced design philosophy, it is difficult for ordinary people to understand its essence by reading the source code.
  • The interview must be asked; if you go to an interview after a few years of work experience, you will basically encounter the situation of being asked about Netty-related questions. It seems that you are not a qualified programmer if you have never used Netty. If you do not understand the Netty threading model, you will put a question mark on your technical ability. Personal experience, not cool! I also disdain to memorize those related theories on the Internet, which is not pragmatic.
  • Netty solves the problem of people's work, but it also limits the technical growth of many Java developers. Some students spend tens of RMB to buy books related to Netty, but no matter how thorough the reading is, they only have some talk with their peers after a meal, and they cannot make you have the ability to write a Netty.

Netty is a river that stretches between you and the realm of communication, and you can see the scenery across the river from a distance, but you can't feel its charm up close. Combined with my personal stereotypes about Netty, it's time to say goodbye to Netty, or see you later. Netty can be used in work, and for learning purposes, consider the protagonist of this article: smart-socket.

hello smart-socket

This is also a communication framework, and it is a domestic open source project that is not eligible to be compared with Netty, at least at this stage. smart-socket is a product of learning, which is gradually optimized and improved by the author in the process of learning AIO. The purpose of this article is also to introduce smart-socket to everyone, so that everyone can understand that there is a wave of people who are not comfortable with the existing high-quality solutions, and are committed to injecting fresh blood into this open source community by recreating the wheel.

Java provided the AIO communication API in version 1.7. Netty also released the AIO version, but finally chose to give up, it is said that the performance improvement is not significant. But through the development of smart-socket, I have a little humble opinion. Java AIO has lowered the threshold for Socket programming to a very low level. As long as this technology point is popularized, there will be many AIO frameworks springing up like mushrooms after a rain, and friends with a little technical ability can develop a set of their own communication frameworks. Netty's implementation based on AIO is equivalent to destroying its own life. The cost of implementing asynchronous non-blocking communication on NIO and achieving this achievement is self-evident, but AIO makes all this very cheap, resulting in Netty is caught in a dilemma, and it is not good to give up the past efforts in the NIO model. Purely personal YY, don't spray if you don't like it, haha.

smart-socket has undergone five months of development and has released four versions on the open-source Chinese platform, always adhering to the principles of simplicity, ease of use, and high performance. The project can be downloaded from the code cloud: smart-socket , you are welcome to download. Next, Po Wang officially started selling melons and boasting about herself.

  • Minimalism
    What is "simplicity", and the small amount of code is "simplicity". The initial implementation of smart-socket only used 11 files and 514 lines of code . Such a framework should be very attractive, which means that you can completely master AIO technology as long as you understand these 514 lines of code. However, in subsequent versions, some auxiliary codec tool classes were added, resulting in an increase in the amount of code, but it was still less than 800 lines.
  • Easy to use
    What is "easy", easy to learn, making it convenient is "easy". smart-socket only needs to be familiar with three interfaces (Filter, MessageProcessor, Protocol) and three classes (AioSession, AioQuickServer, AioQuickClient) to play with Socket.
  • High performance
    See " Processing 500W messages per second, people and machines tremble ", many friends do not agree with this test method. Assuming that the same test method is used, Netty processes 400W per second, whether the 500W data of the smart-socket is more convincing.
    Here is another test data from a netizen who focuses on the performance of smart-socket connection establishment:
    • Test environment: CPU E3-1231 v3 @ 3.40GHz, 8 cores, 16G memory
    • Test results: 8000 long connection requests are processed per second, and the total number of connections to the server reaches 18W (the number of connections established per second after adjusting the configuration items is about 1W)

The positioning of smart-socket is very pure. It is only responsible for coordinating data read and write resources to maximize system performance. Users only need to pay attention to protocol encoding and decoding and message business processing. smart-socket does not cover all aspects of communication functions. I hope that every friend who uses smart-socket can deepen their understanding and cognition of socket through it. If you are a friend who has a strong interest in socket technology, you are very welcome to experience the following smart-socket.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324403029&siteId=291194637