CN112596932A - Service registration and interception method and device, electronic equipment and readable storage medium - Google Patents
Service registration and interception method and device, electronic equipment and readable storage medium Download PDFInfo
- Publication number
- CN112596932A CN112596932A CN202110020904.0A CN202110020904A CN112596932A CN 112596932 A CN112596932 A CN 112596932A CN 202110020904 A CN202110020904 A CN 202110020904A CN 112596932 A CN112596932 A CN 112596932A
- Authority
- CN
- China
- Prior art keywords
- service
- registered
- interceptor
- name
- interception
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Granted
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/54—Interprogram communication
- G06F9/547—Remote procedure calls [RPC]; Web services
-
- Y—GENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
- Y02—TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
- Y02D—CLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
- Y02D10/00—Energy efficient computing, e.g. low power processors, power management or thermal management
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Stored Programmes (AREA)
Abstract
本发明涉及数据处理,揭露一种服务注册及拦截方法,包括:获取注解定义代码,基于待注册服务名称、待注册拦截器名称、待注册服务与待注册拦截器的绑定关系及待注册服务的待拦截方法名列表生成服务定义代码,服务定义代码包括注解标识;将注解定义代码及服务定义代码填入目标应用程序对应的主程序代码中,扫描主程序代码中的注解标识,对待注册服务和待注册拦截器执行注册操作及绑定操作;基于调用请求携带的一个或多个待调用服务名称及其待调用方法名称对调用请求执行拦截处理。本发明还提供一种服务注册及拦截装置、电子设备及可读存储介质。本发明提高了代码的可扩展性、可维护性,并实现了细粒化拦截。
The invention relates to data processing, and discloses a service registration and interception method, comprising: obtaining an annotation definition code; The list of method names to be intercepted generates the service definition code, and the service definition code includes the annotation identifier; fill the annotation definition code and service definition code into the main program code corresponding to the target application, scan the annotation identifier in the main program code, and treat the registration service. Perform registration operations and binding operations with the interceptor to be registered; perform interception processing on the invocation request based on the name of one or more services to be invoked and the name of the method to be invoked carried in the invocation request. The present invention also provides a service registration and interception device, an electronic device and a readable storage medium. The invention improves the expansibility and maintainability of the code, and realizes fine-grained interception.
Description
技术领域technical field
本发明涉及数据处理领域,尤其涉及一种服务注册及拦截方法、装置、电子设备及可读存储介质。The present invention relates to the field of data processing, and in particular, to a service registration and interception method, device, electronic device and readable storage medium.
背景技术Background technique
随着科技的发展,通讯在人们生活中的运用越来越广泛。gRPC(Google RemoteProcedure Call,Google远程过程调用)框架是一个基础的网络通讯组件,在通讯过程中涉及到服务的注册、拦截器的注册及服务与拦截器的绑定,而现有的gRPC框架需要逐一编写对应的注册代码和绑定代码,难以扩展,若需要注册的服务较多,则会造成代码过于臃肿,难以维护,且现有的gRPC框架会拦截服务下的所有方法,不能拦截指定的方法,无法实现细粒化拦截。因此,亟需一种服务注册及拦截方法,以提高代码的可扩展性、可维护性,并实现细粒化拦截。With the development of science and technology, communication is more and more widely used in people's life. The gRPC (Google RemoteProcedure Call, Google Remote Procedure Call) framework is a basic network communication component. The communication process involves service registration, interceptor registration, and binding of services and interceptors. The existing gRPC framework requires Write the corresponding registration code and binding code one by one, which is difficult to expand. If there are many services to be registered, the code will be too bloated and difficult to maintain, and the existing gRPC framework will intercept all methods under the service and cannot intercept the specified ones. method, which cannot achieve fine-grained interception. Therefore, there is an urgent need for a service registration and interception method to improve the scalability and maintainability of the code, and to achieve fine-grained interception.
发明内容SUMMARY OF THE INVENTION
鉴于以上内容,有必要提供一种服务注册及拦截方法,旨在提高代码的可扩展性、可维护性,并实现细粒化拦截。In view of the above, it is necessary to provide a service registration and interception method, which aims to improve the scalability and maintainability of the code, and realize fine-grained interception.
本发明提供的服务注册及拦截方法,包括:The service registration and interception method provided by the present invention includes:
解析用户基于客户端发出的针对目标应用程序的服务注册请求,获取所述服务注册请求携带的待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表;Parse the service registration request for the target application sent by the user based on the client, and obtain the name of the service to be registered carried in the service registration request, the name of the interceptor to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and A list of method names to be intercepted of the to-be-registered service;
从数据库中获取预先确定的注解定义代码,基于所述待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表生成服务定义代码,所述服务定义代码包括注解标识;Obtain a predetermined annotation definition code from the database, based on the name of the service to be registered, the name of the interceptor to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and the name of the method to be intercepted of the service to be registered The list generates service definition code, the service definition code includes an annotation identifier;
将所述注解定义代码及服务定义代码填入所述目标应用程序对应的主程序代码中,扫描所述主程序代码中的注解标识,对所述待注册服务和待注册拦截器执行注册操作及绑定操作;Fill in the main program code corresponding to the target application program with the annotation definition code and the service definition code, scan the annotation identifier in the main program code, and perform a registration operation on the to-be-registered service and the to-be-registered interceptor. bind operation;
实时监控针对所述目标应用程序的调用请求,获取所述调用请求携带的一个或多个待调用服务名称及其待调用方法名称,基于所述待调用服务名称及其待调用方法名称对所述调用请求执行拦截处理。Monitor the call request for the target application in real time, obtain one or more service names to be called and the name of the method to be called carried in the call request, and based on the name of the service to be called and the name of the method to be called Call request to perform interception processing.
可选的,所述服务注册请求还包括待拦截事务,所述基于所述待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表生成服务定义代码包括:Optionally, the service registration request further includes a transaction to be intercepted, the name of the service to be registered, the name of the interceptor to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and the to-be-registered interceptor. The service definition code for generating a list of method names to be intercepted for a service includes:
基于所述待注册拦截器名称及所述待拦截事务生成第一定义代码;generating a first definition code based on the name of the interceptor to be registered and the transaction to be intercepted;
基于所述待注册服务名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表生成第二定义代码;Generate a second definition code based on the name of the service to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and the list of method names to be intercepted of the service to be registered;
合并所述第一定义代码及第二定义代码得到服务定义代码。The first definition code and the second definition code are combined to obtain the service definition code.
可选的,所述扫描所述主程序代码中的注解标识,对所述待注册服务和待注册拦截器执行注册操作及绑定操作,包括:Optionally, performing a registration operation and a binding operation on the to-be-registered service and the to-be-registered interceptor by scanning the annotation identifier in the main program code, including:
扫描所述主程序代码中携带注解标识的程序段,对所述程序段执行正确性校验;Scan the program segment carrying the annotation identifier in the main program code, and perform a correctness check on the program segment;
当校验通过时,基于所述程序段生成拦截数据,基于所述拦截数据对所述待注册服务和待注册拦截器执行注册操作及绑定操作。When the verification is passed, interception data is generated based on the program segment, and a registration operation and a binding operation are performed on the service to be registered and the interceptor to be registered based on the interception data.
可选的,在对所述程序段执行正确性校验之后,所述方法还包括:Optionally, after performing the correctness check on the program segment, the method further includes:
若校验不通过,则对所述程序段进行标识,并发送预警信息至所述客户端。If the verification fails, the program segment is identified, and early warning information is sent to the client.
可选的,所述基于所述待调用服务名称及其待调用方法名称对所述调用请求执行拦截处理,包括:Optionally, performing interception processing on the invocation request based on the name of the service to be invoked and the name of the method to be invoked includes:
判断各个待调用服务是否绑定了拦截器;Determine whether each service to be called is bound with an interceptor;
当判断某一指定待调用服务绑定了拦截器时,判断所述指定待调用服务的待调用方法是否存在于所述指定待调用服务的待拦截方法名列表中;When judging that a specified service to be called is bound with an interceptor, it is judged whether the method to be called of the specified service to be called exists in the list of method names to be intercepted of the specified service to be called;
当判断存在于所述指定待调用服务的待拦截方法名列表中时,对所述调用请求执行拦截处理。When it is determined that it exists in the list of method names to be intercepted for the designated service to be invoked, an interception process is performed on the invocation request.
可选的,在判断各个待调用服务是否绑定了拦截器之后,所述方法还包括:Optionally, after judging whether each service to be called is bound with an interceptor, the method further includes:
若判断每个待调用服务均未绑定拦截器或所述指定待调用服务的待调用方法不存在于所述指定待调用服务的待拦截方法名列表中,则执行所述调用请求对应的调用操作。If it is determined that each service to be called is not bound with an interceptor or the method to be called of the specified service to be called does not exist in the list of method names to be intercepted for the specified service to be called, execute the call corresponding to the call request operate.
为了解决上述问题,本发明还提供一种服务注册及拦截装置,所述装置包括:In order to solve the above problems, the present invention also provides a service registration and interception device, the device includes:
解析模块,用于解析用户基于客户端发出的针对目标应用程序的服务注册请求,获取所述服务注册请求携带的待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表;The parsing module is used to parse the service registration request for the target application sent by the user based on the client, and obtain the name of the service to be registered, the name of the interceptor to be registered, the service to be registered and the interceptor to be registered carried in the service registration request. The binding relationship and the list of method names to be intercepted for the service to be registered;
生成模块,用于从数据库中获取预先确定的注解定义代码,基于所述待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表生成服务定义代码,所述服务定义代码包括注解标识;The generation module is used to obtain the predetermined annotation definition code from the database, based on the name of the service to be registered, the name of the interceptor to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and the service to be registered. The list of method names to be intercepted generates a service definition code, and the service definition code includes an annotation identifier;
扫描模块,用于将所述注解定义代码及服务定义代码填入所述目标应用程序对应的主程序代码中,扫描所述主程序代码中的注解标识,对所述待注册服务和待注册拦截器执行注册操作及绑定操作;A scanning module is used to fill the annotation definition code and service definition code into the main program code corresponding to the target application, scan the annotation identifier in the main program code, and intercept the service to be registered and the service to be registered. The device performs registration and binding operations;
拦截模块,用于实时监控针对所述目标应用程序的调用请求,获取所述调用请求携带的一个或多个待调用服务名称及其待调用方法名称,基于所述待调用服务名称及其待调用方法名称对所述调用请求执行拦截处理。The interception module is used to monitor the call request for the target application in real time, obtain one or more service names to be called and the name of the method to be called carried in the call request, and based on the name of the service to be called and the name of the method to be called The method name performs interception processing on the invocation request.
可选的,所述扫描所述主程序代码中的注解标识,对所述待注册服务和待注册拦截器执行注册操作及绑定操作,包括:Optionally, performing a registration operation and a binding operation on the to-be-registered service and the to-be-registered interceptor by scanning the annotation identifier in the main program code, including:
扫描所述主程序代码中携带注解标识的程序段,对所述程序段执行正确性校验;Scan the program segment carrying the annotation identifier in the main program code, and perform a correctness check on the program segment;
当校验通过时,基于所述程序段生成拦截数据,基于所述拦截数据对所述待注册服务和待注册拦截器执行注册操作及绑定操作。When the verification is passed, interception data is generated based on the program segment, and a registration operation and a binding operation are performed on the service to be registered and the interceptor to be registered based on the interception data.
为了解决上述问题,本发明还提供一种电子设备,所述电子设备包括:In order to solve the above problems, the present invention also provides an electronic device, the electronic device includes:
至少一个处理器;以及,at least one processor; and,
与所述至少一个处理器通信连接的存储器;其中,a memory communicatively coupled to the at least one processor; wherein,
所述存储器存储有可被所述至少一个处理器执行的服务注册及拦截程序,所述服务注册及拦截程序被所述至少一个处理器执行,以使所述至少一个处理器能够执行上述服务注册及拦截方法。The memory stores a service registration and interception program executable by the at least one processor, and the service registration and interception program is executed by the at least one processor to enable the at least one processor to perform the above-mentioned service registration and interception method.
为了解决上述问题,本发明还提供一种计算机可读存储介质,所述计算机可读存储介质上存储有服务注册及拦截程序,所述服务注册及拦截程序可被一个或者多个处理器执行,以实现上述服务注册及拦截方法。In order to solve the above problems, the present invention also provides a computer-readable storage medium, where a service registration and interception program is stored on the computer-readable storage medium, and the service registration and interception program can be executed by one or more processors, In order to realize the above service registration and interception methods.
相较现有技术,本发明首先获取注解定义代码,基于待注册服务名称、待注册拦截器名称、待注册服务与待注册拦截器的绑定关系及待注册服务的待拦截方法名列表生成服务定义代码,服务定义代码包括注解标识;接着,将注解定义代码及服务定义代码填入目标应用程序对应的主程序代码中,扫描主程序代码中的注解标识,对待注册服务和待注册拦截器执行注册操作及绑定操作;最后,基于调用请求携带的待调用服务名称及其待调用方法名称对调用请求执行拦截处理。本方案仅需在待注册服务对应的服务定义代码中打上注解标识,即可实现服务的注册、拦截器的注册及服务与拦截器的绑定,提高了代码的可扩展性、可维护性;通过在服务定义代码中添加服务的待拦截方法名列表,解决了现有gRPC只能拦截服务下的所有方法、而不能拦截服务下的指定方法的技术问题,实现了精细化拦截。因此,本发明提高了代码的可扩展性、可维护性,并实现了细粒化拦截。Compared with the prior art, the present invention first obtains the annotation definition code, and generates a service based on the name of the service to be registered, the name of the interceptor to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and the name list of the method to be intercepted of the service to be registered. Definition code, the service definition code includes the annotation identifier; then, fill in the annotation definition code and the service definition code into the main program code corresponding to the target application, scan the annotation identifier in the main program code, and execute the service to be registered and the interceptor to be registered. Registration operation and binding operation; finally, intercept the call request based on the name of the service to be called and the name of the method to be called carried in the call request. This solution only needs to mark the service definition code corresponding to the service to be registered to realize the registration of the service, the registration of the interceptor, and the binding of the service and the interceptor, which improves the scalability and maintainability of the code; By adding a list of method names to be intercepted for a service in the service definition code, the technical problem that the existing gRPC can only intercept all methods under the service but not the specified methods under the service is solved, and refined interception is realized. Therefore, the present invention improves the expansibility and maintainability of the code, and realizes fine-grained interception.
附图说明Description of drawings
图1为本发明一实施例提供的服务注册及拦截方法的流程示意图;1 is a schematic flowchart of a service registration and interception method provided by an embodiment of the present invention;
图2为本发明一实施例提供的服务注册及拦截装置的模块示意图;2 is a schematic block diagram of a service registration and interception apparatus provided by an embodiment of the present invention;
图3为本发明一实施例提供的实现服务注册及拦截方法的电子设备的结构示意图;3 is a schematic structural diagram of an electronic device implementing a service registration and interception method according to an embodiment of the present invention;
本发明目的的实现、功能特点及优点将结合实施例,参照附图做进一步说明。The realization, functional characteristics and advantages of the present invention will be further described with reference to the accompanying drawings in conjunction with the embodiments.
具体实施方式Detailed ways
为了使本发明的目的、技术方案及优点更加清楚明白,以下结合附图及实施例,对本发明进行进一步详细说明。应当理解,此处所描述的具体实施例仅用以解释本发明,并不用于限定本发明。基于本发明中的实施例,本领域普通技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。In order to make the objectives, technical solutions and advantages of the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention, but not to limit the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
需要说明的是,在本发明中涉及“第一”、“第二”等的描述仅用于描述目的,而不能理解为指示或暗示其相对重要性或者隐含指明所指示的技术特征的数量。由此,限定有“第一”、“第二”的特征可以明示或者隐含地包括至少一个该特征。另外,各个实施例之间的技术方案可以相互结合,但是必须是以本领域普通技术人员能够实现为基础,当技术方案的结合出现相互矛盾或无法实现时应当认为这种技术方案的结合不存在,也不在本发明要求的保护范围之内。It should be noted that the descriptions involving "first", "second", etc. in the present invention are only for the purpose of description, and should not be construed as indicating or implying their relative importance or implying the number of indicated technical features . Thus, a feature delimited with "first", "second" may expressly or implicitly include at least one of that feature. In addition, the technical solutions between the various embodiments can be combined with each other, but must be based on the realization by those of ordinary skill in the art. When the combination of technical solutions is contradictory or cannot be realized, it should be considered that the combination of such technical solutions does not exist. , is not within the scope of protection required by the present invention.
本发明提供一种服务注册及拦截方法。参照图1所示,为本发明一实施例提供的服务注册及拦截方法的流程示意图。该方法可以由一个电子设备执行,该电子设备可以由软件和/或硬件实现。The present invention provides a service registration and interception method. Referring to FIG. 1 , it is a schematic flowchart of a service registration and interception method provided by an embodiment of the present invention. The method may be performed by an electronic device, which may be implemented by software and/or hardware.
本实施例中,服务注册及拦截方法包括:In this embodiment, the service registration and interception methods include:
S1、解析用户基于客户端发出的针对目标应用程序的服务注册请求,获取所述服务注册请求携带的待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表。S1. Analyze the service registration request for the target application sent by the user based on the client, and obtain the service registration request to be registered, the name of the interceptor to be registered, the binding of the service to be registered and the interceptor to be registered. Relationship and a list of method names to be intercepted for the service to be registered.
所述目标应用程序可以是通讯类APP,该目标应用程序采用gRPC框架进行通讯。gRPC是一种通用的RPC框架,面向移动和HTTP/2设计,支持C、Java和Go语言版本,框架采用protobuf(protobuf是一种平台无关、语言无关、可扩展且轻便高效的序列化数据结构的协议,协议是以.proto后缀的文件保存)二进制消息的序列化与反序列化结构。The target application may be a communication APP, and the target application uses the gRPC framework for communication. gRPC is a general-purpose RPC framework, designed for mobile and HTTP/2, supports C, Java and Go language versions, the framework uses protobuf (protobuf is a platform-independent, language-independent, extensible, lightweight and efficient serialization data structure protocol, the protocol is stored in a file with a .proto suffix) serialization and deserialization structure of binary messages.
服务(即服务类),在.proto文件里是以service关键字定义的,通过.proto文件生成的JAVA版里是以继承了io.grpc.BindableService接口的类;服务可以简单理解为系统对外提供访问的接口。Service (ie service class) is defined by the service keyword in the .proto file, and the JAVA version generated by the .proto file is a class that inherits the io.grpc.BindableService interface; service can be simply understood as the system provided externally interface to access.
拦截器(即拦截器类),在gRPC框架里拦截器有客户端拦截器与服务端拦截器,本实施例中的拦截器为服务端拦截器,实现了io.grpc.ServerInterceptor接口的类为服务端拦截器。拦截器可以理解为在调用服务前做前置检查工作(将调用请求拦截下来后拦截器要做的前置检查工作可根据业务场景具体设置),例如1、调用者在访问需要登录后才能访问的服务时,拦截器可以先把调用者的请求拦截下来,判断调用者否已登录,若未登陆则不允许访问相应服务;例如2、系统要求调用者的IP地址要在系统的IP白名单内才允许访问服务,则可以用拦截器先拦截调用者请求,然后判断调用者IP是否在系统白名单内,若不在白名单内则拒绝访问服务,否则允许访问。Interceptor (ie, interceptor class). In the gRPC framework, the interceptor includes a client-side interceptor and a server-side interceptor. The interceptor in this embodiment is a server-side interceptor, and the class that implements the io.grpc.ServerInterceptor interface is Server-side interceptor. The interceptor can be understood as doing pre-check work before calling the service (the pre-check work that the interceptor needs to do after intercepting the call request can be set according to the business scenario), for example 1. The caller can only access after logging in. If the caller is not logged in, it is not allowed to access the corresponding service; for example, 2. The system requires the caller's IP address to be in the system's IP whitelist Only allow access to the service within the system, you can use the interceptor to intercept the caller's request first, and then determine whether the caller's IP is in the system's whitelist, if not, deny access to the service, otherwise allow access.
方法是用来完成某件事情或者实现某一功能(比如,控制输入或计算数值)的,通常会有一个返回值用来作为处理结果或计算结果。A method is used to complete something or implement a function (for example, control input or calculate a value), and usually has a return value used as a processing result or calculation result.
S2、从数据库中获取预先确定的注解定义代码,基于所述待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表生成服务定义代码,所述服务定义代码包括注解标识。S2. Obtain a predetermined annotation definition code from the database, based on the name of the service to be registered, the name of the interceptor to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and the interception of the service to be registered. The list of method names generates service definition code that includes annotation identifiers.
所述注解定义代码为自定义的gRPC服务注解代码,本实施例中,所述注解为@GrpcServer,@GrpcServer注解的属性有value(服务名,类型为String)、interceptor(拦截器类Class,须实现io.grpc.ServerInterceptor接口,如果不设置该属性,则表示该服务无拦截器绑定,否则表示该服务与拦截器绑定)、interceptMethods(待拦截的方法名列表,如果不设置该属性,则拦截服务类的所有方法,否则拦截服务类的待拦截的方法名列表中的方法;该属性必须要interceptor属性设置时才生效)。The annotation definition code is a self-defined gRPC service annotation code. In this embodiment, the annotation is @GrpcServer, and the attributes of the @GrpcServer annotation include value (service name, type is String), interceptor (interceptor class Class, must be Implement the io.grpc.ServerInterceptor interface. If this property is not set, it means that the service has no interceptor binding, otherwise it means that the service is bound with an interceptor), interceptMethods (a list of method names to be intercepted, if this property is not set, Then intercept all methods of the service class, otherwise intercept the methods in the list of method names to be intercepted in the service class; this attribute must be set when the interceptor attribute is set).
本实施例中,所述服务注册请求还包括待拦截事务(例如,判断调用者IP是否在系统白名单内),所述基于所述待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表生成服务定义代码包括:In this embodiment, the service registration request further includes the transaction to be intercepted (for example, judging whether the caller IP is in the system whitelist), and the request is based on the name of the service to be registered, the name of the interceptor to be registered, the name of the to be registered The binding relationship between the service and the interceptor to be registered and the method name list to be intercepted of the to-be-registered service to generate the service definition code include:
A11、基于所述待注册拦截器名称及所述待拦截事务生成第一定义代码;A11. Generate a first definition code based on the name of the interceptor to be registered and the transaction to be intercepted;
本实施例中,根据待注册拦截器名称可确定拦截器类名称xxxServerInterceptor,xxxServerInterceptor拦截器类需继承io.grpc.ServerInterceptor接口类,根据待拦截事务可生成对应的拦截业务代码,汇总后得到第一定义代码。In this embodiment, the interceptor class name xxxServerInterceptor can be determined according to the name of the interceptor to be registered. The xxxServerInterceptor interceptor class needs to inherit the io.grpc.ServerInterceptor interface class, and the corresponding interception business code can be generated according to the transaction to be intercepted. Define code.
A12、基于所述待注册服务名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表生成第二定义代码;A12. Generate a second definition code based on the name of the service to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and a list of method names to be intercepted of the service to be registered;
根据待注册服务名称可确定服务类名称xxxServer,xxxServer服务类需继承io.grpc.BindableService接口类,在xxxServer服务类上添加注解标识@GrpcServer,并根据待注册服务与待注册拦截器的绑定关系及待注册服务的待拦截方法名列表设置相关属性,汇总后得到第二定义代码。The service class name xxxServer can be determined according to the name of the service to be registered. The xxxServer service class needs to inherit the io.grpc.BindableService interface class, add the annotation @GrpcServer to the xxxServer service class, and according to the binding relationship between the service to be registered and the interceptor to be registered and the to-be-intercepted method name list of the service to be registered to set the relevant attributes, and the second definition code is obtained after summarizing.
A13、合并所述第一定义代码及第二定义代码得到服务定义代码。A13. Merge the first definition code and the second definition code to obtain a service definition code.
S3、将所述注解定义代码及服务定义代码填入所述目标应用程序对应的主程序代码中,扫描所述主程序代码中的注解标识,对所述待注册服务和待注册拦截器执行注册操作及绑定操作。S3. Fill the annotation definition code and service definition code into the main program code corresponding to the target application, scan the annotation identifier in the main program code, and perform registration on the to-be-registered service and the to-be-registered interceptor operations and binding operations.
本实施例中,所述扫描所述主程序代码中的注解标识,对所述待注册服务和待注册拦截器执行注册操作及绑定操作,包括:In this embodiment, the scanning of the annotation identifier in the main program code, and performing the registration operation and the binding operation on the to-be-registered service and the to-be-registered interceptor, include:
B11、扫描所述主程序代码中携带注解标识的程序段,对所述程序段执行正确性校验;B11. Scan the program segment carrying the annotation mark in the main program code, and perform correctness check on the program segment;
所述正确性校验包括校验程序段中的服务类是否继承了io.grpc.BindableService接口类、校验拦截器类是否继承了io.grpc.ServerInterceptor接口类以及语法正确性校验。The correctness check includes checking whether the service class in the program segment inherits the io.grpc.BindableService interface class, checking whether the interceptor class inherits the io.grpc.ServerInterceptor interface class, and checking the syntax correctness.
B12、当校验通过时,基于所述程序段生成拦截数据,基于所述拦截数据对所述待注册服务和待注册拦截器执行注册操作及绑定操作。B12. When the verification is passed, generate interception data based on the program segment, and perform a registration operation and a binding operation on the to-be-registered service and the to-be-registered interceptor based on the interception data.
本实施例中,所述拦截数据样例如下:In this embodiment, the intercepted data samples are as follows:
//拦截数据的结构体定义//The structure definition of intercepting data
struct GrpcServer{struct GrpcServer {
serverClass,//服务类serverClass,//Service class
interceptorClass,//拦截器类 interceptorClass,//Interceptor class
interceptMethods//要拦截该服务类下的方法名列表interceptMethods//List of method names under the service class to be intercepted
}}
本实施例中,通过调用gRPC框架提供的serverBuilder.addService方法循环将拦截数据中的服务和拦截器注册并绑定到系统中。In this embodiment, by calling the serverBuilder.addService method provided by the gRPC framework, the services and interceptors in the intercepted data are registered and bound to the system in a loop.
现有的gRPC框架在需要增加注册服务及绑定拦截器时,需要逐步添加每个服务及拦截器的注册代码、绑定代码,当增加的服务较多时,代码量较为庞大,而本实施例中,注册及绑定操作对应的代码只有一行,当需要增加服务注册及绑定拦截器时,只需要在对应的服务定义代码前添加注解标识并设置相关属性,删除服务或解绑拦截器时只需将注解标识删除或删除注解中的属性即可,本实施例使得代码可维护性更强且更易扩展。When the existing gRPC framework needs to add registration services and binding interceptors, it is necessary to gradually add the registration code and binding code of each service and interceptor. When more services are added, the amount of code is relatively large, and this embodiment There is only one line of code corresponding to registration and binding operations. When you need to add service registration and binding interceptors, you only need to add annotations and set relevant properties before the corresponding service definition code. When deleting services or unbinding interceptors It is only necessary to delete the annotation identifier or delete the attribute in the annotation. This embodiment makes the code more maintainable and easier to expand.
本实施例中,在对所述程序段执行正确性校验之后,所述方法还包括:In this embodiment, after the correctness check is performed on the program segment, the method further includes:
若校验不通过,则对所述程序段进行标识,并发送预警信息至所述客户端。If the verification fails, the program segment is identified, and early warning information is sent to the client.
S4、实时监控针对所述目标应用程序的调用请求,获取所述调用请求携带的一个或多个待调用服务名称及其待调用方法名称,基于所述待调用服务名称及其待调用方法名称对所述调用请求执行拦截处理。S4. Monitor the call request for the target application in real time, obtain one or more service names to be called and the names of the methods to be called carried in the call request, and compare the names of the services to be called and the names of the methods to be called based on the names of the services to be called and the names of the methods to be called. The invocation request performs interception processing.
所述基于所述待调用服务名称及其待调用方法名称对所述调用请求执行拦截处理,包括:The performing interception processing on the invocation request based on the name of the service to be invoked and the name of the method to be invoked includes:
C11、判断各个待调用服务是否绑定了拦截器;C11. Determine whether each service to be called is bound with an interceptor;
本实施例可根据程序段生成的拦截数据判断待调用服务是否绑定了拦截器。In this embodiment, it can be determined whether the service to be called is bound with an interceptor according to the interception data generated by the program segment.
C12、当判断某一指定待调用服务绑定了拦截器时,判断所述指定待调用服务的待调用方法是否存在于所述指定待调用服务的待拦截方法名列表中;C12. When judging that a specified service to be called is bound with an interceptor, judge whether the method to be called of the specified service to be called exists in the list of method names to be intercepted of the specified service to be called;
C13、当判断存在于所述指定待调用服务的待拦截方法名列表中时,对所述调用请求执行拦截处理。C13. When it is determined that the method name list to be intercepted of the designated service to be invoked exists, perform interception processing on the invocation request.
将调用请求拦截后,可根据待拦截事务对应的拦截业务代码执行相应的业务处理。不同于现有gRPC框架只能拦截到服务粒度(只能拦截服务下的所有方法),本步骤实现了精细化拦截,可拦截服务的待拦截方法名列表中的任一指定方法。After the call request is intercepted, corresponding business processing can be performed according to the intercepted business code corresponding to the transaction to be intercepted. Different from the existing gRPC framework, which can only intercept the granularity of the service (only all methods under the service can be intercepted), this step implements refined interception, which can intercept any specified method in the list of method names to be intercepted by the service.
本实施例中,在判断各个待调用服务是否绑定了拦截器之后,所述方法还包括:In this embodiment, after judging whether each service to be called is bound with an interceptor, the method further includes:
若判断每个待调用服务均未绑定拦截器或所述指定待调用服务的待调用方法不存在于所述指定待调用服务的待拦截方法名列表中,则执行所述调用请求对应的调用操作。If it is determined that each service to be called is not bound with an interceptor or the method to be called of the specified service to be called does not exist in the list of method names to be intercepted for the specified service to be called, execute the call corresponding to the call request operate.
由上述实施例可知,本发明提出的服务注册及拦截方法,首先,获取注解定义代码,基于待注册服务名称、待注册拦截器名称、待注册服务与待注册拦截器的绑定关系及待注册服务的待拦截方法名列表生成服务定义代码,服务定义代码包括注解标识;接着,将注解定义代码及服务定义代码填入目标应用程序对应的主程序代码中,扫描主程序代码中的注解标识,对待注册服务和待注册拦截器执行注册操作及绑定操作;最后,基于调用请求携带的待调用服务名称及其待调用方法名称对调用请求执行拦截处理。本方案仅需在待注册服务对应的服务定义代码中打上注解标识,即可实现服务的注册、拦截器的注册及服务与拦截器的绑定,提高了代码的可扩展性、可维护性;通过在服务定义代码中添加服务的待拦截方法名列表,解决了现有gRPC只能拦截服务下的所有方法、而不能拦截服务下的指定方法的技术问题,实现了精细化拦截。因此,本发明提高了代码的可扩展性、可维护性,并实现了细粒化拦截。As can be seen from the above embodiments, the service registration and interception method proposed by the present invention firstly obtains the annotation definition code, based on the name of the service to be registered, the name of the interceptor to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and the to-be-registered interceptor. The list of method names to be intercepted of the service generates the service definition code, and the service definition code includes the annotation identifier; then, fill in the annotation definition code and the service definition code into the main program code corresponding to the target application, and scan the annotation identifier in the main program code, The registration operation and the binding operation are performed on the service to be registered and the interceptor to be registered; finally, the interception processing is performed on the call request based on the name of the service to be called and the name of the method to be called carried in the call request. This solution only needs to mark the service definition code corresponding to the service to be registered to realize the registration of the service, the registration of the interceptor, and the binding of the service and the interceptor, which improves the scalability and maintainability of the code; By adding a list of method names to be intercepted for a service in the service definition code, the technical problem that the existing gRPC can only intercept all methods under the service but not the specified methods under the service is solved, and refined interception is realized. Therefore, the present invention improves the expansibility and maintainability of the code, and realizes fine-grained interception.
如图2所示,为本发明一实施例提供的服务注册及拦截装置的模块示意图。As shown in FIG. 2 , it is a schematic block diagram of a service registration and interception apparatus provided by an embodiment of the present invention.
本发明所述服务注册及拦截装置100可以安装于电子设备中。根据实现的功能,所述服务注册及拦截装置100可以包括解析模块110、生成模块120、扫描模块130及拦截模块140。本发明所述模块也可以称之为单元,是指一种能够被电子设备处理器所执行,并且能够完成固定功能的一系列计算机程序段,其存储在电子设备的存储器中。The service registration and interception apparatus 100 of the present invention can be installed in an electronic device. According to the implemented functions, the service registration and interception apparatus 100 may include a parsing module 110 , a generating module 120 , a scanning module 130 and an intercepting module 140 . The modules in the present invention can also be called units, which refer to a series of computer program segments that can be executed by the electronic device processor and can perform fixed functions, and are stored in the memory of the electronic device.
在本实施例中,关于各模块/单元的功能如下:In this embodiment, the functions of each module/unit are as follows:
解析模块110,用于解析用户基于客户端发出的针对目标应用程序的服务注册请求,获取所述服务注册请求携带的待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表。The parsing module 110 is configured to parse the service registration request for the target application sent by the user based on the client, and obtain the name of the service to be registered, the name of the interceptor to be registered, the service to be registered and the interception to be registered carried in the service registration request. The binding relationship of the server and the list of method names to be intercepted of the to-be-registered service.
所述目标应用程序可以是通讯类APP,该目标应用程序采用gRPC框架进行通讯。gRPC是一种通用的RPC框架,面向移动和HTTP/2设计,支持C、Java和Go语言版本,框架采用protobuf(protobuf是一种平台无关、语言无关、可扩展且轻便高效的序列化数据结构的协议,协议是以.proto后缀的文件保存)二进制消息的序列化与反序列化结构。The target application may be a communication APP, and the target application uses the gRPC framework for communication. gRPC is a general-purpose RPC framework, designed for mobile and HTTP/2, supports C, Java and Go language versions, the framework uses protobuf (protobuf is a platform-independent, language-independent, extensible, lightweight and efficient serialization data structure Protocol, the protocol is stored in a file with a .proto suffix) serialization and deserialization structure of binary messages.
服务(即服务类),在.proto文件里是以service关键字定义的,通过.proto文件生成的JAVA版里是以继承了io.grpc.BindableService接口的类;服务可以简单理解为系统对外提供访问的接口。Service (ie service class) is defined by the service keyword in the .proto file, and the JAVA version generated by the .proto file is a class that inherits the io.grpc.BindableService interface; service can be simply understood as the system provided externally interface to access.
拦截器(即拦截器类),在gRPC框架里拦截器有客户端拦截器与服务端拦截器,本实施例中的拦截器为服务端拦截器,实现了io.grpc.ServerInterceptor接口的类为服务端拦截器。拦截器可以理解为在调用服务前做前置检查工作(将调用请求拦截下来后拦截器要做的前置检查工作可根据业务场景具体设置),例如1、调用者在访问需要登录后才能访问的服务时,拦截器可以先把调用者的请求拦截下来,判断调用者否已登录,若未登陆则不允许访问相应服务;例如2、系统要求调用者的IP地址要在系统的IP白名单内才允许访问服务,则可以用拦截器先拦截调用者请求,然后判断调用者IP是否在系统白名单内,若不在白名单内则拒绝访问服务,否则允许访问。Interceptor (ie, interceptor class). In the gRPC framework, the interceptor includes a client-side interceptor and a server-side interceptor. The interceptor in this embodiment is a server-side interceptor, and the class that implements the io.grpc.ServerInterceptor interface is Server-side interceptor. The interceptor can be understood as doing pre-check work before calling the service (the pre-check work that the interceptor needs to do after intercepting the call request can be set according to the business scenario), for example 1. The caller can only access after logging in. If the caller is not logged in, it is not allowed to access the corresponding service; for example, 2. The system requires the caller's IP address to be in the system's IP whitelist Only allow access to the service within the system, you can use the interceptor to intercept the caller's request first, and then determine whether the caller's IP is in the system's whitelist, if not, deny access to the service, otherwise allow access.
方法是用来完成某件事情或者实现某一功能(比如,控制输入或计算数值)的,通常会有一个返回值用来作为处理结果或计算结果。A method is used to complete something or implement a function (for example, control input or calculate a value), and usually has a return value used as a processing result or calculation result.
生成模块120,用于从数据库中获取预先确定的注解定义代码,基于所述待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表生成服务定义代码,所述服务定义代码包括注解标识。The generating module 120 is used to obtain a predetermined annotation definition code from the database, based on the name of the service to be registered, the name of the interceptor to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and the to-be-registered interceptor The service definition code is generated from the list of method names to be intercepted for the service, and the service definition code includes the annotation identifier.
所述注解定义代码为自定义的gRPC服务注解代码,本实施例中,所述注解为@GrpcServer,@GrpcServer注解的属性有value(服务名,类型为String)、interceptor(拦截器类Class,须实现io.grpc.ServerInterceptor接口,如果不设置该属性,则表示该服务无拦截器绑定,否则表示该服务与拦截器绑定)、interceptMethods(待拦截的方法名列表,如果不设置该属性,则拦截服务类的所有方法,否则拦截服务类的待拦截的方法名列表中的方法;该属性必须要interceptor属性设置时才生效)。The annotation definition code is a self-defined gRPC service annotation code. In this embodiment, the annotation is @GrpcServer, and the attributes of the @GrpcServer annotation include value (service name, type is String), interceptor (interceptor class Class, must be Implement the io.grpc.ServerInterceptor interface. If this property is not set, it means that the service has no interceptor binding, otherwise it means that the service is bound with an interceptor), interceptMethods (a list of method names to be intercepted, if this property is not set, Then intercept all methods of the service class, otherwise intercept the methods in the list of method names to be intercepted in the service class; this attribute must be set when the interceptor attribute is set).
本实施例中,所述服务注册请求还包括待拦截事务(例如,判断调用者IP是否在系统白名单内),所述基于所述待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表生成服务定义代码包括:In this embodiment, the service registration request further includes the transaction to be intercepted (for example, judging whether the caller IP is in the system whitelist), and the request is based on the name of the service to be registered, the name of the interceptor to be registered, the name of the to be registered The binding relationship between the service and the interceptor to be registered and the method name list to be intercepted of the to-be-registered service to generate the service definition code include:
A21、基于所述待注册拦截器名称及所述待拦截事务生成第一定义代码;A21. Generate a first definition code based on the name of the interceptor to be registered and the transaction to be intercepted;
本实施例中,根据待注册拦截器名称可确定拦截器类名称xxxServerInterceptor,xxxServerInterceptor拦截器类需继承io.grpc.ServerInterceptor接口类,根据待拦截事务可生成对应的拦截业务代码,汇总后得到第一定义代码。In this embodiment, the interceptor class name xxxServerInterceptor can be determined according to the name of the interceptor to be registered. The xxxServerInterceptor interceptor class needs to inherit the io.grpc.ServerInterceptor interface class, and the corresponding interception business code can be generated according to the transaction to be intercepted. Define code.
A22、基于所述待注册服务名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表生成第二定义代码;A22. Generate a second definition code based on the name of the service to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and a list of method names to be intercepted of the service to be registered;
根据待注册服务名称可确定服务类名称xxxServer,xxxServer服务类需继承io.grpc.BindableService接口类,在xxxServer服务类上添加注解标识@GrpcServer,并根据待注册服务与待注册拦截器的绑定关系及待注册服务的待拦截方法名列表设置相关属性,汇总后得到第二定义代码。The service class name xxxServer can be determined according to the name of the service to be registered. The xxxServer service class needs to inherit the io.grpc.BindableService interface class, add the annotation @GrpcServer to the xxxServer service class, and according to the binding relationship between the service to be registered and the interceptor to be registered and the to-be-intercepted method name list of the service to be registered to set the relevant attributes, and the second definition code is obtained after summarizing.
A23、合并所述第一定义代码及第二定义代码得到服务定义代码。A23. Merge the first definition code and the second definition code to obtain a service definition code.
扫描模块130,用于将所述注解定义代码及服务定义代码填入所述目标应用程序对应的主程序代码中,扫描所述主程序代码中的注解标识,对所述待注册服务和待注册拦截器执行注册操作及绑定操作。The scanning module 130 is used to fill the annotation definition code and service definition code into the main program code corresponding to the target application, scan the annotation identifier in the main program code, and scan the to-be-registered service and to-be-registered service. Interceptors perform registration and binding operations.
本实施例中,所述扫描所述主程序代码中的注解标识,对所述待注册服务和待注册拦截器执行注册操作及绑定操作,包括:In this embodiment, the scanning of the annotation identifier in the main program code, and performing the registration operation and the binding operation on the to-be-registered service and the to-be-registered interceptor, include:
B21、扫描所述主程序代码中携带注解标识的程序段,对所述程序段执行正确性校验;B21. Scan the program segment carrying the annotation identifier in the main program code, and perform a correctness check on the program segment;
所述正确性校验包括校验程序段中的服务类是否继承了io.grpc.BindableService接口类、校验拦截器类是否继承了io.grpc.ServerInterceptor接口类以及语法正确性校验。The correctness check includes checking whether the service class in the program segment inherits the io.grpc.BindableService interface class, checking whether the interceptor class inherits the io.grpc.ServerInterceptor interface class, and checking the syntax correctness.
B22、当校验通过时,基于所述程序段生成拦截数据,基于所述拦截数据对所述待注册服务和待注册拦截器执行注册操作及绑定操作。B22. When the verification is passed, generate interception data based on the program segment, and perform a registration operation and a binding operation on the to-be-registered service and the to-be-registered interceptor based on the interception data.
本实施例中,所述拦截数据样例如下:In this embodiment, the intercepted data samples are as follows:
//拦截数据的结构体定义//The structure definition of intercepting data
struct GrpcServer{struct GrpcServer {
serverClass,//服务类serverClass,//Service class
interceptorClass,//拦截器类 interceptorClass,//Interceptor class
interceptMethods//要拦截该服务类下的方法名列表interceptMethods//List of method names under the service class to be intercepted
}}
本实施例中,通过调用gRPC框架提供的serverBuilder.addService方法循环将拦截数据中的服务和拦截器注册并绑定到系统中。In this embodiment, by calling the serverBuilder.addService method provided by the gRPC framework, the services and interceptors in the intercepted data are registered and bound to the system in a loop.
现有的gRPC框架在需要增加注册服务及绑定拦截器时,需要逐步添加每个服务及拦截器的注册代码、绑定代码,当增加的服务较多时,代码量较为庞大,而本实施例中,注册及绑定操作对应的代码只有一行,当需要增加服务注册及绑定拦截器时,只需要在对应的服务定义代码前添加注解标识并设置相关属性,删除服务或解绑拦截器时只需将注解标识删除或删除注解中的属性即可,本实施例使得代码可维护性更强且更易扩展。When the existing gRPC framework needs to add registration services and binding interceptors, it is necessary to gradually add the registration code and binding code of each service and interceptor. When more services are added, the amount of code is relatively large, and this embodiment There is only one line of code corresponding to registration and binding operations. When you need to add service registration and binding interceptors, you only need to add annotations and set relevant properties before the corresponding service definition code. When deleting services or unbinding interceptors It is only necessary to delete the annotation identifier or delete the attribute in the annotation. This embodiment makes the code more maintainable and easier to expand.
本实施例中,在对所述程序段执行正确性校验之后,所述扫描模块130还用于:In this embodiment, after the correctness check is performed on the program segment, the scanning module 130 is further configured to:
若校验不通过,则对所述程序段进行标识,并发送预警信息至所述客户端。If the verification fails, the program segment is identified, and early warning information is sent to the client.
拦截模块140,用于实时监控针对所述目标应用程序的调用请求,获取所述调用请求携带的一个或多个待调用服务名称及其待调用方法名称,基于所述待调用服务名称及其待调用方法名称对所述调用请求执行拦截处理。The interception module 140 is configured to monitor the call request for the target application in real time, obtain one or more service names to be called and the names of the methods to be called carried in the call request, and based on the name of the service to be called and the names of the methods to be called. The calling method name performs interception processing on the calling request.
所述基于所述待调用服务名称及其待调用方法名称对所述调用请求执行拦截处理,包括:The performing interception processing on the invocation request based on the name of the service to be invoked and the name of the method to be invoked includes:
C21、判断各个待调用服务是否绑定了拦截器;C21. Determine whether each service to be called is bound with an interceptor;
本实施例可根据程序段生成的拦截数据判断待调用服务是否绑定了拦截器。In this embodiment, it can be determined whether the service to be called is bound with an interceptor according to the interception data generated by the program segment.
C22、当判断某一指定待调用服务绑定了拦截器时,判断所述指定待调用服务的待调用方法是否存在于所述指定待调用服务的待拦截方法名列表中;C22. When judging that a specified service to be called is bound with an interceptor, judge whether the method to be called of the specified service to be called exists in the list of method names to be intercepted of the specified service to be called;
C23、当判断存在于所述指定待调用服务的待拦截方法名列表中时,对所述调用请求执行拦截处理。C23. When it is determined that the method name list to be intercepted of the designated service to be invoked exists, perform interception processing on the invocation request.
将调用请求拦截后,可根据待拦截事务对应的拦截业务代码执行相应的业务处理。不同于现有gRPC框架只能拦截到服务粒度(只能拦截服务下的所有方法),本步骤实现了精细化拦截,可拦截服务的待拦截方法名列表中的任一指定方法。After the call request is intercepted, corresponding business processing can be performed according to the intercepted business code corresponding to the transaction to be intercepted. Different from the existing gRPC framework, which can only intercept the granularity of the service (only all methods under the service can be intercepted), this step implements refined interception, which can intercept any specified method in the list of method names to be intercepted by the service.
本实施例中,在判断各个待调用服务是否绑定了拦截器之后,所述拦截模块140还用于:In this embodiment, after judging whether each service to be called is bound with an interceptor, the interception module 140 is further configured to:
若判断每个待调用服务均未绑定拦截器或所述指定待调用服务的待调用方法不存在于所述指定待调用服务的待拦截方法名列表中,则执行所述调用请求对应的调用操作。If it is determined that each service to be called is not bound with an interceptor or the method to be called of the specified service to be called does not exist in the list of method names to be intercepted for the specified service to be called, execute the call corresponding to the call request operate.
如图3所示,为本发明一实施例提供的实现服务注册及拦截方法的电子设备的结构示意图。As shown in FIG. 3 , it is a schematic structural diagram of an electronic device implementing a service registration and interception method according to an embodiment of the present invention.
所述电子设备1是一种能够按照事先设定或者存储的指令,自动进行数值计算和/或信息处理的设备。所述电子设备1可以是计算机、也可以是单个网络服务器、多个网络服务器组成的服务器组或者基于云计算的由大量主机或者网络服务器构成的云,其中云计算是分布式计算的一种,由一群松散耦合的计算机集组成的一个超级虚拟计算机。The electronic device 1 is a device that can automatically perform numerical calculation and/or information processing according to pre-set or stored instructions. The electronic device 1 may be a computer, a single network server, a server group composed of multiple network servers, or a cloud composed of a large number of hosts or network servers based on cloud computing, where cloud computing is a type of distributed computing, A super virtual computer consisting of a collection of loosely coupled computers.
在本实施例中,电子设备1包括,但不仅限于,可通过系统总线相互通信连接的存储器11、处理器12、网络接口13,该存储器11中存储有服务注册及拦截程序10,所述服务注册及拦截程序10可被所述处理器12执行。图3仅示出了具有组件11-13以及服务注册及拦截程序10的电子设备1,本领域技术人员可以理解的是,图3示出的结构并不构成对电子设备1的限定,可以包括比图示更少或者更多的部件,或者组合某些部件,或者不同的部件布置。In this embodiment, the electronic device 1 includes, but is not limited to, a memory 11, a processor 12, and a network interface 13 that can be communicatively connected to each other through a system bus. The memory 11 stores a service registration and interception program 10, the service The registration and interception procedure 10 may be executed by the processor 12 . FIG. 3 only shows the electronic device 1 with components 11-13 and the service registration and interception program 10. Those skilled in the art can understand that the structure shown in FIG. 3 does not constitute a limitation on the electronic device 1, and may include Fewer or more components than shown, or some components are combined, or a different arrangement of components.
其中,存储器11包括内存及至少一种类型的可读存储介质。内存为电子设备1的运行提供缓存;可读存储介质可为如闪存、硬盘、多媒体卡、卡型存储器(例如,SD或DX存储器等)、随机访问存储器(RAM)、静态随机访问存储器(SRAM)、只读存储器(ROM)、电可擦除可编程只读存储器(EEPROM)、可编程只读存储器(PROM)、磁性存储器、磁盘、光盘等的非易失性存储介质。在一些实施例中,可读存储介质可以是电子设备1的内部存储单元,例如该电子设备1的硬盘;在另一些实施例中,该非易失性存储介质也可以是电子设备1的外部存储设备,例如电子设备1上配备的插接式硬盘,智能存储卡(Smart Media Card,SMC),安全数字(Secure Digital,SD)卡,闪存卡(Flash Card)等。本实施例中,存储器11的可读存储介质通常用于存储安装于电子设备1的操作系统和各类应用软件,例如存储本发明一实施例中的服务注册及拦截程序10的代码等。此外,存储器11还可以用于暂时地存储已经输出或者将要输出的各类数据。The memory 11 includes a memory and at least one type of readable storage medium. The memory provides a cache for the operation of the electronic device 1; the readable storage medium can be, for example, flash memory, hard disk, multimedia card, card-type memory (for example, SD or DX memory, etc.), random access memory (RAM), static random access memory (SRAM) ), read only memory (ROM), electrically erasable programmable read only memory (EEPROM), programmable read only memory (PROM), magnetic memory, magnetic disk, optical disk, etc. non-volatile storage media. In some embodiments, the readable storage medium may be an internal storage unit of the electronic device 1, such as a hard disk of the electronic device 1; in other embodiments, the non-volatile storage medium may also be an external storage unit of the electronic device 1 A storage device, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash memory card (Flash Card), etc. equipped on the electronic device 1 . In this embodiment, the readable storage medium of the memory 11 is generally used to store the operating system and various application software installed in the electronic device 1 , for example, to store the code of the service registration and interception program 10 in an embodiment of the present invention. In addition, the memory 11 can also be used to temporarily store various types of data that have been output or will be output.
处理器12在一些实施例中可以是中央处理器(Central Processing Unit,CPU)、控制器、微控制器、微处理器、或其他数据处理芯片。该处理器12通常用于控制所述电子设备1的总体操作,例如执行与其他设备进行数据交互或者通信相关的控制和处理等。本实施例中,所述处理器12用于运行所述存储器11中存储的程序代码或者处理数据,例如运行服务注册及拦截程序10等。In some embodiments, the processor 12 may be a central processing unit (Central Processing Unit, CPU), a controller, a microcontroller, a microprocessor, or other data processing chips. The processor 12 is generally used to control the overall operation of the electronic device 1, such as performing control and processing related to data interaction or communication with other devices. In this embodiment, the processor 12 is used for running the program code or processing data stored in the memory 11, for example, running the service registration and interception program 10 and the like.
网络接口13可包括无线网络接口或有线网络接口,该网络接口13用于在所述电子设备1与客户端(图中未画出)之间建立通信连接。The network interface 13 may include a wireless network interface or a wired network interface, and the network interface 13 is used to establish a communication connection between the electronic device 1 and a client (not shown in the figure).
可选的,所述电子设备1还可以包括用户接口,用户接口可以包括显示器(Display)、输入单元比如键盘(Keyboard),可选的用户接口还可以包括标准的有线接口、无线接口。可选的,在一些实施例中,显示器可以是LED显示器、液晶显示器、触控式液晶显示器以及OLED(Organic Light-Emitting Diode,有机发光二极管)触摸器等。其中,显示器也可以适当的称为显示屏或显示单元,用于显示在电子设备1中处理的信息以及用于显示可视化的用户界面。Optionally, the electronic device 1 may further include a user interface, and the user interface may include a display (Display), an input unit such as a keyboard (Keyboard), and an optional user interface may also include a standard wired interface and a wireless interface. Optionally, in some embodiments, the display may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, an OLED (Organic Light-Emitting Diode, organic light-emitting diode) touch device, and the like. The display may also be appropriately called a display screen or a display unit, which is used for displaying information processed in the electronic device 1 and for displaying a visualized user interface.
应该了解,所述实施例仅为说明之用,在专利申请范围上并不受此结构的限制。It should be understood that the embodiments are only used for illustration, and are not limited by this structure in the scope of the patent application.
所述电子设备1中的所述存储器11存储的服务注册及拦截程序10是多个指令的组合,在所述处理器12中运行时,可以实现:The service registration and interception program 10 stored in the memory 11 in the electronic device 1 is a combination of multiple instructions, and when running in the processor 12, can realize:
解析用户基于客户端发出的针对目标应用程序的服务注册请求,获取所述服务注册请求携带的待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表;Parse the service registration request for the target application sent by the user based on the client, and obtain the name of the service to be registered carried in the service registration request, the name of the interceptor to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and A list of method names to be intercepted of the to-be-registered service;
从数据库中获取预先确定的注解定义代码,基于所述待注册服务名称、待注册拦截器名称、所述待注册服务与待注册拦截器的绑定关系及所述待注册服务的待拦截方法名列表生成服务定义代码,所述服务定义代码包括注解标识;Obtain a predetermined annotation definition code from the database, based on the name of the service to be registered, the name of the interceptor to be registered, the binding relationship between the service to be registered and the interceptor to be registered, and the name of the method to be intercepted of the service to be registered The list generates service definition code, the service definition code includes an annotation identifier;
将所述注解定义代码及服务定义代码填入所述目标应用程序对应的主程序代码中,扫描所述主程序代码中的注解标识,对所述待注册服务和待注册拦截器执行注册操作及绑定操作;Fill in the main program code corresponding to the target application program with the annotation definition code and the service definition code, scan the annotation identifier in the main program code, and perform a registration operation on the to-be-registered service and the to-be-registered interceptor. bind operation;
实时监控针对所述目标应用程序的调用请求,获取所述调用请求携带的一个或多个待调用服务名称及其待调用方法名称,基于所述待调用服务名称及其待调用方法名称对所述调用请求执行拦截处理。Monitor the call request for the target application in real time, obtain one or more service names to be called and the name of the method to be called carried in the call request, and based on the name of the service to be called and the name of the method to be called Call request to perform interception processing.
具体地,所述处理器12对上述服务注册及拦截程序10的具体实现方法可参考图1对应实施例中相关步骤的描述,在此不赘述。Specifically, for the specific implementation method of the above-mentioned service registration and interception program 10 by the processor 12, reference may be made to the description of the relevant steps in the corresponding embodiment of FIG. 1, and details are not described herein.
进一步地,所述电子设备1集成的模块/单元如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储介质中。所述计算机可读介质可以是非易失性的,也可以是非易失性的。所述计算机可读介质可以包括:能够携带所述计算机程序代码的任何实体或装置、记录介质、U盘、移动硬盘、磁碟、光盘、计算机存储器、只读存储器(ROM,Read-Only Memory)。Further, if the modules/units integrated in the electronic device 1 are implemented in the form of software functional units and sold or used as independent products, they may be stored in a computer-readable storage medium. The computer-readable medium may be non-volatile or non-volatile. The computer-readable medium may include: any entity or device capable of carrying the computer program code, a recording medium, a U disk, a removable hard disk, a magnetic disk, an optical disk, a computer memory, a read-only memory (ROM, Read-Only Memory) .
所述计算机可读存储介质上存储有服务注册及拦截程序10,所述服务注册及拦截程序10可被一个或者多个处理器执行,本发明计算机可读存储介质具体实施方式与上述服务注册及拦截方法各实施例基本相同,在此不作赘述。The computer-readable storage medium stores a service registration and interception program 10, and the service registration and interception program 10 can be executed by one or more processors. Each embodiment of the interception method is basically the same, and will not be repeated here.
在本发明所提供的几个实施例中,应该理解到,所揭露的设备,装置和方法,可以通过其它的方式实现。例如,以上所描述的装置实施例仅仅是示意性的,例如,所述模块的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式。In the several embodiments provided by the present invention, it should be understood that the disclosed apparatus, apparatus and method may be implemented in other manners. For example, the apparatus embodiments described above are only illustrative. For example, the division of the modules is only a logical function division, and there may be other division manners in actual implementation.
所述作为分离部件说明的模块可以是或者也可以不是物理上分开的,作为模块显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分或者全部模块来实现本实施例方案的目的。The modules described as separate components may or may not be physically separated, and components shown as modules may or may not be physical units, that is, may be located in one place, or may be distributed to multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution in this embodiment.
另外,在本发明各个实施例中的各功能模块可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。上述集成的单元既可以采用硬件的形式实现,也可以采用硬件加软件功能模块的形式实现。In addition, each functional module in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically alone, or two or more units may be integrated into one unit. The above-mentioned integrated units can be implemented in the form of hardware, or can be implemented in the form of hardware plus software function modules.
对于本领域技术人员而言,显然本发明不限于上述示范性实施例的细节,而且在不背离本发明的精神或基本特征的情况下,能够以其他的具体形式实现本发明。It will be apparent to those skilled in the art that the present invention is not limited to the details of the above-described exemplary embodiments, but that the present invention may be embodied in other specific forms without departing from the spirit or essential characteristics of the invention.
因此,无论从哪一点来看,均应将实施例看作是示范性的,而且是非限制性的,本发明的范围由所附权利要求而不是上述说明限定,因此旨在将落在权利要求的等同要件的含义和范围内的所有变化涵括在本发明内。不应将权利要求中的任何附关联图标记视为限制所涉及的权利要求。Therefore, the embodiments are to be regarded in all respects as illustrative and not restrictive, and the scope of the invention is to be defined by the appended claims rather than the foregoing description, which are therefore intended to fall within the scope of the claims. All changes within the meaning and range of the equivalents of , are included in the present invention. Any reference signs in the claims shall not be construed as limiting the involved claim.
此外,显然“包括”一词不排除其他单元或步骤,单数不排除复数。系统权利要求中陈述的多个单元或装置也可以由一个单元或装置通过软件或者硬件来实现。第二等词语用来表示名称,而并不表示任何特定的顺序。Furthermore, it is clear that the word "comprising" does not exclude other units or steps and the singular does not exclude the plural. Several units or means recited in the system claims can also be realized by one unit or means by means of software or hardware. Second-class terms are used to denote names and do not denote any particular order.
最后应说明的是,以上实施例仅用以说明本发明的技术方案而非限制,尽管参照较佳实施例对本发明进行了详细说明,本领域的普通技术人员应当理解,可以对本发明的技术方案进行修改或等同替换,而不脱离本发明技术方案的精神和范围。Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and not to limit them. Although the present invention has been described in detail with reference to the preferred embodiments, those of ordinary skill in the art should understand that the technical solutions of the present invention can be Modifications or equivalent substitutions can be made without departing from the spirit and scope of the technical solutions of the present invention.
Claims (10)
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202110020904.0A CN112596932B (en) | 2021-01-04 | 2021-01-04 | Service registration and interception method, device, electronic device and readable storage medium |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202110020904.0A CN112596932B (en) | 2021-01-04 | 2021-01-04 | Service registration and interception method, device, electronic device and readable storage medium |
Publications (2)
| Publication Number | Publication Date |
|---|---|
| CN112596932A true CN112596932A (en) | 2021-04-02 |
| CN112596932B CN112596932B (en) | 2025-01-14 |
Family
ID=75207479
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN202110020904.0A Active CN112596932B (en) | 2021-01-04 | 2021-01-04 | Service registration and interception method, device, electronic device and readable storage medium |
Country Status (1)
| Country | Link |
|---|---|
| CN (1) | CN112596932B (en) |
Cited By (7)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN113111378A (en) * | 2021-04-14 | 2021-07-13 | 山东英信计算机技术有限公司 | Terminal, storage medium, annotation method and system, and pre-verification method and system |
| CN113162998A (en) * | 2021-04-07 | 2021-07-23 | 广州炫视智能科技有限公司 | Intelligent interactive communication method and communication system |
| CN113220723A (en) * | 2021-04-27 | 2021-08-06 | 深圳市云网万店科技有限公司 | Flow control method and device, computer equipment and storage medium |
| CN113691511A (en) * | 2021-08-13 | 2021-11-23 | 广州华多网络科技有限公司 | Service request processing method, device, equipment and medium thereof |
| CN114610381A (en) * | 2022-03-10 | 2022-06-10 | 北京沃东天骏信息技术有限公司 | Method, device, device and storage medium for invoking method service |
| CN115269060A (en) * | 2022-06-15 | 2022-11-01 | 知学云(北京)科技股份有限公司 | Service execution pre-post processing method based on aPaaS platform |
| CN116909681A (en) * | 2023-06-13 | 2023-10-20 | 北京远舢智能科技有限公司 | Generation method, device, electronic equipment and storage medium of data processing component |
Citations (7)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US20040143625A1 (en) * | 2002-12-13 | 2004-07-22 | Joseph Sheinis | Component proxy with interception logic in remote component container |
| US20060031176A1 (en) * | 2004-05-21 | 2006-02-09 | Bea Systems, Inc. | System and method for message handling using message interceptors |
| CN104811453A (en) * | 2012-09-29 | 2015-07-29 | 北京奇虎科技有限公司 | Active defense method and device |
| CN105956470A (en) * | 2016-05-03 | 2016-09-21 | 北京金山安全软件有限公司 | Method and terminal for intercepting application program behaviors |
| CN111314496A (en) * | 2020-05-15 | 2020-06-19 | 太平金融科技服务(上海)有限公司 | Registration request intercepting method and device, computer equipment and storage medium |
| WO2020167481A1 (en) * | 2019-02-14 | 2020-08-20 | Microsoft Technology Licensing, Llc | Extensible device driver verification |
| CN111913741A (en) * | 2020-08-31 | 2020-11-10 | 网易(杭州)网络有限公司 | Object interception method, device, medium and electronic equipment |
-
2021
- 2021-01-04 CN CN202110020904.0A patent/CN112596932B/en active Active
Patent Citations (7)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US20040143625A1 (en) * | 2002-12-13 | 2004-07-22 | Joseph Sheinis | Component proxy with interception logic in remote component container |
| US20060031176A1 (en) * | 2004-05-21 | 2006-02-09 | Bea Systems, Inc. | System and method for message handling using message interceptors |
| CN104811453A (en) * | 2012-09-29 | 2015-07-29 | 北京奇虎科技有限公司 | Active defense method and device |
| CN105956470A (en) * | 2016-05-03 | 2016-09-21 | 北京金山安全软件有限公司 | Method and terminal for intercepting application program behaviors |
| WO2020167481A1 (en) * | 2019-02-14 | 2020-08-20 | Microsoft Technology Licensing, Llc | Extensible device driver verification |
| CN111314496A (en) * | 2020-05-15 | 2020-06-19 | 太平金融科技服务(上海)有限公司 | Registration request intercepting method and device, computer equipment and storage medium |
| CN111913741A (en) * | 2020-08-31 | 2020-11-10 | 网易(杭州)网络有限公司 | Object interception method, device, medium and electronic equipment |
Non-Patent Citations (1)
| Title |
|---|
| 阿伟: "gRPC拦截器那点事,希望帮到你", pages 1 - 4, Retrieved from the Internet <URL:https://cloud.tencent.com/developer/article/1507514> * |
Cited By (9)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN113162998A (en) * | 2021-04-07 | 2021-07-23 | 广州炫视智能科技有限公司 | Intelligent interactive communication method and communication system |
| CN113111378A (en) * | 2021-04-14 | 2021-07-13 | 山东英信计算机技术有限公司 | Terminal, storage medium, annotation method and system, and pre-verification method and system |
| CN113220723A (en) * | 2021-04-27 | 2021-08-06 | 深圳市云网万店科技有限公司 | Flow control method and device, computer equipment and storage medium |
| CN113691511A (en) * | 2021-08-13 | 2021-11-23 | 广州华多网络科技有限公司 | Service request processing method, device, equipment and medium thereof |
| CN113691511B (en) * | 2021-08-13 | 2024-01-19 | 广州华多网络科技有限公司 | Service request processing method and device, equipment and medium thereof |
| CN114610381A (en) * | 2022-03-10 | 2022-06-10 | 北京沃东天骏信息技术有限公司 | Method, device, device and storage medium for invoking method service |
| CN115269060A (en) * | 2022-06-15 | 2022-11-01 | 知学云(北京)科技股份有限公司 | Service execution pre-post processing method based on aPaaS platform |
| CN115269060B (en) * | 2022-06-15 | 2023-06-20 | 知学云(北京)科技股份有限公司 | Service execution pre-post processing method based on aPaaS platform |
| CN116909681A (en) * | 2023-06-13 | 2023-10-20 | 北京远舢智能科技有限公司 | Generation method, device, electronic equipment and storage medium of data processing component |
Also Published As
| Publication number | Publication date |
|---|---|
| CN112596932B (en) | 2025-01-14 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| CN112596932A (en) | Service registration and interception method and device, electronic equipment and readable storage medium | |
| JP5021886B2 (en) | System and method for actively managing an enterprise comprising configurable components | |
| CN108052334B (en) | Page jumping method, apparatus, computer equipment and storage medium | |
| CN113946425A (en) | Service processing method and device, electronic equipment and computer readable storage medium | |
| CN111901238B (en) | Dynamic routing service method, device, electronic device and storage medium | |
| CN111310427A (en) | Service data configuration processing method, apparatus, computer equipment and storage medium | |
| CN111475376A (en) | Method and device for processing test data, computer equipment and storage medium | |
| US9590859B2 (en) | Discovering resources of a distributed computing environment | |
| CN103970758A (en) | Database accessing system and method | |
| WO2023029509A1 (en) | Dynamic service publishing method and apparatus, and electronic device and storage medium | |
| CN114238391A (en) | Data paging query method and device, electronic equipment and storage medium | |
| US12314425B2 (en) | Privacy data management in distributed computing systems | |
| CN115086047A (en) | Interface authentication method and device, electronic equipment and storage medium | |
| CN117009397A (en) | Data query method, data query device, electronic equipment and storage medium | |
| CN115733828A (en) | A method, device, electronic device and storage medium for identifying API parameters | |
| CN114036068B (en) | Update detection method, device, equipment and storage medium based on privacy security | |
| CN116258461A (en) | A business process orchestration method, execution method and device | |
| CN114637969A (en) | Authentication method and device for target object | |
| CN111782244A (en) | Configuration file update method, device, computer equipment and storage medium | |
| CN113364848B (en) | File caching method and device, electronic equipment and storage medium | |
| CN110413644B (en) | Data caching method, electronic device and computer readable storage medium | |
| CN113746661B (en) | A business processing method and device | |
| CN111695986B (en) | Method and device for controlling accumulated gold image | |
| CN115277857A (en) | Method and device for interface verification, electronic equipment and storage medium | |
| CN114266072A (en) | Authority distribution control method and device, electronic equipment and storage medium |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| PB01 | Publication | ||
| PB01 | Publication | ||
| SE01 | Entry into force of request for substantive examination | ||
| SE01 | Entry into force of request for substantive examination | ||
| GR01 | Patent grant | ||
| GR01 | Patent grant |
