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 a simple RPC Framework

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

Share

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

How to achieve a simple RPC framework, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

1. Define context object

Add a parameter object of type map to the RpcContext object, which can store any extended parameters.

Add context parameters to the 2.RPC request object

RpcRequest adds the following fields for server-side invocation.

Add context parameters to 3.RpcInvocation interface

In the subsequent use of newly added filters.

4. Client agent

When RpcProxy assembles the RpcRequest object, it gets the latest parameters from RpcContext and passes them to RpcReuest, which is passed to the server.

5. Client context filter

The main function is to get the parameters from the thread variables of the local line and pass them to RpcInvocation.

6. Server context filter

The server-side context filter is contrary to the function of the client, which is to get the parameters from RpcInvocation and pass them to the local thread variable RpcContext. Later, when executing the server-side method, you can easily get the specified variables through RpcContext.

7. Filter sorting

Because our RpcContext is a local thread variable, and the Rpc server handles business with multiple threads, we need to clean up the relevant local thread variable information in time after the request ends. This requires the filtering action to clean up the context to be performed at the end, otherwise the parameters will be emptied before the server-side method is executed. Create a utility class that specifically handles getting client-side and server-side filters. Add the order field and upgrade the arrangement.

8. Filter tool class

Create an ActiveFilterUtil that contains the following two functions.

(1) filter the sort function, get the class of a specific annotation, and then sort it in ascending order according to the sort attribute on the annotation.

(2) get the list of RPC filters and provide an assistance method for the client and the server to facilitate the client and the service to build the filter responsibility chain.

9. Client and server initialization

The logic to get the filter map is changed to call the above ActiveFilterUtil.getFilterMap method.

After reading the above, have you mastered how to implement a simple RPC framework? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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