Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement TimeServer in Netty

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to achieve TimeServer in Netty, which is very detailed and has a certain reference value. Interested friends must read it!

No1, what is Netty

Netty is one of the most popular NIO frameworks in the industry, and its performance, robustness and scope of application are second to none in similar frameworks. For example, RPC framework dubbo, Hadoop's RPC framework Avro uses Netty as the underlying communication framework.

The disadvantages of iUnip O in No2 and JDK

I believe all of you who have studied Socket programming have seen the following code:

The processing logic diagram is as follows

This is the simplest Socket program. As soon as the server receives the request, it creates a thread to process the request. There seems to be nothing wrong with it, but if the concurrency is slightly larger, creating and destroying threads on the server side will particularly consume performance. Some friends will certainly say, since the bottleneck of the system lies in the creation and destruction of threads, will it be possible to use thread pools soon? Keep the number of threads created by the system within a certain range, and the system threads will not be exhausted because of the concurrency. This is true, but because the underlying communication still uses the synchronous blocking model, it can not fundamentally solve the problem.

No3, why choose Netty

The reasons for not selecting JAVA native NIO are as follows:

NIO library and API are complex

Complex code

It is difficult to get started, and a lot of JAVA knowledge is needed.

Bug of JDK NIO

. . .

The reasons for choosing Netty are as follows:

API is simple and the threshold for development is low.

Powerful, preset a variety of coding and decoding functions

High performance, mature and stable

High degree of community activity

Fixed all Bug found in NIO

. . .

Little Demo--TimeServer for getting started with No4 and Netty

OK, let's write the simplest network Mini Program based on Netty. The function of the program is to link client to server,server and return the current time to client.

Step1, engineering construction

Create a maven project to introduce related dependencies

Step2, writing server-- TimeServer

Create two NioEventLoopGroup instances. NioEventLiipGroup is a thread group that contains a set of NIO threads. Two are created because one is for the server to receive the client's connection and the other is for reading and writing to the Sockerchannel.

Create ServerBootstrap, which is the startup class of the Netty startup service, passes EventLoop Group into ServerBootstrap, and finally binds the handler class ChildChannelHandler for the Imax O event.

The bind port, which calls the synchronous blocking method sync to wait for the binding operation to complete and returns a ChannelFuture object. Its function is similar to java.util.concurrent.Future and is mainly used for asynchronous callback.

The main method ends after waiting for the server-side link to shut down

The implementation of ChildChannelHandler.java event handler class iPUBO

Create a response and write it to the client.

This is the server code. It's very simple, isn't it?

Step3, write client-TimeClient

Create an EventLoopGroup instance

Create a Bootstrap instance. Unlike TimeServer, Channel needs to be set to NioSockerChannel.

Call connect to initiate an asynchronous connection, and then call sync to wait for the connection to succeed

Close the connection and release resources

The realization of TimeClientHandler.java

When the TCP connection between client and server is successful, the NIO thread of Netty calls the channelActive method to send a request to the server

Encapsulate byte [] data into ByteBuf and send a request

Convert the response msg into a ByteBuf object and write it to byte []

Print response data

These are all the contents of the article "how to achieve TimeServer in Netty". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report