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

Implement a mini version of RPC

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Preface

In the actual backend service development, for example, the order service (developer An is responsible) needs to call the commodity service (developer B is responsible), then developer B will make an agreement with A to call API and provide it to An in the form of interface. Usually, B uploads API to Maven private server, then B starts to write the implementation of API, and An only needs to introduce API dependency for development.

Hands-on realization of RPC Commodity Service Project

Note that I have divided the API and implementation of merchandise services into two modules of Maven. Here, we want to give a commodity ID and query to get the product object information.

Commodity object

Note that Product can be serialized, Why?

Obviously, when the order system calls the commodity system, it needs the commodity system to return a commodity, which inevitably involves network transmission, which involves the serialization and deserialization of objects.

API interface for commodity query

Order system calls commodity service

It is necessary to introduce API dependence on goods and services in order system engineering.

In the code above, the most important thing is the rpc method!

Rpc implementation method

First, we see Proxy.newProxyInstance, which is obviously doing dynamic proxies. In other words, in the code in which the order service invokes the commodity service, we first return a proxy's IProductService type object through a dynamic proxy, which means that when the proxy object calls the queryById method, the invoke method is automatically called!

Second, let's see what invoke has done.

It is essentially Socket communication, so what information does it need to convey to goods and services?

We know that the order system wants to call a method of a certain class of commodity services, and then transfer the return result of this method to the order system!

Think about it, how do you call a method of a class?

As long as we can determine the fully qualified class name of this class, determine the method name, and determine the parameter type of the method, given the specific parameters needed by the method, it can be realized through reflection.

The result obtained after the commodity service call is serialized into the Socket stream and deserialized in the order system to get the object.

Third, we need to think about a problem here: in the order system, we only know the API of goods and services, but we do not know how the API behind this is realized, so we need to have a mapping, that is, a mapping from the API of goods and services to the implementation of goods and services. in fact, this is the so-called service registration!

Concrete realization of Commodity API

Goods and services

From here, we can see clearly that the commodity service reads the data sent by the order system when calling the commodity system, uses the reflection mechanism to call the method, and writes the call result into the Socket output stream.

Running result

After starting the commodity service, the invocation of the commodity service is initiated through the order system.

In the past, I always thought that RPC was out of reach, and it felt like a magical thing. In fact, isn't that what its underlying implementation is?

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