In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the example analysis of RPC design, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Preface
RPC full-range remote method invocation has been widely used in large and small companies, and there are many kinds, such as Dubbo,Spring cloud, GRPC,Thrift, and possibly many companies have developed their own, etc. Each company may choose its own appropriate RPC framework according to its own business needs, but there are only a few general dimensions: performance, extensibility, cross-platform, functionality, monitoring, usability, etc. So if we want to design a RPC framework, we can consider it from these angles.
Performance
As the core component of micro-service, the amount of RPC invocation in a system is often very high, so performance is a very important consideration; since it is a remote call, it must involve network connection, and the choice of Imax O model directly affects performance, the long connection and short connection of the network and the serialization mode also affect the performance.
1.I/O model
The common Unix5 IhampO models are: blocking Ipicuro, non-blocking IMagazure Iplink O multiplexing (select,poll,epoll, etc.), signal-driven Imaco, asynchronous IhampO; from the early blocking Icando O mode, which can only create a large number of threads to ensure that each user does not affect each other, to the now widely used Imax O multiplexing model, and then to asynchronous Icano. From the select model to the current mainstream epoll model, the performance has been qualitatively upgraded; of course, we do not need to implement it ourselves, we can directly use the network communication framework Netty,Mina and so on.
two。 Long connection short connection
A short connection means that the connection is closed after each communication, while the connection is maintained after the long connection, so that there is no need to re-establish the connection next time. If the communication is frequent, the performance of the persistent connection is obviously higher. However, persistent connections need to do some extra work, such as keeping alive; in addition, if there are too many clients, the server cannot support it.
3. Serialization mode
Data in network transmission needs to be serialized and deserialized, so the performance of this block is also very important. Common serialization includes json and binary, json common such as fastjson,jackson, binary such as protobuf,thrift, kryo, etc. This can be considered in terms of serialization performance, size, and ease of use. Of course, stability and security also need to be considered, such as frequent security vulnerabilities in fastjson.
4. Agreement
Here we are mainly talking about application layer protocols. RPC generally customizes the protocols, and of course, there are also those that directly use existing protocols, such as http; custom protocols can be controlled by themselves, and the protocols can be done very small and concise, of course, decoding and coding need to be implemented on their own. If you use the existing http protocol, the whole protocol package is relatively large, but it is already a specification, and many things can be used directly, which is more general.
Expandability
Scalability can be viewed from two perspectives, one is the load balancing strategy of service providers and consumers, and the other is that users can customize the framework.
1. Load balancing
The two core components of RPC, the service provider and the consumer, need to provide a scale-out mechanism to achieve higher responsibility, such as the registry provided by Spring Cloud and Dubbo, and then combine the fault-tolerant mechanism to achieve load balancing. It is easy to achieve scale-out.
2.SPI mechanism
A good framework supports user definition, and users implement custom extensions according to their own needs; JDK provides SPI mechanism, and a common scenario is database driver; in addition, Dubbo provides a more powerful SPI mechanism on this basis; this extension is multifaceted for RPC, which can be underlying communication framework extension, serialization extension, registry extension, fault tolerance mechanism extension, protocol extension and so on.
Cross platform
Now there are a variety of development languages, if it can be made cross-platform, of course, it is a big advantage, but often in order to cross-platform, we will make trade-offs and concessions in some places, and of course it is more difficult; so when we implement a RPC, we need to make clear our own position, that is, whether it is for a certain language or cross-platform support; common RPC frameworks that support cross-platform, such as GRPC,Thrift
The implementation mechanism can be referred to, each has its own set of interface definition language IDL, and then through different code generators, generate a variety of programming language consumer-side and server-side code to ensure direct communication between different languages.
Functional function
As a RPC framework, in addition to the core communication module and serialization module, the functional module is also very important, which often saves developers a lot of time. Developers may choose to use this framework because of a certain function of RPC; common functions include: fault tolerance mechanism, load balancing mechanism, synchronous asynchronous calls, result caching, routing rules, service degradation, multi-version, thread model, etc.
1. Fault-tolerant mechanism
In the complex network environment, the failure of remote invocation is normal, and the fault tolerance mechanism is very necessary. Common fault tolerance mechanisms such as failure retry, rapid failure, direct neglect of failure, parallel call to multiple servers only need a successful return, etc. Users can choose their own appropriate fault tolerance scheme according to their needs.
two。 Responsible for equilibrium
The scalability of service providers and consumers is mentioned above. Consumers need to have a certain load balancing strategy for multiple providers to ensure the stability of the system; common strategies such as: random, polling, minimum number of active calls, consistent hash, etc.
3. Synchronous asynchronous invocation
Some common synchronous calls can not meet the requirements, for example, multiple remote methods need to be called at the same time, and some of them may be slow to execute; at this time, asynchronous calls are important, multiple requests can be sent asynchronously at the same time, and the waiting time is the slowest request response; it can be realized through Future,CompletableFuture
4. Result caching
Caching has always been the magic weapon of performance. In some scenarios, the real-time requirement of the data that may be responded to by the provider is not high. At this time, if the caching mechanism of the results can be provided at the service provider, then the performance will be greatly improved. You can design a local cache yourself, or of course you can directly integrate the third-party cache framework such as ehcache,jcache.
5. Routing rules
There are often many service providers, and users may have some special needs. They can do routing according to their own defined rules. For example, we often do grayscale publishing, and it will be very simple to combine the routing rules provided by RPC. This rule may be conditional expressions, scripts, tags, etc. The RPC framework we designed needs a place to define rules for users, such as real-time push through a registry, and related engines to process rules, such as script engines.
6. Service degradation
In the high availability principle of the system, a very important one is the degradation processing. In some non-core functions, it can be set as a degraded service directly in the event of a timeout / failure to give a unified response, so that valuable resources can be left to those core functions. You can refer to the implementation of Dubbo and write dynamic configuration coverage rules to the registry to achieve real-time degradation processing.
7. Multiple editions
Interface upgrade often happens, we may rack our brains to be compatible with the previous version, but sometimes there is nothing we can do about it. At this time, multiple versions become very important, so that two versions can exist at the same time, and wait for the right opportunity to upgrade slowly. The dimension of a service like dubbo is service name + version number, so it is good to implement multiple versions. However, the Spring Cloud dimension does not reach the version number, which can be realized through routing rules.
8. Thread model
Thread isolation is an important principle in the high availability principle of the system. Take Dubbo and its underlying communication framework netty as an example. As a communication framework, netty has its own thread model. If the business processing thread directly uses the underlying communication thread model, the communication thread model will be blocked because of business blocking. At this time, it is particularly important for Dubbo to provide its own thread model. Thread isolation can be achieved, and business threads do not affect communication threads.
Of course, there are many functions that can be realized as a RPC framework. Here we mainly talk about some of the more commonly used functions.
Can be monitored
It is impossible for a stable system without a special monitoring platform. Of course, RPC is no exception, such as dubbo's monitor module, Spring Cloud Admin and so on. For a RPC, we mainly monitor: what are the service providers, what are the service consumers, and their status, it is best to have some statistical functions, such as call volume over a period of time, success rate, failure rate, average response time, maximum response time, maximum concurrency, and so on.
Usability
Finally, the framework we designed will eventually be used by users, so whether users can easily use it is also a very important point. Some frameworks may be abandoned eventually because of the tedious use of them. For example, annotations are much simpler than xml. There is also, for example, the service dimension: the dubbo dimension is the interface, while the Spring cloud dimension is the application, so Spring cloud is more convenient to use as a whole; of course, simple API, documentation and Demo are also essential for developers.
Thank you for reading this article carefully. I hope the article "sample Analysis of RPC Design" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.