In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use netty development, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
In the Java world, netty is undoubtedly a specialty in the development of web applications. You do not need to pay much attention to the details of the complex nio model and the underlying network, using its rich interfaces, you can easily achieve complex communication functions.
Compared with golang's network module, netty is still too bloated. However, this is the case with the java class framework, which belongs to the kind of coding language that cannot survive without IDE.
The netty version of * * divides modules into very fine sections. If you don't know what each module contains, you can use netty-all directly.
In terms of use, netty is very simple, and you can develop it by mastering ByteBuf, Channel, Pipeline, Event models, and so on. You will find that there is nothing to talk about about the netty interview. But Netty is very different from other development models, the most important thing is its asynchronization. The consequence of asynchronization is that the programming model is different, at the same time, there are difficulties in debugging, and the requirement for coding is relatively high, because the cost of bug is not the same as the bug cost of business code.
But in terms of the project, although the sparrow is complete, from the business layer to the service gateway, as well as a variety of technical support, including monitoring and configuration, are all factors to be considered. Netty itself accounts for a very small proportion.
This article will explain what common content you focus on when developing with netty, and then attach a stand-alone linux configuration that supports 100w connections. This article does not focus on the basics of netty.
Protocol development
The most important thing in network development is its communication format and protocol. Our common protobuf, json, avro, mqtt, and so on, all belong to this category. The protocol has three elements: syntax, semantics and timing.
I have seen many middleware applications that use redis protocol, but mysql; has also seen more custom storage systems implemented by mysql protocol, such as proxy middleware, tidb and so on.
Our commonly used redis uses a text protocol; mysql and others implement a binary protocol. The same is true in netty, where you can implement a set of codec (inheriting the Decoder or Encoder series). Netty implements dns, haproxy, http, http2, memcache, mqtt, redis, smtp, socks, stomp, xml and other protocols by default, which can be said to be very complete, and it is very cool to use them directly.
A possible product structure would look like this, providing a consistent look and feel, but different core storage:
The text protocol is relatively intuitive and easy to debug, but the security is poor, while the binary protocol needs to rely on log, wireshark and other ways for analysis, which increases the difficulty of development. The legendary sticky package is dealt with here. The reason for the sticky packet is mainly due to the intervention of the buffer, so it is necessary to agree on the transmission summary and other information of both parties. Netty has solved this problem to a certain extent.
Every student who wants to develop network applications has a dream seed of redesigning the protocol. However, the design of the protocol can be said to be very difficult, to cultivate the corresponding business, but also to consider its expansibility. If it is not particularly necessary, it is recommended to use the existing agreement.
Connection management function
Do Netty development, connection management function is very important. Communication quality, system status, and some cool techs functions all rely on connection management functions.
Whether as a server or a client, netty gets an object called Channel after creating a connection. All we have to do is manage it. I'm used to calling it ConnectionManager.
The management class will count the running data by caching some in-memory objects. For example, connection-oriented functions: the number of packets sent and received; packet sending and receiving rate; error count; connection reconnection times; call delay; connection status and so on. This frequently uses the related classes of the concurrent package in java, often where bug is centralized.
But we need more, and the management class will give each connection more functionality. For example, if you want to warm up some functions after the connection is created, these states can participate in routing decisions. In general, it is a very important function to attach users or other meta-information to connections, to filter some connections according to conditions, and to carry out batch operations, such as grayscale, overload protection and so on.
The management backend can see the information of each connection. After filtering one or more connections, you can enable traffic recording, information monitoring and breakpoint debugging of these connections, and you can experience the feeling of controlling everything.
The management function can also see the entire running status of the system, adjust the load balancing strategy in time, and provide data basis for capacity expansion and reduction.
Heartbeat detection
The heartbeat of the application protocol layer is necessary, which is a completely different concept from tcp keepalive.
The heartbeat of the application layer protocol layer detects the viability of both sides of the connection and the quality of the connection, while keepalive detects the viability of the connection itself. And the time-out of the latter is too long by default, which can not adapt to the modern network environment at all.
Heartbeat depends on rotation training, whether it is the server or the client such as GCM and so on. The survival mechanism will dynamically switch in different application scenarios, such as program arousal and in the background, the strategy of rotation training is different.
Netty is built-in for convenient heartbeat control by adding IdleStateHandler to generate IDLE events. What you have to deal with is the logic of heartbeat timeout, such as delayed reconnection. But its rotation training time is fixed, can not be dynamically modified, advanced functions need to be customized.
In some clients such as Android, frequent heartbeat arousal will waste a lot of network and power, and its heartbeat strategy will be more complicated.
Boundary
Elegant exit mechanism
Graceful downtime for Java is usually achieved by registering for JDK ShutdownHook.
Runtime.getRuntime () .addShutdownHook ()
Generally, the java process is shut down through kill-15 in order to do some cleaning before the process dies.
Note: kill-9 will kill the process immediately, do not give the last words the opportunity, more dangerous.
Although netty has done a lot of graceful exit work and made some state settings for nio through EventLoopGroup's shutdownGracefully method, in many cases, this is not enough. It is only responsible for the elegant shutdown of the stand-alone environment.
Traffic may also continue to enter through the outer route, resulting in invalid requests. My usual approach is to first remove the local instance on the outer route, truncate the traffic, and then gracefully shut down the netty itself. This design is very simple and works well even without a retry mechanism, as long as the relevant interfaces need to be exposed in advance at the routing layer.
Exception handling function
Because of its asynchronous development mode and its event mechanism, netty is very important in exception handling. In order to ensure the high reliability of the connection, many exceptions need to be quietly ignored or not perceived in the user state.
Netty exceptions are propagated through pipeline, so handling at any layer is feasible, but programming habits are routinely thrown to the outermost centralized processing.
In order to distinguish exception information within the limit, a large number of exception classes are usually defined, and different errors will throw different exceptions. After an exception occurs, you can choose to disconnect and reconnect according to different types (such as some binary protocol codec disorder), or schedule to other nodes.
Functional limitation
Instruction mode
Network applications should do what network applications do, and any communication is expensive. In "the rest of the Life of the Desert Island (5) Network" of Linux, we talk about millions of connected servers. Broadcasting a 1kb message requires a bandwidth of 1000m, so not everything can be put in network applications.
A reasonable idea for a large-scale network application is to send relevant instructions by value. After receiving the instruction, the client performs large files to fetch through other means, such as http. This is the design idea of many IM.
The instruction mode also ensures the expansibility and stability of the communication system. The additional instruction can be configured and take effect immediately, and the server does not need to be restarted by coding.
Stability guarantee
The traffic of network applications is generally very large, which is not suitable for the opening of full logs. The application should only pay attention to the log of the main event, pay attention to the processing flow in the case of exception, and the log should be printed in a certain degree.
Network applications are also not suitable for calling other slow api, or any interface that blocks Icano. Some real-time events should not spit out data through the calling interface, but can take other asynchronous channels such as high-speed mq.
Caching is probably the most frequently used component in web applications. The cache in jvm can store some stand-alone statistical data, while redis and so on can store some global statistical and intermediate data.
Redis, kv and high throughput mq are widely used in network applications to respond to user requests quickly. In short, try to keep the communication layer as fresh as possible, you will save a lot of worries.
Linux configuration with stand-alone support for 1 million connections
It is feasible for a stand-alone machine to support 1 million connections, but the bandwidth problem will become a significant bottleneck. Enabling compressed binary protocols saves some bandwidth, but makes it more difficult to develop.
Like the ES configuration mentioned in "LWP process resource exhaustion, Resource temporarily unavailable", optimization has a similar idea. This configuration can save you a few days, please accept it!
Operating system optimization
Change the number of file handles in the process *
Ulimit-n 1048576
Modify the number of * files that can be allocated by a single process
Echo 2097152 > / proc/sys/fs/nr_open
Modify / etc/security/limits.conf file
* soft nofile 1048576* hard nofile 1048576* soft nproc unlimitedroot soft nproc unlimited
Remember to clean up the configuration under / etc/security/limits.d/*
Network optimization
Open / etc/sysctl.conf to add configuration
Then execute and use sysctl to take effect.
# number of fs.nr_open=2097152 files that can be allocated by a single process # system * number of file handles fs.file-max = 1048576 # backlog setting net.core.somaxconn=32768 net.ipv4.tcp_max_syn_backlog=16384 net.core.netdev_max_backlog=16384 # available well-known port range configuration net.ipv4.ip_local_port_range='1000 65535'# TCP Socket read / write Buffer setting net.core.rmem_default=262144 net.core.wmem_default=262144 net.core. Rmem_max=16777216 net.core.wmem_max=16777216 net.core.optmem_max=16777216 net.ipv4.tcp_rmem='1024 4096 16777216 'net.ipv4.tcp_wmem='1024 4096 16777216' # TCP connection tracking settings net.nf_conntrack_max=1000000 net.netfilter.nf_conntrack_max=1000000 net.netfilter.nf_conntrack_tcp_timeout_time_wait=30 # TIME-WAIT Socket * * number, Recycling and reuse Settings net.ipv4.tcp_max_tw_buckets=1048576 # FIN-WAIT-2 Socket timeout Settings net.ipv4.tcp_fin_timeout = 15 are all the contents of the article "how to develop with netty" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.