WO2022222442A1 - Method for client to dynamically call server by means of grpc - Google Patents
Method for client to dynamically call server by means of grpc Download PDFInfo
- Publication number
- WO2022222442A1 WO2022222442A1 PCT/CN2021/129976 CN2021129976W WO2022222442A1 WO 2022222442 A1 WO2022222442 A1 WO 2022222442A1 CN 2021129976 W CN2021129976 W CN 2021129976W WO 2022222442 A1 WO2022222442 A1 WO 2022222442A1
- Authority
- WO
- WIPO (PCT)
- Prior art keywords
- grpc
- server
- client
- interface
- dynamically
- Prior art date
Links
- 238000000034 method Methods 0.000 title claims abstract description 64
- 239000007858 starting material Substances 0.000 claims abstract description 7
- 230000002457 bidirectional effect Effects 0.000 claims abstract description 4
- 230000004044 response Effects 0.000 claims abstract description 4
- 230000006978 adaptation Effects 0.000 claims description 29
- 238000011161 development Methods 0.000 claims description 11
- 230000008676 import Effects 0.000 claims description 7
- 230000010354 integration Effects 0.000 claims description 6
- 230000008569 process Effects 0.000 claims description 5
- 230000007246 mechanism Effects 0.000 claims description 4
- 230000003044 adaptive effect Effects 0.000 abstract 2
- 238000004891 communication Methods 0.000 description 4
- 238000012544 monitoring process Methods 0.000 description 4
- 235000010627 Phaseolus vulgaris Nutrition 0.000 description 3
- 244000046052 Phaseolus vulgaris Species 0.000 description 3
- 230000008901 benefit Effects 0.000 description 3
- 238000005516 engineering process Methods 0.000 description 3
- 230000000694 effects Effects 0.000 description 2
- 230000009471 action Effects 0.000 description 1
- 230000009286 beneficial effect Effects 0.000 description 1
- 238000006243 chemical reaction Methods 0.000 description 1
- 238000010586 diagram Methods 0.000 description 1
- 230000006870 function Effects 0.000 description 1
- 230000004927 fusion Effects 0.000 description 1
- 230000003993 interaction Effects 0.000 description 1
- 238000012423 maintenance Methods 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 230000003252 repetitive effect Effects 0.000 description 1
- 230000001360 synchronised effect Effects 0.000 description 1
- 238000012360 testing method Methods 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR 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/44—Arrangements for executing specific programs
- G06F9/445—Program loading or initiating
- G06F9/44521—Dynamic linking or loading; Link editing at or after load time, e.g. Java class loading
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/20—Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
- G06F16/28—Databases characterised by their database models, e.g. relational or object models
- G06F16/283—Multi-dimensional databases or data warehouses, e.g. MOLAP or ROLAP
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR 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/44—Arrangements for executing specific programs
- G06F9/445—Program loading or initiating
- G06F9/44505—Configuring for program initiating, e.g. using registry, configuration files
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06Q—INFORMATION AND COMMUNICATION TECHNOLOGY [ICT] SPECIALLY ADAPTED FOR ADMINISTRATIVE, COMMERCIAL, FINANCIAL, MANAGERIAL OR SUPERVISORY PURPOSES; SYSTEMS OR METHODS SPECIALLY ADAPTED FOR ADMINISTRATIVE, COMMERCIAL, FINANCIAL, MANAGERIAL OR SUPERVISORY PURPOSES, NOT OTHERWISE PROVIDED FOR
- G06Q50/00—Information and communication technology [ICT] specially adapted for implementation of business processes of specific business sectors, e.g. utilities or tourism
- G06Q50/06—Energy or water supply
Definitions
- the invention belongs to the technical field of security protection communication security of a power monitoring system, and in particular relates to a method for a client to dynamically call a server through GRPC.
- RPC Remote Procedure Call
- one of the important components of the serialization protocol is to serialize and deserialize the transmitted binary data.
- the advantage is that it is completely object-oriented development.
- Both ends of the communication need to define the interface protocol in advance, output the file, compile the file into a java file, and import the file from the server, aiming at the protocol interface.
- the client introduces this file, fills in the specified type parameters, and makes a call, thereby realizing the entire link of the GRPC call, see Figure 1 for details.
- the client Before calling any interface of the server, the client must know the definition of the interface, that is, the protocol file.
- the problems it faces include: (1) The client and the server maintain the protocol files at the same time, and whether the file synchronization is timely. (2) When the client wants to call the server, it must know the protocol file in advance and do a lot of repetitive work.
- the present invention proposes a method for the client to dynamically call the server through GRPC, which can save the cost of jointly maintaining the protocol file by both the client and the server, and can also realize the client and the service without introducing the grpc interface package.
- the call of the terminal reduces the work cost of personnel and reduces the bugs caused by synchronizing the protocol files.
- a method for the client to dynamically call the server through GRPC including:
- the server configures a preset adaptation jar package, the adaptation jar package includes a java project, a grpc module, a grpc interface, supports GRPC bidirectional flow and upgrades the core class to the springboot-starter mode, and the grpc interface provides the service
- the entry that can be dynamically called by the server under the premise that the terminal code is non-intrusive;
- the client sends a call request to the server, and accepts the response data sent by the server to complete the method that the client dynamically calls the server through GRPC.
- the implementation method of the adaptation jar package includes:
- the core class is made into a switch configuration and introduced into the spring environment, so that the original code is non-invasive and can be used out of the box.
- the method for implementing the adaptation jar package specifically includes the following steps:
- the building a java project framework specifically includes the following steps:
- the fusion GRPC framework specifically includes the following steps:
- the logical development of the adaptation jar package specifically includes the following steps:
- the interface is automatically registered in the GRPC interface registration list.
- the specific process of configuring the preset adaptation jar package by the server is as follows:
- the server side introduces the jar package. If it is a maven project, the pom dependency is introduced, and the core class of the jar package is injected into the spring environment.
- the invention uses the characteristics of java language to create an adaptation jar package, and links the grpc client and server server together without the constraints of the proto file protocol, so that the grpc client You can access any server-side interface. It solves the problem that the grpc client needs to import the specified proto file or the package compiled by the proto file when calling the server, and also solves the problem that the proto file is synchronized between the client and the server respectively.
- the development process is simplified, the workload is reduced, and the project risk is reduced.
- FIG. 1 is a frame diagram of a method for a client to dynamically call a server through GRPC in the present invention.
- the present invention provides a method for a client to dynamically call a server through GRPC, which specifically includes the following steps:
- the server configures a preset adaptation jar package, the adaptation jar package includes a java project, a grpc module, a grpc interface, supports GRPC bidirectional flow and upgrades the core class to the springboot-starter mode, and the grpc interface is Provide an entry that can be dynamically called by the server without being intrusive to the server code;
- the client sends a call request to the server, and accepts the response data sent by the server to complete the method of dynamically calling the server by the client through GRPC.
- Adapting the jar package is the core part of the whole invention
- the purpose of adapting the jar package is to provide an entry that can be dynamically invoked by the server without being intrusive to the server code.
- the specific implementation method of adapting the jar package is: using the java integration framework springboot to build a java project; using spring-ioc and JDK reflection mechanism to implement request routing; using the springboot-starter mode to make the core class into a switch configuration and introduce it into the spring environment to achieve this There is no intrusion to the original code, and it can be used out of the box.
- the server introduces the jar package, if it is a maven project, then the pom dependency is introduced, and the core class of the jar package is injected into the spring environment.
- the development of the adaptation jar package is divided into three steps.
- the first step is to build the java project framework
- the second step is to integrate the GRPC framework
- the third step is to develop the main logic of the adaptation package.
- Step 1 Build the java project framework:
- Springboot can quickly build and start a java project, built-in startup container, and the Springboot framework integrates many common components, such as database jdbc, redis, etc. And after the project is packaged into a jar package, the startup method can be started directly through java-jar, which is convenient and fast.
- Maven is a component for managing java projects. It does not need to download and import the external packages that the project depends on. It provides automatic loading through the configuration file (pom.xml).
- Step 2 Integrate the GRPC framework:
- GRPC is used as a way of remote process interaction between projects, and this article is also a dynamic call based on GRPC, so it is necessary to introduce the created java project into the GRPC framework, and write and implement the code required by GRPC.
- Step 3 The main logic development of the adaptation package:
- the first step is to implement the protocol interface in Section 2.2.
- the second step is to write the main dynamic calling logic in the interface implementation method body.
- the third step is to automatically register the interface in the GRPC interface registration list.
- This step is mainly divided into three points.
- the first point is to parse the method input parameters
- the second point is to route the parsed value to the target class and method
- the third point is to call the target class and method through reflection.
- the method input parameters mainly include the class name of the target (the method actually called by the client), the name of the target method, and the parameters of the target method.
- the second point is to route to the target class and method through the parsed value
- the adaptation jar package can take effect after it is introduced into the server, it means that the adaptation package can obtain the context of the internal environment of the server.
- the context of the internal environment of the java project generally refers to the context of the Spring environment. Then, you can Using the context of the Spring environment to find the target class and method through the parsed value mainly depends on the principle and API of Spring IOC.
- the third point call the target class and method through reflection
- Java Since Java is a statically compiled language, the specified API cannot be used after dynamically capturing the target. Therefore, after obtaining the real target class and method, use the reflection mechanism of Java to call the target class and method to obtain the return value.
- the operation of registering the interface in the server GRPC interface registration list is changed to automatic registration, that is, the main server introduces the adaptation package , when the server starts, the interface is registered in the interface registration list by itself to provide external services.
- the server project introduces the adaptation jar package
- the server project pom.xml file introduces the adaptation jar
- the client project introduces the GRPC protocol file
- the client needs to call the server through the GRPC interface.
- This interface is the protocol interface defined in step 2.2, so the client needs to import the compiled file of the interface protocol.
- the input parameters are the bean name of the target class in the server spring, the target method name, the target method input parameters, and start the request.
- Another data middle-end project is responsible for capturing data from various data sources and transmitting other data warehouses.
- the English name is Pier.
- the pom file in the Pier project is also added to the pom dependency of the adaptation package, as follows:
- Each part tag is a type of request, where name is the name of the part, which is unique; server is the name of the registry in the target service; bean is the name of the class where the target method is located; method is the target method; params is the target method parameter; Pier When the project starts the service, it reads the configuration file, integrates the parameters in the configuration file, and requests the Caller method of the target service (this method is the method in the adaptation package).
- the core method body is as follows:
- the target method in the target server can be accessed normally according to the parameters in the configuration file.
- a client based on the power monitoring network security system architecture dynamically communicates through GRPC.
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Software Systems (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Business, Economics & Management (AREA)
- Databases & Information Systems (AREA)
- General Engineering & Computer Science (AREA)
- Economics (AREA)
- Health & Medical Sciences (AREA)
- Data Mining & Analysis (AREA)
- Public Health (AREA)
- Water Supply & Treatment (AREA)
- General Health & Medical Sciences (AREA)
- Human Resources & Organizations (AREA)
- Marketing (AREA)
- Primary Health Care (AREA)
- Strategic Management (AREA)
- Tourism & Hospitality (AREA)
- General Business, Economics & Management (AREA)
- Stored Programmes (AREA)
Abstract
Disclosed is a method for a client to dynamically call a server by means of a gRPC, comprising: a server configures a preset adaptive JAR package, the adaptive JAR package comprising a Java item, a gRPC module and a gRPC interface, supporting the bidirectional flow of a gRPC and upgrading a core class to a springboot-starter means, and the gRPC interface being an entry which may be dynamically called by the server on the premise that a code of the server is not invasive; and the client sends a call request to the server and receives response data sent by the server, so as to complete the method for a client to dynamically call a server by means of a gRPC. In the present invention, the cost of both a client and a server maintaining a protocol file may be saved, the calling of the client and the server may be achieved while not introducing a gRPC interface package, working costs of personnel may be reduced, and bugs generated by synchronizing the protocol file are reduced.
Description
本发明属于电力监控系统安全防护通信安全技术领域,具体涉及一种客户端通过GRPC动态调用服务端的方法。The invention belongs to the technical field of security protection communication security of a power monitoring system, and in particular relates to a method for a client to dynamically call a server through GRPC.
现如今,在软件生态圈需求的不断催动下,分布式已经成为老生常谈的话题,而在分布式环境中,为了桥接各个服务间的通信,RPC技术应运而生。thrift、grpc、dubbo、dubbox、motan等。Nowadays, driven by the demand of the software ecosystem, distributed has become a common topic. In a distributed environment, in order to bridge the communication between various services, RPC technology came into being. thrift, grpc, dubbo, dubbox, motan, etc.
随着电力监控网络安全系统设计的项目数不断增加,业务需求的不断增加,grpc接口数也在不断增加,这就意味着,grpc协议文件内容也在不断增加,这给维护客户端和服务端项目的人员带来一定的工作成本,grpc协议文件的同步成了每次项目联调关注重点,也由于协议文件的同步产生很多不应该存在的bug。With the continuous increase in the number of projects designed for the power monitoring network security system, the continuous increase in business requirements, and the continuous increase in the number of grpc interfaces, which means that the content of the grpc protocol file is also increasing, which provides maintenance clients and servers. The project personnel bring certain work costs. The synchronization of the grpc protocol file has become the focus of each project joint debugging, and there are many bugs that should not exist due to the synchronization of the protocol file.
现如今,在软件生态圈需求的不断催动下,分布式已经成为老生常谈的话题,而在分布式环境中,为了桥接各个服务间的通信,RPC(Remote Procedure Call)技术应运而生,主要包括thrift、grpc、dubbo、dubbox、motan等。RPC框架中,序列化协议重要的组成部分之一是对传输的二进制数据进行序列化和反序列化。对java这种静态编译的语言来说,带来的好处是完全面向对象开发,通信两端需要事先定义好接口协议,产出文件,将文件编译成java文件,服务端引入文件,针对协议接口提供功能实现。客户端引入该文件,填充指定类型参数,进行调用,从而实现了GRPC调用的整个链路,具体参见图1。Nowadays, driven by the continuous demand of the software ecosystem, distributed has become a common topic. In a distributed environment, in order to bridge the communication between various services, RPC (Remote Procedure Call) technology came into being, mainly including thrift, grpc, dubbo, dubbox, motan, etc. In the RPC framework, one of the important components of the serialization protocol is to serialize and deserialize the transmitted binary data. For a statically compiled language like java, the advantage is that it is completely object-oriented development. Both ends of the communication need to define the interface protocol in advance, output the file, compile the file into a java file, and import the file from the server, aiming at the protocol interface. Provide function implementation. The client introduces this file, fills in the specified type parameters, and makes a call, thereby realizing the entire link of the GRPC call, see Figure 1 for details.
客户端在调用服务端任意接口前,必须要知道接口的定义,也就是协议文件。其面临的问题包括:(1)客户端和服务端同时维护协议文件,文件同步是否及时。(2)客户端要调用服务端时,必须提前知道协议文件,做很多重复工作。Before calling any interface of the server, the client must know the definition of the interface, that is, the protocol file. The problems it faces include: (1) The client and the server maintain the protocol files at the same time, and whether the file synchronization is timely. (2) When the client wants to call the server, it must know the protocol file in advance and do a lot of repetitive work.
发明内容SUMMARY OF THE INVENTION
针对上述问题,本发明提出一种客户端通过GRPC动态调用服务端的方法,能够省却客户端和服务端双方共同维护协议文件的成本,还可实现在不引入grpc接口包的前提下客户端和服务端的调用,实现了降低人员的工作成本,减少由于同步协议文件产生的bug。In view of the above problems, the present invention proposes a method for the client to dynamically call the server through GRPC, which can save the cost of jointly maintaining the protocol file by both the client and the server, and can also realize the client and the service without introducing the grpc interface package. The call of the terminal reduces the work cost of personnel and reduces the bugs caused by synchronizing the protocol files.
为了实现上述技术目的,达到上述技术效果,本发明通过以下技术方案实现:In order to realize the above-mentioned technical purpose and achieve the above-mentioned technical effect, the present invention is realized through the following technical solutions:
一种客户端通过GRPC动态调用服务端的方法,包括:A method for the client to dynamically call the server through GRPC, including:
服务端配置预设的适配jar包,所述适配jar包包括java项目、grpc模块、grpc接口、支持GRPC双向流和将核心类升级到springboot-starter方式,所述grpc接口为提供对服务 端代码无侵入性前提下可被服务端动态调用的入口;The server configures a preset adaptation jar package, the adaptation jar package includes a java project, a grpc module, a grpc interface, supports GRPC bidirectional flow and upgrades the core class to the springboot-starter mode, and the grpc interface provides the service The entry that can be dynamically called by the server under the premise that the terminal code is non-intrusive;
客户端向服务端发送调用请求,并接受服务端发送的响应数据,完成客户端通过GRPC动态调用服务端的方法。The client sends a call request to the server, and accepts the response data sent by the server to complete the method that the client dynamically calls the server through GRPC.
可选地,所述适配jar包的实现方法包括:Optionally, the implementation method of the adaptation jar package includes:
采用java集成框架springboot构建一个java项目;Build a java project using the java integration framework springboot;
采用spring-ioc和JDK反射机制实现请求路由;Use spring-ioc and JDK reflection mechanism to implement request routing;
利用springboot-starter模式将核心类做成开关配置引入spring环境中做到对原有代码无侵入,做到开箱即用。Using the springboot-starter mode, the core class is made into a switch configuration and introduced into the spring environment, so that the original code is non-invasive and can be used out of the box.
可选地,所述适配jar包的实现方法具体包括以下步骤:Optionally, the method for implementing the adaptation jar package specifically includes the following steps:
搭建java项目框架;Build the java project framework;
融合GRPC框架;Integrate GRPC framework;
进行适配jar包的逻辑开发。Carry out the logic development of the adaptation jar package.
可选地,所述搭建java项目框架具体包括以下步骤:Optionally, the building a java project framework specifically includes the following steps:
选定java的jdk开发版本;Select the jdk development version of java;
采用springboot集成框架;Adopt springboot integration framework;
使用Maven方式构建项目和引入第三方依赖包。Use Maven to build projects and introduce third-party dependencies.
可选地,所述融合GRPC框架具体包括以下步骤:Optionally, the fusion GRPC framework specifically includes the following steps:
导入GRPC依赖包;Import GRPC dependencies;
编写GRPC协议文件,该文件里只定义一个Service,且该Service中只提供一个待实现接口。Write a GRPC protocol file. Only one Service is defined in the file, and only one interface to be implemented is provided in the Service.
可选地,所述适配jar包的逻辑开发具体包括以下步骤:Optionally, the logical development of the adaptation jar package specifically includes the following steps:
创建一个java实现类继承所述接口的接口协议类,只有继承了指定的接口协议类,才能在客户端调用协议接口的时候,将请求转发到具体的实现类中;Create a java implementation class that inherits the interface protocol class of the interface. Only by inheriting the specified interface protocol class can the request be forwarded to the specific implementation class when the client calls the protocol interface;
在所述接口的实现方法体中写入动态调用逻辑;Write dynamic calling logic in the implementation method body of the interface;
将所述接口自动注册到GRPC接口注册列表中。The interface is automatically registered in the GRPC interface registration list.
可选地,所述服务端配置预设的适配jar包的具体过程为:Optionally, the specific process of configuring the preset adaptation jar package by the server is as follows:
服务端引入jar包,如果是maven项目,则引入pom依赖,将jar包核心类注入到spring环境中即可。The server side introduces the jar package. If it is a maven project, the pom dependency is introduced, and the core class of the jar package is injected into the spring environment.
与现有技术相比,本发明的有益效果:Compared with the prior art, the beneficial effects of the present invention:
本发明基于电力监控网络安全系统架构,利用java语言的特性,创建一个适配jar包,将grpc的客户端和服务端服务端,在没有proto文件协议的约束下链接在一起,使得grpc 客户端可以访问任何一个服务端的接口。很好的解决了grpc客户端在调用服务端时需要引入指定的proto文件或者proto文件编译后的包,也解决了proto文件分别在客户端和服务端同步的问题。简化了开发流程,减少了工作量,减少了项目风险。Based on the power monitoring network security system architecture, the invention uses the characteristics of java language to create an adaptation jar package, and links the grpc client and server server together without the constraints of the proto file protocol, so that the grpc client You can access any server-side interface. It solves the problem that the grpc client needs to import the specified proto file or the package compiled by the proto file when calling the server, and also solves the problem that the proto file is synchronized between the client and the server respectively. The development process is simplified, the workload is reduced, and the project risk is reduced.
为了使本发明的内容更容易被清楚地理解,下面根据具体实施例并结合附图,对本发明作进一步详细的说明,其中:In order to make the content of the present invention easier to be understood clearly, the present invention will be described in further detail below according to specific embodiments and in conjunction with the accompanying drawings, wherein:
图1为本发明中客户端通过GRPC动态调用服务端的方法的框架图。FIG. 1 is a frame diagram of a method for a client to dynamically call a server through GRPC in the present invention.
为了使本发明的目的、技术方案及优点更加清楚明白,以下结合实施例,对本发明进行进一步详细说明。应当理解,此处所描述的具体实施例仅仅用以解释本发明,并不用于限定本发明的保护范围。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 embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention, and are not used to limit the protection scope of the present invention.
下面结合附图对本发明的应用原理作详细的描述。The application principle of the present invention will be described in detail below with reference to the accompanying drawings.
本发明中提供了一种客户端通过GRPC动态调用服务端的方法,具体包括以下步骤:The present invention provides a method for a client to dynamically call a server through GRPC, which specifically includes the following steps:
(1)服务端配置预设的适配jar包,所述适配jar包包括java项目、grpc模块、grpc接口、支持GRPC双向流和将核心类升级到springboot-starter方式,所述grpc接口为提供对服务端代码无侵入性前提下可被服务端动态调用的入口;(1) The server configures a preset adaptation jar package, the adaptation jar package includes a java project, a grpc module, a grpc interface, supports GRPC bidirectional flow and upgrades the core class to the springboot-starter mode, and the grpc interface is Provide an entry that can be dynamically called by the server without being intrusive to the server code;
(2)客户端向服务端发送调用请求,并接受服务端发送的响应数据,完成客户端通过GRPC动态调用服务端的方法。(2) The client sends a call request to the server, and accepts the response data sent by the server to complete the method of dynamically calling the server by the client through GRPC.
下面结合图1以及一具体的实施过程对本发明中的客户端通过GRPC动态调用服务端的方法进行详细说明。The method for dynamically invoking the server through GRPC by the client in the present invention will be described in detail below with reference to FIG. 1 and a specific implementation process.
1、开发适配jar包1. Develop and adapt jar package
适配jar包为整个发明的核心部分Adapting the jar package is the core part of the whole invention
适配jar包的目的:对服务端代码无侵入性前提下提供可被服务端动态调用调用的入口。The purpose of adapting the jar package is to provide an entry that can be dynamically invoked by the server without being intrusive to the server code.
适配jar包的具体实现方式为:采用java集成框架springboot构建一个java项目;采用spring-ioc和JDK反射机制实现请求路由;利用springboot-starter模式将核心类做成开关配置引入spring环境中做到对原有代码无侵入,做到开箱即用。The specific implementation method of adapting the jar package is: using the java integration framework springboot to build a java project; using spring-ioc and JDK reflection mechanism to implement request routing; using the springboot-starter mode to make the core class into a switch configuration and introduce it into the spring environment to achieve this There is no intrusion to the original code, and it can be used out of the box.
适配jar包的使用方式:服务端引入jar包,如果是maven项目,则引入pom依赖,将jar包核心类注入到spring环境中即可。Adapt the usage of the jar package: the server introduces the jar package, if it is a maven project, then the pom dependency is introduced, and the core class of the jar package is injected into the spring environment.
适配jar包的开发总共分为三步,第一步搭建java项目框架,第二步融合GRPC框架,第三步适配包主要逻辑开发。The development of the adaptation jar package is divided into three steps. The first step is to build the java project framework, the second step is to integrate the GRPC framework, and the third step is to develop the main logic of the adaptation package.
下面是具体实现方式:The following is the specific implementation method:
步骤1:搭建java项目框架:Step 1: Build the java project framework:
1.1基于jdk1.8版本开发1.1 Developed based on jdk1.8 version
选定java的jdk开发版本,这里选择jdk1.8。Select the jdk development version of java, here select jdk1.8.
1.2采用springboot集成框架1.2 Adopt springboot integration framework
Springboot是可以快速构建和启动一个java项目,内置启动容器,并且Springboot框架集成了很多常用的组件,比如数据库jdbc,redis等。且将项目打成jar包后,启动方式可以直接通过java-jar启动,方便、快捷。Springboot can quickly build and start a java project, built-in startup container, and the Springboot framework integrates many common components, such as database jdbc, redis, etc. And after the project is packaged into a jar package, the startup method can be started directly through java-jar, which is convenient and fast.
1.3使用Maven方式构建项目和引入第三方依赖包1.3 Use Maven to build projects and introduce third-party dependencies
Maven是一个管理java项目的一个组件,他不需要将项目依赖的外部包下载并导入项目,他提供通过配置文件(pom.xml)的方式,自动加载。Maven is a component for managing java projects. It does not need to download and import the external packages that the project depends on. It provides automatic loading through the configuration file (pom.xml).
步骤2:融合GRPC框架:Step 2: Integrate the GRPC framework:
目前环境中项目之间通过GRPC作为远程过程交互的方式,且本文也是基于GRPC做的一个动态调用,所以需要将创建好的java项目引入GRPC框架,并且编写和实现GRPC所需的代码。In the current environment, GRPC is used as a way of remote process interaction between projects, and this article is also a dynamic call based on GRPC, so it is necessary to introduce the created java project into the GRPC framework, and write and implement the code required by GRPC.
2.1导入GRPC依赖包2.1 Import GRPC dependencies
在项目的pom.xml中(Maven的配置文件)引入GRPC关于java语言的相关maven依赖grpc-protobuf、protobuf-java-util、grpc-netty、grpc-stub和protobuf-maven-plugin组件,这些依赖支撑了java项目对grpc框架的融合,完成创建.proto协议文件和编译。In the project's pom.xml (Maven's configuration file), introduce the relevant maven dependencies of GRPC on the java language, grpc-protobuf, protobuf-java-util, grpc-netty, grpc-stub and protobuf-maven-plugin components, these dependencies support The java project integrates the grpc framework, and completes the creation and compilation of the .proto protocol file.
2.2编写GRPC协议文件(protobuf文件)2.2 Write GRPC protocol file (protobuf file)
创建grpc协议文件即.proto文件,该文件里只定义一个Service,且该Service中只提供一个待实现接口(该接口为客户端访问服务端的唯一入口),接口名为Caller(Parameters param)returns Object,param作为入参参数,参数中包含待调用的目标类在spring中的bean名,待调用的目标方法名,待调用的目标方法入参。在项目的pom.xml文件中配置grpc协议文件路径,当项目在执行编译动作时,将grpc协议文件编译成java文件并打成jar,供项目引入和代码调用。Create a grpc protocol file, a .proto file. Only one Service is defined in this file, and only one interface to be implemented is provided in the Service (this interface is the only entry for the client to access the server), and the interface name is Caller(Parameters param) returns Object , param is used as the input parameter. The parameter contains the bean name of the target class to be called in spring, the name of the target method to be called, and the input parameter of the target method to be called. Configure the grpc protocol file path in the project's pom.xml file. When the project is executing the compilation action, the grpc protocol file is compiled into a java file and marked into a jar for project introduction and code invocation.
步骤3适配包主要逻辑开发:Step 3 The main logic development of the adaptation package:
该步主要分为三小步,第一步实现2.2节中的协议接口,第二步在接口实现方法体中写主要动态调用逻辑,第三步将该接口自动注册到GRPC接口注册列表中。This step is mainly divided into three small steps. The first step is to implement the protocol interface in Section 2.2. The second step is to write the main dynamic calling logic in the interface implementation method body. The third step is to automatically register the interface in the GRPC interface registration list.
3.1实现步骤2.2中的协议接口3.1 Implement the protocol interface in step 2.2
创建一个java实现类继承2.2节中接口协议编译后生成的接口协议类。只有继承了指 定的接口协议类,才能在客户端调用协议接口的时候,将请求转发到具体的实现类中。Create a java implementation class to inherit the interface protocol class generated after compiling the interface protocol in Section 2.2. Only if the specified interface protocol class is inherited, the client can forward the request to the specific implementation class when the client calls the protocol interface.
3.2在接口实现方法体中写主要动态调用逻辑3.2 Write the main dynamic calling logic in the interface implementation method body
该步主要分为三点,第一点解析方法入参,第二点通过解析出来的值路由到目标类和方法,第三点通过反射调用目标类和方法。This step is mainly divided into three points. The first point is to parse the method input parameters, the second point is to route the parsed value to the target class and method, and the third point is to call the target class and method through reflection.
第一点、解析方法入参The first point, parsing method input parameters
方法入参中,主要包含了目标(客户端实际要调用的方法)类名,目标方法名,目标方法参数。The method input parameters mainly include the class name of the target (the method actually called by the client), the name of the target method, and the parameters of the target method.
第二点、通过解析出来的值路由到目标类和方法The second point is to route to the target class and method through the parsed value
由于该适配jar包引入服务端后才可生效,也就代表该适配包可以获取到服务端内部环境的上下文,java项目内部环境的上下文,一般是指Spring环境的上下文,那么,就可以利用Spring环境的上下文来通过解析出来的值找到目标的类和方法,主要依赖Spring IOC的原理和API。Since the adaptation jar package can take effect after it is introduced into the server, it means that the adaptation package can obtain the context of the internal environment of the server. The context of the internal environment of the java project generally refers to the context of the Spring environment. Then, you can Using the context of the Spring environment to find the target class and method through the parsed value mainly depends on the principle and API of Spring IOC.
第三点、通过反射调用目标类和方法The third point, call the target class and method through reflection
由于Java是静态编译的语言,在动态捕捉目标后无法使用指定的API,所以在获取到真实的目标类和方法后,利用java的反射机制,调用目标类和方法,获取返回值。Since Java is a statically compiled language, the specified API cannot be used after dynamically capturing the target. Therefore, after obtaining the real target class and method, use the reflection mechanism of Java to call the target class and method to obtain the return value.
3.3将该接口自动注册到GRPC接口注册列表中3.3 Automatically register the interface to the GRPC interface registration list
为了简化服务端引入适配包后的操作和降低对服务端代码的侵入性,将该接口注册到服务端GRPC接口注册列表中的操作修改为自动注册,也就是主要服务端引入该适配包,在服务端启动时,自定将该接口注册到接口注册列表中,对外提供服务。In order to simplify the operation of the server after introducing the adaptation package and reduce the intrusiveness to the server code, the operation of registering the interface in the server GRPC interface registration list is changed to automatic registration, that is, the main server introduces the adaptation package , when the server starts, the interface is registered in the interface registration list by itself to provide external services.
该步原理是通过Springboot框架提供的spring-boot-autoconfigure包提供的方式实现的。The principle of this step is implemented by means of the spring-boot-autoconfigure package provided by the Springboot framework.
2、服务端项目引入适配jar包2. The server project introduces the adaptation jar package
以Maven管理的java项目为例Take the java project managed by Maven as an example
服务端项目pom.xml文件引入该适配jarThe server project pom.xml file introduces the adaptation jar
3、客户端项目引入GRPC协议文件3. The client project introduces the GRPC protocol file
客户端需要通过GRPC接口调用服务端,这个接口就是步骤2.2定义的协议接口,所以客户端需要引入该接口协议编译后的文件The client needs to call the server through the GRPC interface. This interface is the protocol interface defined in step 2.2, so the client needs to import the compiled file of the interface protocol.
4、客户端请求服务端4. The client requests the server
获取服务端地址,构建GRPC客户端,调用步骤2.2中定义的协议接口,入参为目标类在服务端spring中的bean名称,目标方法名,目标方法入参,开始请求。Get the server address, build the GRPC client, call the protocol interface defined in step 2.2, the input parameters are the bean name of the target class in the server spring, the target method name, the target method input parameters, and start the request.
应用案例Applications
下面结合正在线上运行的项目之一DataCenter,是一个数据查询和转换的业务项目, 其中包含几百个业务接口,先将DataCenter的pom文件中加入适配包的pom依赖如下:The following is combined with DataCenter, one of the projects running online, which is a data query and conversion business project, which contains hundreds of business interfaces. First, add the pom dependencies of the adaptation package to the pom file of DataCenter as follows:
另一个数据中台项目,负责抓取各种数据源中的数据传输其他数据仓库中,英文名Pier,现将Pier项目中的pom文件也加入适配包的pom依赖,如下:Another data middle-end project is responsible for capturing data from various data sources and transmitting other data warehouses. The English name is Pier. Now the pom file in the Pier project is also added to the pom dependency of the adaptation package, as follows:
Pier项目中配置我要请求的目标服务和方法,本次采用配置文件的方式,如下:Configure the target service and method I want to request in the Pier project. This time, the configuration file is used, as follows:
每一个part标签为一类请求,其中name为part的名称,唯一;server为目标服务中注册中心的名称;bean为目标方法所在的类的名称;method为目标方法;params为目标方法参数;Pier项目启动服务时读取配置文件,将配置文件中的参数整合,请求目标服务的Caller方法(该方法为适配包中的方法)。核心方法体如下:Each part tag is a type of request, where name is the name of the part, which is unique; server is the name of the registry in the target service; bean is the name of the class where the target method is located; method is the target method; params is the target method parameter; Pier When the project starts the service, it reads the configuration file, integrates the parameters in the configuration file, and requests the Caller method of the target service (this method is the method in the adaptation package). The core method body is as follows:
经过测试,可根据配置文件中的参数,正常访问目标服务器中的目标方法。After testing, the target method in the target server can be accessed normally according to the parameters in the configuration file.
到此一种基于电力监控网络安全系统架构下客户端通过GRPC动态At this point, a client based on the power monitoring network security system architecture dynamically communicates through GRPC.
调用服务端的技术已经完成。The technology of calling the server has been completed.
以上显示和描述了本发明的基本原理和主要特征和本发明的优点。本行业的技术人员应该了解,本发明不受上述实施例的限制,上述实施例和说明书中描述的只是说明本发明的原理,在不脱离本发明精神和范围的前提下,本发明还会有各种变化和改进,这些变化和改进都落入要求保护的本发明范围内。本发明要求保护范围由所附的权利要求书及其等效物界定。The basic principles and main features of the present invention and the advantages of the present invention have been shown and described above. Those skilled in the art should understand that the present invention is not limited by the above-mentioned embodiments, and the descriptions in the above-mentioned embodiments and the description are only to illustrate the principle of the present invention. Without departing from the spirit and scope of the present invention, the present invention will have Various changes and modifications fall within the scope of the claimed invention. The claimed scope of the present invention is defined by the appended claims and their equivalents.
Claims (7)
- 一种客户端通过GRPC动态调用服务端的方法,其特征在于,包括:A method for a client to dynamically call a server through GRPC, comprising:服务端配置预设的适配jar包,所述适配jar包包括java项目、grpc模块、grpc接口、支持GRPC双向流和将核心类升级到springboot-starter方式,所述grpc接口为提供对服务端代码无侵入性前提下可被服务端动态调用的入口;The server configures a preset adaptation jar package, the adaptation jar package includes a java project, a grpc module, a grpc interface, supports GRPC bidirectional flow and upgrades the core class to the springboot-starter mode, and the grpc interface provides the service The entry that can be dynamically called by the server under the premise that the terminal code is non-intrusive;客户端向服务端发送调用请求,并接受服务端发送的响应数据,完成客户端通过GRPC动态调用服务端的方法。The client sends a call request to the server, and accepts the response data sent by the server to complete the method that the client dynamically calls the server through GRPC.
- 根据权利要求1所述的一种客户端通过GRPC动态调用服务端的方法,其特征在于:The method for dynamically invoking a server through GRPC by a client according to claim 1, is characterized in that:所述适配jar包的实现方法包括:The implementation method of the adaptation jar package includes:采用java集成框架springboot构建一个java项目;Build a java project using the java integration framework springboot;采用spring-ioc和JDK反射机制实现请求路由;Use spring-ioc and JDK reflection mechanism to implement request routing;利用springboot-starter模式将核心类做成开关配置引入spring环境中做到对原有代码无侵入,做到开箱即用。Using the springboot-starter mode, the core class is made into a switch configuration and introduced into the spring environment, so that the original code is non-invasive and can be used out of the box.
- 根据权利要求2所述的一种户端通过GRPC动态调用服务端的方法,其特征在于:所述适配jar包的实现方法具体包括以下步骤:The method for dynamically invoking a server through GRPC by a client according to claim 2, wherein the method for implementing the adaptation jar package specifically comprises the following steps:搭建java项目框架;Build the java project framework;融合GRPC框架;Integrate GRPC framework;进行适配jar包的逻辑开发。Carry out the logic development of the adaptation jar package.
- 根据权利要求2所述的一种户端通过GRPC动态调用服务端的方法,其特征在于,所述搭建java项目框架具体包括以下步骤:The method for dynamically invoking a server by a client through GRPC according to claim 2, wherein the building a java project framework specifically includes the following steps:选定java的jdk开发版本;Select the jdk development version of java;采用springboot集成框架;Adopt springboot integration framework;使用Maven方式构建项目和引入第三方依赖包。Use Maven to build projects and introduce third-party dependencies.
- 根据权利要求2所述的一种户端通过GRPC动态调用服务端的方法,其特征在于,所述融合GRPC框架具体包括以下步骤:The method for dynamically invoking a server through GRPC by a client according to claim 2, wherein the integrated GRPC framework specifically comprises the following steps:导入GRPC依赖包;Import GRPC dependencies;编写GRPC协议文件,该文件里只定义一个Service,且该Service中只提供一个待实现接口。Write a GRPC protocol file. Only one Service is defined in the file, and only one interface to be implemented is provided in the Service.
- 根据权利要求5所述的一种户端通过GRPC动态调用服务端的方法,其特征在于,所述适配jar包的逻辑开发具体包括以下步骤:The method for dynamically invoking a server through GRPC by a client according to claim 5, wherein the logical development of the adaptation jar package specifically includes the following steps:创建一个java实现类继承所述接口的接口协议类,只有继承了指定的接口协议类,才能在客户端调用协议接口的时候,将请求转发到具体的实现类中;Create a java implementation class that inherits the interface protocol class of the interface. Only by inheriting the specified interface protocol class can the request be forwarded to the specific implementation class when the client calls the protocol interface;在所述接口的实现方法体中写入动态调用逻辑;Write dynamic calling logic in the implementation method body of the interface;将所述接口自动注册到GRPC接口注册列表中。The interface is automatically registered in the GRPC interface registration list.
- 根据权利要求1所述的一种户端通过GRPC动态调用服务端的方法,其特征在于,所述服务端配置预设的适配jar包的具体过程为:The method for dynamically invoking a server by a client through GRPC according to claim 1, wherein the specific process of configuring a preset adaptation jar package on the server is as follows:服务端引入jar包,如果是maven项目,则引入pom依赖,将jar包核心类注入到spring环境中即可。The server side introduces the jar package. If it is a maven project, the pom dependency is introduced, and the core class of the jar package is injected into the spring environment.
Applications Claiming Priority (2)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202110423244.0 | 2021-04-20 | ||
CN202110423244.0A CN113515319B (en) | 2021-04-20 | 2021-04-20 | Method for dynamically calling server by client through GRPC |
Publications (1)
Publication Number | Publication Date |
---|---|
WO2022222442A1 true WO2022222442A1 (en) | 2022-10-27 |
Family
ID=78061530
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
PCT/CN2021/129976 WO2022222442A1 (en) | 2021-04-20 | 2021-11-11 | Method for client to dynamically call server by means of grpc |
Country Status (2)
Country | Link |
---|---|
CN (1) | CN113515319B (en) |
WO (1) | WO2022222442A1 (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115878125A (en) * | 2022-11-11 | 2023-03-31 | 盐城金堤科技有限公司 | Generation class extension method and device, storage medium and electronic equipment |
Families Citing this family (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113515319B (en) * | 2021-04-20 | 2023-08-08 | 北京科东电力控制系统有限责任公司 | Method for dynamically calling server by client through GRPC |
CN114301970B (en) * | 2021-12-21 | 2023-10-17 | 北京达佳互联信息技术有限公司 | Service calling method, device, electronic equipment and storage medium |
Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN104009994A (en) * | 2014-05-30 | 2014-08-27 | 北京京东尚科信息技术有限公司 | Method and device for achieving communication of server side and client side |
CN108337236A (en) * | 2017-12-28 | 2018-07-27 | 杭州马猴烧韭科技有限公司 | A kind of gRPC call methods and device based on Protobuf and HTTP/1.1 |
CN109739655A (en) * | 2018-11-21 | 2019-05-10 | 北京奇安信科技有限公司 | A kind of parameter setting method and device of gRPC request |
CN110944018A (en) * | 2019-12-27 | 2020-03-31 | 浪潮通用软件有限公司 | Rpc communication method, device and medium based on annotation and dynamic proxy |
CN113515319A (en) * | 2021-04-20 | 2021-10-19 | 北京科东电力控制系统有限责任公司 | Method for client to dynamically call server through GRPC |
Family Cites Families (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US10698791B2 (en) * | 2018-07-28 | 2020-06-30 | International Business Machines Corporation | Handling request data with type safety in a remote service |
CN109597693A (en) * | 2018-09-25 | 2019-04-09 | 国网浙江省电力有限公司信息通信分公司 | The application method of GRPC communication protocol in distributed software system |
CN110262912B (en) * | 2019-06-26 | 2023-12-22 | 深圳前海微众银行股份有限公司 | Calling method and device for procedure call gRPC |
US20210019400A1 (en) * | 2019-07-15 | 2021-01-21 | SecureNative Ltd. | Security infrastructure as a service |
CN111930541A (en) * | 2020-08-17 | 2020-11-13 | 湖南快乐阳光互动娱乐传媒有限公司 | Dynamic calling method and system for GRPC (Global positioning System) service |
-
2021
- 2021-04-20 CN CN202110423244.0A patent/CN113515319B/en active Active
- 2021-11-11 WO PCT/CN2021/129976 patent/WO2022222442A1/en unknown
Patent Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN104009994A (en) * | 2014-05-30 | 2014-08-27 | 北京京东尚科信息技术有限公司 | Method and device for achieving communication of server side and client side |
CN108337236A (en) * | 2017-12-28 | 2018-07-27 | 杭州马猴烧韭科技有限公司 | A kind of gRPC call methods and device based on Protobuf and HTTP/1.1 |
CN109739655A (en) * | 2018-11-21 | 2019-05-10 | 北京奇安信科技有限公司 | A kind of parameter setting method and device of gRPC request |
CN110944018A (en) * | 2019-12-27 | 2020-03-31 | 浪潮通用软件有限公司 | Rpc communication method, device and medium based on annotation and dynamic proxy |
CN113515319A (en) * | 2021-04-20 | 2021-10-19 | 北京科东电力控制系统有限责任公司 | Method for client to dynamically call server through GRPC |
Non-Patent Citations (1)
Title |
---|
ANONYMOUS: "SpringBoot Integrating Grpc for Cross-language RPC Communication", 28 May 2020 (2020-05-28), XP055979646, Retrieved from the Internet <URL:https://blog.csdn.net/21aspnet/article/details/100727562> [retrieved on 20221109] * |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115878125A (en) * | 2022-11-11 | 2023-03-31 | 盐城金堤科技有限公司 | Generation class extension method and device, storage medium and electronic equipment |
CN115878125B (en) * | 2022-11-11 | 2024-02-06 | 盐城天眼察微科技有限公司 | Expansion method and device of generation class, storage medium and electronic equipment |
Also Published As
Publication number | Publication date |
---|---|
CN113515319A (en) | 2021-10-19 |
CN113515319B (en) | 2023-08-08 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
WO2022222442A1 (en) | Method for client to dynamically call server by means of grpc | |
US7350184B2 (en) | System and method for enterprise application interactions | |
US7472349B1 (en) | Dynamic services infrastructure for allowing programmatic access to internet and other resources | |
US9256353B2 (en) | Providing application and device management using entitlements | |
Abiteboul et al. | The Active XML project: an overview | |
US8788569B2 (en) | Server computer system running versions of an application simultaneously | |
US8984534B2 (en) | Interfacing between a receiving component of a server application and a remote application | |
US9323647B2 (en) | Request-based activation of debugging and tracing | |
US8095823B2 (en) | Server computer component | |
CA2471855C (en) | Java application framework for use in a content delivery network (cdn) | |
KR100783679B1 (en) | A Middleware System Facilitating Development, Deployment, and Provision of Stream-based Services | |
US20060271911A1 (en) | Generating executable objects implementing methods for an information model | |
US20060029054A1 (en) | System and method for modeling and dynamically deploying services into a distributed networking architecture | |
US20090172636A1 (en) | Interactive development tool and debugger for web services | |
US20080282222A1 (en) | Xml push and remote execution of a wireless applications | |
US7562369B1 (en) | Method and system for dynamic configuration of activators in a client-server environment | |
Sharma | Modern API Development with Spring and Spring Boot: Design highly scalable and maintainable APIs with REST, gRPC, GraphQL, and the reactive paradigm | |
US20090172692A1 (en) | Enterprise Resource Planning with Asynchronous Notifications of Background Processing Events | |
KR20210144045A (en) | System and method for executing application based on microservice architecture and computer program for the same | |
JP2006510955A (en) | Context-independent framework system and method for managing and executing XML processing tasks | |
WO2002025440A2 (en) | Event bus architecture | |
Wetherbee et al. | Beginning EJB 3: Java EE 7 Edition | |
US20240176593A1 (en) | System and method for API driven rapid application development and execution | |
Behrends et al. | A component-oriented model for the design of safe multi-threaded applications | |
Saeed et al. | REST with Jakarta EE REST API |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
121 | Ep: the epo has been informed by wipo that ep was designated in this application |
Ref document number: 21937664 Country of ref document: EP Kind code of ref document: A1 |
|
NENP | Non-entry into the national phase |
Ref country code: DE |
|
32PN | Ep: public notification in the ep bulletin as address of the adressee cannot be established |
Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205 DATED 16/02/2024) |