Movatterモバイル変換


[0]ホーム

URL:


CN114296985A - Global exception handling method and platform in large-scale microservice cluster scenarios - Google Patents

Global exception handling method and platform in large-scale microservice cluster scenarios
Download PDF

Info

Publication number
CN114296985A
CN114296985ACN202111653425.9ACN202111653425ACN114296985ACN 114296985 ACN114296985 ACN 114296985ACN 202111653425 ACN202111653425 ACN 202111653425ACN 114296985 ACN114296985 ACN 114296985A
Authority
CN
China
Prior art keywords
exception information
exception
information
microservice
splicing
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.)
Pending
Application number
CN202111653425.9A
Other languages
Chinese (zh)
Inventor
石鸿伟
史精文
黄韬
刘韵洁
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Network Communication and Security Zijinshan Laboratory
Original Assignee
Network Communication and Security Zijinshan Laboratory
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Network Communication and Security Zijinshan LaboratoryfiledCriticalNetwork Communication and Security Zijinshan Laboratory
Priority to CN202111653425.9ApriorityCriticalpatent/CN114296985A/en
Publication of CN114296985ApublicationCriticalpatent/CN114296985A/en
Priority to PCT/CN2022/143273prioritypatent/WO2023125773A1/en
Pendinglegal-statusCriticalCurrent

Links

Images

Classifications

Landscapes

Abstract

Translated fromChinese

本发明涉及计算机微服务应用领域,公开了一种大规模微服务集群场景下的全局异常处理方法和平台,方法包括步骤:获取来自微服务集群的所有原始异常信息;对微服务异常信息进行处理得到第一拼接异常信息,对公共组件异常信息进行处理,得到第二拼接异常信息;将得到的第一拼接异常信息和第二拼接异常信息发送至Spring框架切面上,进行异常封装处理,得到对应的第一封装异常信息和第二封装异常信息;返回第一封装异常信息和第二封装异常信息给对应的原始异常信息的发送方。本发明能够解决现有系统中对异常的处理难以统一管理的问题,以更少的资源消耗,实现了全局异常集中存储、快速查询、统一处理等功能。

Figure 202111653425

The invention relates to the field of computer micro-service applications, and discloses a global exception processing method and platform in a large-scale micro-service cluster scenario. The method includes the steps of: acquiring all original exception information from the micro-service cluster; processing the micro-service exception information Obtain the first splicing exception information, process the common component exception information, and obtain the second splicing exception information; send the obtained first splicing exception information and the second splicing exception information to the Spring framework section, perform exception encapsulation processing, and obtain the corresponding The first packaged exception information and the second packaged exception information are returned; the first packaged exception information and the second packaged exception information are returned to the sender of the corresponding original exception information. The invention can solve the problem that the abnormal processing in the existing system is difficult to manage uniformly, and realizes the functions of global abnormal centralized storage, fast query, unified processing and the like with less resource consumption.

Figure 202111653425

Description

Translated fromChinese
大规模微服务集群场景下的全局异常处理方法和平台Global exception handling method and platform in large-scale microservice cluster scenario

技术领域technical field

本发明涉及计算机微服务应用领域,尤其涉及一种大规模微服务集群场景下的全局异常处理方法和平台。The invention relates to the field of computer micro-service applications, in particular to a global exception processing method and platform in a large-scale micro-service cluster scenario.

背景技术Background technique

微服务是一种架构设计方式,每个微服务被独立部署,仅关注于完成一件任务。集群模式是通过在不同服务器上部署同一套服务对外访问,实现服务的负载均衡。目前很多大型复杂应用系统将业务拆分成多个子业务,然后针对每个子业务进行集群部署,如果某个子业务出现问题,整个系统仍能保持正常动作。每个子业务可以由一个或多个微服务组成,每个微服务通常包括向外部客户端提供的面向外部的服务接口,用于向客户端提供用户登录、注册等服务;还包括向其它微服务提供面向内部的服务接口,用于相互之间的服务调用,实现一个完整的业务流程。Microservices are an architectural design where each microservice is deployed independently and only focuses on accomplishing one task. Cluster mode is to deploy the same set of services on different servers for external access to achieve load balancing of services. At present, many large and complex application systems divide the business into multiple sub-businesses, and then deploy clusters for each sub-business. If a problem occurs in a sub-business, the entire system can still maintain normal operation. Each sub-business can be composed of one or more microservices. Each microservice usually includes an external-facing service interface provided to external clients, which is used to provide clients with services such as user login and registration; it also includes other microservices. Provide internal-oriented service interfaces for mutual service calls to implement a complete business process.

在实际应用中,无论是作为服务端向外部客户端提供微服务,还是微服务之间的彼此调用,都会不可避免地出现异常情况,需要及时捕获和处理。异常分为检查型异常和非检查型异常,各种异常出现时,系统的业务代码都需要做处理,防止直接抛出给用户。检查型异常需要在产生异常的代码方法上做处理,关于检查型异常的处理可能分布在系统的各个位置;非检查型异常的处理可以在方法服务调用的最上层进行处理。由于一个系统中有众多方法服务,所以对非检查型异常的处理也可能在各个方法服务的调用处都有。因此,针对异常的处理可能分布在系统中的各个位置的问题,难以统一管理。In practical applications, whether as a server to provide microservices to external clients, or to call each other between microservices, exceptions will inevitably occur, which need to be captured and handled in time. Exceptions are divided into checked exceptions and unchecked exceptions. When various exceptions occur, the business code of the system needs to be processed to prevent them from being directly thrown to the user. Checked exceptions need to be handled on the code method that generates the exception, and the handling of checked exceptions may be distributed in various parts of the system; the handling of unchecked exceptions can be handled at the top layer of method service calls. Since there are many method services in a system, the handling of unchecked exceptions may also exist at the invocation of each method service. Therefore, it is difficult to uniformly manage the problem that the processing of exceptions may be distributed in various locations in the system.

业界普遍将业务系统结合Spring框架的切面特征去做全局的异常处理,Spring框架是一种微服务框架,切面由切点和增强组成,既包括了横切逻辑的定义,也包括了连接点的定义。Spring框架定义的异常处理方法,面向的是用特定方法注解修饰的类,能够对特定的异常进行业务处理。该方法能够完成单服务内部的全局异常处理,但是难以做到异常处理业务高内聚在一起,因此不能很好地适用于微服务集群这种多服务的场景。此外,异常信息中可能有部分字段需要做替换,在不同的场景下抛出的内容不同,大规模微服务集群场景下的异常处理,需要做到统一处理的同时,又要求系统尽量少关注异常的处理,使两者平衡。最后,各个微服务都可能会使用数据库等公共组件,对公共组件的异常处理如果放到各个微服务中就会有冗余代码。The industry generally combines the business system with the aspect features of the Spring framework for global exception handling. The Spring framework is a microservice framework. The aspect is composed of pointcuts and enhancements, including not only the definition of cross-cutting logic, but also the definition of connection points. definition. The exception handling method defined by the Spring framework is oriented to classes that are annotated and decorated with specific methods, and can perform business processing on specific exceptions. This method can complete global exception handling within a single service, but it is difficult to achieve high cohesion of exception handling services, so it is not well suited for multi-service scenarios such as microservice clusters. In addition, some fields in the exception information may need to be replaced, and the content thrown in different scenarios is different. The exception handling in the large-scale microservice cluster scenario needs to be handled uniformly, and at the same time, the system is required to pay as little attention to exceptions as possible. processing to balance the two. Finally, each microservice may use common components such as databases, and if the exception handling of common components is placed in each microservice, there will be redundant code.

发明内容SUMMARY OF THE INVENTION

技术目的:针对上述技术问题,本发明公开了一种大规模微服务集群场景下的全局异常处理方法和平台,能够在大规模微服务集群场景下,对全局异常进行集中存储、快速查询、统一处理。Technical purpose: In view of the above technical problems, the present invention discloses a global exception processing method and platform in a large-scale micro-service cluster scenario, which can centrally store, quickly query, and unify global exceptions in a large-scale micro-service cluster scenario. deal with.

技术方案:为实现上述技术目的,本发明采用了如下技术方案:Technical scheme: In order to realize the above-mentioned technical purpose, the present invention adopts the following technical scheme:

一种大规模微服务集群场景下的全局异常处理方法,其特征在于,包括步骤:A global exception handling method in a large-scale microservice cluster scenario, characterized by comprising the steps of:

获取来自微服务集群的所有原始异常信息;Get all raw exception information from the microservice cluster;

根据信息格式的区别,将获取的原始异常信息分类为微服务异常信息和公共组件异常信息,对微服务异常信息进行处理得到第一拼接异常信息,对公共组件异常信息进行拼接处理,得到第二拼接异常信息;According to the difference in information format, the obtained original exception information is classified into microservice exception information and common component exception information, the microservice exception information is processed to obtain the first spliced exception information, and the common component exception information is spliced to obtain the second exception information. splicing exception information;

将所述第一拼接异常信息和第二拼接异常信息发送至Spring框架切面上,进行异常封装处理,得到对应的第一封装异常信息和第二封装异常信息;Sending the first splicing exception information and the second splicing exception information to the Spring framework section, and performing exception encapsulation processing to obtain corresponding first encapsulation exception information and second encapsulation exception information;

返回第一封装异常信息和第二封装异常信息给对应的原始异常信息的发送方。Return the first encapsulated exception information and the second encapsulated exception information to the sender of the corresponding original exception information.

优选地,将获取的原始异常信息分类为微服务异常信息和公共组件异常信息,包括步骤:Preferably, classifying the acquired original exception information into microservice exception information and common component exception information, including steps:

判断原始异常信息是否以包括微服务名称、异常码、带有第一变量的异常消息、第二变量的格式表示,若是,则将此原始异常信息归类于微服务异常信息,否则将所述原始异常信息归类于公共组件异常信息。Determine whether the original exception information is represented in a format including the name of the microservice, the exception code, the exception message with the first variable, and the second variable. If so, classify the original exception information into the microservice exception information, otherwise, classify the The raw exception information is classified under the common component exception information.

优选地,对微服务异常信息进行处理得到第一拼接异常信息,对公共组件异常信息进行拼接处理,得到第二拼接异常信息,包括步骤:Preferably, the microservice exception information is processed to obtain the first splicing exception information, and the common component exception information is spliced to obtain the second splicing exception information, including the steps:

对于微服务异常信息,根据微服务名称,判断发生异常的微服务来源,将微服务异常信息中的第一变量替换为第二变量,得到第一拼接异常信息;For the microservice exception information, according to the name of the microservice, determine the source of the abnormal microservice, replace the first variable in the microservice exception information with the second variable, and obtain the first splicing exception information;

对于公共组件异常信息,将公共组件异常信息按照预定义格式进行拼接,得到第二拼接异常信息。For the common component exception information, the common component exception information is spliced according to a predefined format to obtain the second spliced exception information.

优选地,所述原始异常信息产生于微服务集群中的任一微服务在服务调用发生错误时,由被调用的微服务发出;或者产生于公共组件发生异常时,由访问和发现公共组件发生异常的微服务发出。Preferably, the original exception information is generated by any microservice in the microservice cluster when an error occurs in the service call, and is sent by the called microservice; or when an exception occurs in a public component, it is generated by accessing and discovering the public component. Exception issued by the microservice.

优选地,所述原始异常信息、第一拼接异常信息和第二拼接异常信息以Map的形式存储,key为异常码、value为异常信息的内容,异常码定义为全局静态常量。Preferably, the original exception information, the first splicing exception information and the second splicing exception information are stored in the form of a Map, the key is the exception code, the value is the content of the exception information, and the exception code is defined as a global static constant.

一种全局异常处理平台,其特征在于,包括:A global exception handling platform, characterized in that it includes:

detector模块,用于获取来自微服务集群的所有原始异常信息,并根据信息格式的区别,将获取的原始异常信息分类为微服务异常信息和公共组件异常信息;然后对微服务异常信息进行处理得到第一拼接异常信息,对公共组件异常信息进行处理,得到第二拼接异常信息;The detector module is used to obtain all the original exception information from the microservice cluster, and classify the obtained original exception information into microservice exception information and common component exception information according to the difference in the information format; and then process the microservice exception information to obtain The first splicing exception information, processing the common component exception information, to obtain the second splicing exception information;

封装模块,用于将所述第一拼接异常信息和第二拼接异常信息发送至Spring框架切面上,进行异常封装处理,得到对应的第一封装异常信息和第二封装异常信息;An encapsulation module, configured to send the first splicing exception information and the second splicing exception information to the Spring framework aspect, and perform an exception encapsulation process to obtain corresponding first encapsulation exception information and second encapsulation exception information;

反馈模块,用于返回第一封装异常信息和第二封装异常信息给对应的原始异常信息的发送方。The feedback module is configured to return the first packaged exception information and the second packaged exception information to the sender of the corresponding original exception information.

优选地,还包括service模块,用于接收异常信息查询请求并进行异常信息查询。Preferably, a service module is also included, which is used for receiving an abnormal information query request and performing abnormal information query.

优选地,还包括存储模块,用于存储所有原始的异常信息、第一拼接异常信息和第二拼接异常信息。Preferably, a storage module is also included for storing all the original abnormal information, the first splicing anomaly information and the second splicing anomaly information.

一种介质,存储有计算机可执行指令,其特征在于:所述指令在被处理单元执行时用于实现所述方法。A medium storing computer-executable instructions, wherein the instructions are used to implement the method when executed by a processing unit.

一种电子设备,其特征在于:包括处理单元和存储单元,存储有计算机可执行指令,所述指令在被所述处理单元执行时用于实现所述方法。An electronic device is characterized by comprising a processing unit and a storage unit, storing computer-executable instructions, and the instructions are used to implement the method when executed by the processing unit.

有益效果:与现有技术相比,本发明具有如下技术效果:Beneficial effects: compared with the prior art, the present invention has the following technical effects:

本发明采用的方案能够解决大规模微服务集群场景下的全局异常处理难以统一管理的问题,通过异常处理平台进行异常信息的统一处理,结合spring框架的切面能力,以更少的资源消耗,实现了全局异常集中存储、快速查询、统一处理等功能,最大可能的减少系统中的异常信息处理量。The solution adopted by the present invention can solve the problem that the global exception processing in the large-scale micro-service cluster scenario is difficult to manage in a unified manner. The exception processing platform is used to perform unified processing of exception information, combined with the aspect capability of the spring framework, to achieve the realization of less resource consumption. It has functions such as centralized storage of global exceptions, fast query, and unified processing, which minimizes the amount of exception information processing in the system.

附图说明Description of drawings

图1为本发明实施例中的一种全局异常处理方法的流程图;1 is a flowchart of a global exception handling method in an embodiment of the present invention;

图2是本发明实施例中的整个微服务群在互联网中的结构示意图;2 is a schematic structural diagram of the entire microservice group in the Internet in an embodiment of the present invention;

图3是本发明实施例中的全局异常处理平台的结构示意图。FIG. 3 is a schematic structural diagram of a global exception processing platform in an embodiment of the present invention.

具体实施方式Detailed ways

实施例1Example 1

本实施例提供一种大规模微服务集群场景下的全局异常处理方法,包括步骤:This embodiment provides a global exception handling method in a large-scale microservice cluster scenario, including the steps:

获取来自微服务集群的所有原始异常信息;Get all raw exception information from the microservice cluster;

根据信息格式的区别,将获取的原始异常信息分类为微服务异常信息和公共组件异常信息,对微服务异常信息进行处理得到第一拼接异常信息,对公共组件异常信息进行处理,得到第二拼接异常信息;According to the difference in information format, the obtained original exception information is classified into microservice exception information and common component exception information, and the microservice exception information is processed to obtain the first splicing exception information, and the common component exception information is processed to obtain the second splicing. abnormal information;

将得到的第一拼接异常信息和第二拼接异常信息发送至Spring框架切面上,进行异常封装处理,得到对应的第一封装异常信息和第二封装异常信息;Sending the obtained first splicing exception information and second splicing exception information to the Spring framework aspect, and performing exception encapsulation processing to obtain corresponding first encapsulation exception information and second encapsulation exception information;

返回第一封装异常信息和第二封装异常信息给对应的原始异常信息的发送方;Return the first packaged exception information and the second packaged exception information to the sender of the corresponding original exception information;

其中,原始异常信息产生于微服务集群中的任一微服务在服务调用发生错误时,由被调用的微服务发出;或者产生于公共组件发生异常时,由访问和发现公共组件发生异常的微服务发出。Among them, the original exception information is generated by any microservice in the microservice cluster when an error occurs in the service call, and is sent by the called microservice; or when an exception occurs in a public component, it is generated by the microservice that accesses and discovers the abnormality of the public component. service issued.

具体地,根据信息格式的区别,将获取的原始异常信息分类时,包括步骤:Specifically, according to the difference in the information format, when classifying the acquired original abnormal information, the steps include:

分析接收到的原始异常信息的格式,如果原始异常信息以包括微服务ID、异常码ID、带有第一变量的异常消息、第二变量的格式表示,判断此原始异常信息归类于微服务异常信息,否则将所述原始异常信息归类于公共组件异常信息。Analyze the format of the received original exception information. If the original exception information is represented in the format including the microservice ID, exception code ID, exception message with the first variable, and the second variable, it is judged that the original exception information is classified as a microservice. Exception information, otherwise the original exception information is classified as common component exception information.

具体地,对分类后的原始异常信息进行处理时,包括步骤:Specifically, when processing the classified original abnormal information, the steps include:

对微服务异常信息进行处理时,根据微服务ID,判断发生异常的微服务来源,并将原始异常信息中的第一变量替换为第二变量,得到第一拼接异常信息;如原始信息中包括“设备%s异常,PE1”,“%s”为第一变量,“PE1”为第二变量。When processing the microservice exception information, determine the source of the abnormal microservice according to the microservice ID, and replace the first variable in the original exception information with the second variable to obtain the first spliced exception information; if the original information includes "Device %s is abnormal, PE1", "%s" is the first variable, and "PE1" is the second variable.

对公共组件异常信息进行处理时,将公共组件异常信息按照预定义格式进行拼接,得到第二拼接异常信息。When processing the abnormal information of the common component, the abnormal information of the common component is spliced according to a predefined format to obtain the second spliced abnormal information.

所述原始异常信息、第一拼接异常信息和第二拼接异常信息以Map的形式存储,key为异常码、value为异常信息的内容,异常码定义为全局静态常量。The original exception information, the first splicing exception information and the second splicing exception information are stored in the form of Map, the key is the exception code, the value is the content of the exception information, and the exception code is defined as a global static constant.

具体地,微服务异常信息中包括:exceptionEnum即异常码,serviceId即微服务名称,带有第一变量errorVariable的异常消息exceptionMessage,第二变量。不同的微服务可以定义不同的微服务名称serviceId。Specifically, the exception information of the microservice includes: exceptionEnum is the exception code, serviceId is the name of the microservice, the exception message exceptionMessage with the first variable errorVariable, and the second variable. Different microservices can define different microservice names serviceId.

实施例2Example 2

如图3所示,本实施例提供一种全局异常处理平台,首先需要搭建与业务代码解耦的一个统一异常处理平台,平台分为detector和service两个模块,detector模块用于做异常的存储和处理,service模块用于做异常的查询。其中,detector模块的处理包含两方面,一方面是对异常信息中的变量做拼接,另一方面的处理是指对公共组件的异常处理。As shown in Figure 3, this embodiment provides a global exception processing platform. First, it is necessary to build a unified exception processing platform that is decoupled from business codes. The platform is divided into two modules: detector and service. The detector module is used for exception storage. And processing, the service module is used to do abnormal queries. Among them, the processing of the detector module includes two aspects. On the one hand, the variables in the exception information are spliced. On the other hand, the processing refers to the exception processing of the public components.

具体包括:Specifically include:

detector模块,用于获取来自微服务集群的所有原始异常信息,并根据信息格式的区别,将获取的原始异常信息分类为微服务异常信息和公共组件异常信息;然后对微服务异常信息进行处理得到第一拼接异常信息,对公共组件异常信息进行处理,得到第二拼接异常信息;The detector module is used to obtain all the original exception information from the microservice cluster, and classify the obtained original exception information into microservice exception information and common component exception information according to the difference in the information format; and then process the microservice exception information to obtain The first splicing exception information, processing the common component exception information, to obtain the second splicing exception information;

封装模块,用于将得到的第一拼接异常信息和第二拼接异常信息发送至Spring框架切面上,进行异常封装处理,得到对应的第一封装异常信息和第二封装异常信息;an encapsulation module, configured to send the obtained first splicing exception information and second splicing exception information to the Spring framework aspect, and perform exception encapsulation processing to obtain corresponding first encapsulation exception information and second encapsulation exception information;

反馈模块,用于返回第一封装异常信息和第二封装异常信息给发出对应的原始异常信息的发送方。The feedback module is configured to return the first packaged exception information and the second packaged exception information to the sender who sent the corresponding original exception information.

service模块,用于接收异常信息查询请求并进行异常信息查询。The service module is used to receive abnormal information query requests and query abnormal information.

存储模块,将所有原始的异常信息、第一异常信息和第二异常信息存入存储模块中。The storage module stores all the original abnormal information, the first abnormal information and the second abnormal information in the storage module.

进一步地,每个微服务中设有服务发布模块、业务流程模块、远程调用模块和信息处理模块,其中,Further, each microservice is provided with a service publishing module, a business process module, a remote calling module and an information processing module, wherein,

服务发布模块,用于向业务流程模块发送业务要求,以及接收其它微服务发送的业务请求并将业务请求发送给业务流程模块;The service publishing module is used to send business requirements to the business process module, receive business requests sent by other microservices, and send the business requests to the business process module;

业务流程模块,用于根据服务发布模块发送的信息进行业务处理,以及在服务调用或发生公共组件发生异常时,调用全局异常处理平台的detector模块或service模块;The business process module is used to perform business processing according to the information sent by the service publishing module, and call the detector module or service module of the global exception processing platform when a service call or an exception occurs in a common component;

远程调用模块,用于向其他微服务发送业务请求或者接收其它微服务发送的封装后的异常信息;The remote calling module is used to send business requests to other microservices or receive encapsulated exception information sent by other microservices;

信息处理模块,用于接收远程调用模块转发的封装后的异常信息,对封装后的异常信息进行处理或转发。The information processing module is used to receive the encapsulated exception information forwarded by the remote calling module, and process or forward the encapsulated exception information.

其中,service模块依赖detector模块,对外提供服务查询接口、对detector模块中的异常信息做查询即可,如对外暴露Get协议的URL查询接口,提供这个接口,后续如果一个异常信息增加了异常处理步骤,信息量很大,用户可以通过界面显示的异常码,依据异常码进行异常的查询,返回异常信息、处理步骤等。Among them, the service module relies on the detector module to provide a service query interface to the outside world and query the exception information in the detector module. For example, if the URL query interface of the Get protocol is exposed externally, this interface is provided. If an exception information is added later, exception handling steps are added. , the amount of information is very large, the user can query the exception according to the exception code through the exception code displayed on the interface, and return the exception information and processing steps.

实施例3:Example 3:

图1所示为实施例2中的全局异常处理平台的应用示例。FIG. 1 shows an application example of the global exception processing platform in the second embodiment.

一个集群服务包含微服务A、微服务B、微服务C、微服务D,其中,微服务A调用微服务B,微服务B会调用微服务C和D,为了做全局的异常处理,需要在detector模块中定义微服务B、C、D的异常信息。A cluster service includes microservice A, microservice B, microservice C, and microservice D. Among them, microservice A calls microservice B, and microservice B calls microservices C and D. In order to do global exception handling, it is necessary to The exception information of microservices B, C, and D is defined in the detector module.

以微服务A调用微服务B的过程为例,包括如下步骤:Take the process of microservice A calling microservice B as an example, including the following steps:

(1)微服务A向微服务B发起远程微服务调用;(1) Microservice A initiates a remote microservice call to microservice B;

(2)微服务B根据接收到的微服务A发送的指令进行业务处理;若微服务B的业务处理无误,则向微服务A返回响应信息;若有误,微服务B调用全局异常处理平台的detector模块进行异常处理;(2) Microservice B performs business processing according to the received instructions sent by microservice A; if the business processing of microservice B is correct, it returns response information to microservice A; if there is an error, microservice B calls the global exception handling platform The detector module performs exception handling;

(3)detector模块生成拼接好的异常信息并抛出;(3) The detector module generates the spliced exception information and throws it;

(4)拼接好的异常信息被Spring框架切面捕获,在Spring框架切面处做统一的异常封装处理后,返回给微服务B;(4) The spliced exception information is captured by the Spring framework aspect, and after unified exception encapsulation processing is performed at the Spring framework aspect, it is returned to microservice B;

(5)微服务B向微服务A返回封装后的异常信息;(5) Microservice B returns the encapsulated exception information to microservice A;

(6)微服务A对异常做处理。(6) Microservice A handles exceptions.

detector模块中添加系统异常信息的定义格式。所有异常信息以Map的形式存储,key为异常码、value为异常信息的内容。Add the definition format of system exception information in the detector module. All exception information is stored in the form of Map, the key is the exception code, and the value is the content of the exception information.

1)、微服务异常信息处理:1), Microservice exception information processing:

在detector模块中定义微服务B的一个类BMircoServiceConstants,用于表示微服务B的异常信息;定义一个全局静态常量PE_NOT_EXIST,表示PE设备不存在的异常码;定义一个全局Map结构,用于存储异常信息;将异常码和异常信息存储到Map中。Define a class BMircoServiceConstants of microservice B in the detector module, which is used to represent the exception information of microservice B; define a global static constant PE_NOT_EXIST, which represents the exception code that does not exist in the PE device; define a global Map structure to store exception information ; Store the exception code and exception information in the Map.

对微服务B的异常信息处理的方法如下:输入参数exceptionEnum为异常码,errorVariable为异常信息中需要替换的变量;调用detector模块中的detector方法进行处理,返回包装好的微服务B的异常类,最后抛出拼接后的异常信息。The method for processing the exception information of microservice B is as follows: the input parameter exceptionEnum is the exception code, and errorVariable is the variable to be replaced in the exception information; call the detector method in the detector module for processing, and return the packaged exception class of microservice B, Finally throw the spliced exception information.

如拼接处理前的微服务异常信息中的第一变量可使用%s做临时填充,也可使用其它内容做临时填充,进行拼接处理时,使用第二变量替换替换异常消息中的第二变量errorVariable即可。如当errorVariable为“PE1”、exceptionMessage为“设备%s异常”,拼接之后的第一异常信息为“设备PE1异常”。For example, the first variable in the microservice exception information before the splicing process can be temporarily filled with %s, or it can be temporarily filled with other contents. When the splicing process is performed, the second variable in the exception message can be replaced with the second variable errorVariable. That's it. For example, when errorVariable is "PE1" and exceptionMessage is "equipment %s exception", the first exception message after splicing is "equipment PE1 exception".

2)、公共组件异常信息处理:2), public component exception information processing:

detector模块将公共组件异常信息按照预定义格式进行拼接,得到第二拼接异常信息。具体包括步骤:判断公共组件异常信息是否属于预定义的能够识别的公共组件异常,如果是,则包装成具有异常码和异常信息的第二拼接异常信息,即包装成BaseException,然后抛出给Spring框架切面;如果不是,直接抛出,由对应的微服务处理。The detector module splices the common component exception information according to a predefined format to obtain the second splicing exception information. The specific steps include: judging whether the public component exception information belongs to a predefined and identifiable public component exception, and if so, package it into a second spliced exception information with exception code and exception information, that is, package it as BaseException, and then throw it to Spring Framework aspect; if not, throw it directly and handle it by the corresponding microservice.

以一个数据库语法异常为例,包括如下处理步骤:Take a database syntax exception as an example, including the following processing steps:

在detector模块存储公共异常的类CommonConstants中增加一个Map,Map的key为异常码,此处可添加一个全局静态常量,SQL_SYNTAX_ERROR,表示异常码,放入Map中,value为“数据库语法异常”。Add a Map to the CommonConstants class that stores common exceptions in the detector module. The key of the Map is the exception code. Here you can add a global static constant, SQL_SYNTAX_ERROR, which represents the exception code, put it in the Map, and the value is "database syntax exception".

然后在公共异常处理时,直接执行公共异常处理类CommonExceptionHandler的方法handlerException,在此方法中,可以增加“数据库语法异常”的处理逻辑,逻辑为先判断异常是否为MySQLSyntaxErrorException类型,如果是,则定义一个BaseException并抛出,BaseException的内容即为上述异常码和异常信息。Then, when handling common exceptions, directly execute the method handlerException of the common exception handling class CommonExceptionHandler. In this method, the processing logic of "database syntax exception" can be added. The logic is to first judge whether the exception is of MySQLSyntaxErrorException type, and if so, define a BaseException is thrown, and the content of BaseException is the above exception code and exception information.

微服务B调用微服务C、D的异常信息定义、处理方法、执行流程类似,不再赘述。The exception information definition, processing method, and execution flow of microservice B calling microservices C and D are similar, and they will not be repeated here.

实施例4Example 4

图2所示,是实施例2中的全局异常处理平台所在的微服务群在公网中的示意图。FIG. 2 is a schematic diagram of a micro-service group on the public network where the global exception processing platform in Embodiment 2 is located.

微服务设有面向集群内部提供的内部服务调用接口,和面向集群外部提供的外部服务服务接口,外部服务服务接口通过API路由网关Spring Cloud GateWay面向公网提供服务,如给外部客户端提供用户登录、注册等服务接口。Microservices have internal service invocation interfaces provided for the inside of the cluster, and external service service interfaces provided for the outside of the cluster. The external service service interface provides services to the public network through the API routing gateway Spring Cloud GateWay, such as providing user login for external clients , registration and other service interfaces.

外部客户端如手机终端、电脑终端访问公网,经过nginx反向代理和API路由网关安全认证、负载均衡(Ribbon)或服务限流熔断(Sentiel)之后,连接到微服务集群。在基于Spring Cloud的微服务架构中,面向外部的服务接口会通过API路由网关Spring CloudGateWay向公网暴露,面向内部的服务接口则在API路由网关进行屏蔽,内部微服务之间的微服务在微服务集群内进行,不暴露给公网。所有的微服务向Nacos注册自己的信息,Nacos组件用于存储服务信息,并对外提供服务列表查询接口。Spring Cloud GateWay就可以通过Nacos查询到现在所有的微服务。External clients such as mobile terminals and computer terminals access the public network and connect to the microservice cluster after passing nginx reverse proxy and API routing gateway security authentication, load balancing (Ribbon) or service current limiting and fuse (Sentiel). In the Spring Cloud-based microservice architecture, the external-facing service interface will be exposed to the public network through the API routing gateway Spring CloudGateWay, the internal-facing service interface will be shielded in the API routing gateway, and the microservices between internal microservices will be exposed to the microservices. It is carried out within the service cluster and is not exposed to the public network. All microservices register their own information with Nacos, and Nacos components are used to store service information and provide external service list query interfaces. Spring Cloud GateWay can query all current microservices through Nacos.

在全局异常处理平台中,由系统开发工程师编写异常信息代码,向detector模块中添加系统异常信息的定义格式等。detector模块对公共组件的异常处理时,在Spring的异常处理切面中,定义对Exception异常的处理,处理内容最开始使用public ErrorResulthandlerException(Exception ex)throws Exception方法。In the global exception processing platform, the system development engineer writes the exception information code, and adds the definition format of the system exception information to the detector module. When the detector module handles the exception of the public component, in the exception handling aspect of Spring, the handling of the Exception exception is defined, and the processing content first uses the public ErrorResulthandlerException(Exception ex)throws Exception method.

如果抛出了定义的BaseException既代表是已知异常,并且已经完成了处理,如果不是则说明系统尚存在未处理的异常,需要增加代码进行处理。具体来说如果是数据库异常,即为所说的公共三组件异常,将公共组件异常包装成已经识别的自定义BaseException异常并抛出;如果不是已知的公共组件异常,则直接抛出,待各自的微服务处理。If the defined BaseException is thrown, it means that it is a known exception and has been processed. If it is not, it means that there is an unhandled exception in the system, and you need to add code to deal with it. Specifically, if it is a database exception, that is, the so-called public three-component exception, wrap the public component exception into a recognized custom BaseException exception and throw it; if it is not a known public component exception, it will be thrown directly and wait for Respective microservice processing.

综上,本发明在微服务集群场景下搭建了一个统一异常处理平台,结合spring框架的切面能力,spring框架下包括AOP模块(Aspect Oriented Programming),即面向切面编程模块,就是在切面上将与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。In summary, the present invention builds a unified exception handling platform in the micro-service cluster scenario, combined with the aspect capability of the spring framework, the spring framework includes an AOP module (Aspect Oriented Programming), that is, an aspect-oriented programming module, which combines the It is not related to business, but is encapsulated by the logic or responsibility commonly called by business modules, which is convenient to reduce the repeated code of the system, reduce the coupling degree between modules, and is conducive to future operability and maintainability.

本发明还提供一种介质,存储有计算机可执行指令,所述指令在被处理单元执行时用于实现上述任意一种所述全局异常处理方法。The present invention further provides a medium storing computer-executable instructions, which are used to implement any one of the above-mentioned global exception handling methods when executed by a processing unit.

本发明还提供一种电子设备,包括处理单元和存储单元,存储有计算机可执行指令,所述指令在被所述处理单元执行时用于实现上述任意一种所述全局异常处理方法。The present invention also provides an electronic device, comprising a processing unit and a storage unit, storing computer-executable instructions, the instructions being used to implement any one of the above-mentioned global exception handling methods when executed by the processing unit.

以上所述仅是本发明的优选实施方式,应当指出:对于本技术领域的普通技术人员来说,在不脱离本发明原理的前提下,还可以做出若干改进和润饰,这些改进和润饰也应视为本发明的保护范围。The above is only the preferred embodiment of the present invention, it should be pointed out that: for those skilled in the art, without departing from the principle of the present invention, several improvements and modifications can also be made, and these improvements and modifications are also It should be regarded as the protection scope of the present invention.

Claims (10)

Translated fromChinese
1.一种大规模微服务集群场景下的全局异常处理方法,其特征在于,包括步骤:1. A global exception handling method under a large-scale microservice cluster scenario, characterized in that it comprises the steps:获取来自微服务集群的所有原始异常信息;Get all raw exception information from the microservice cluster;根据信息格式的区别,将获取的原始异常信息分类为微服务异常信息和公共组件异常信息,对微服务异常信息进行处理得到第一拼接异常信息,对公共组件异常信息进行拼接处理,得到第二拼接异常信息;According to the difference in information format, the obtained original exception information is classified into microservice exception information and common component exception information, the microservice exception information is processed to obtain the first spliced exception information, and the common component exception information is spliced to obtain the second exception information. splicing exception information;将所述第一拼接异常信息和第二拼接异常信息发送至Spring框架切面上,进行异常封装处理,得到对应的第一封装异常信息和第二封装异常信息;Sending the first splicing exception information and the second splicing exception information to the Spring framework section, and performing exception encapsulation processing to obtain corresponding first encapsulation exception information and second encapsulation exception information;返回第一封装异常信息和第二封装异常信息给对应的原始异常信息的发送方。Return the first encapsulated exception information and the second encapsulated exception information to the sender of the corresponding original exception information.2.根据权利要求1所述的大规模微服务集群场景下的全局异常处理方法,其特征在于,将获取的原始异常信息分类为微服务异常信息和公共组件异常信息,包括步骤:2. The global exception handling method in a large-scale microservice cluster scenario according to claim 1, wherein the obtained original exception information is classified into microservice exception information and common component exception information, comprising the steps of:判断原始异常信息是否以包括微服务名称、异常码、带有第一变量的异常消息、第二变量的格式表示,若是,则将此原始异常信息归类于微服务异常信息,否则将所述原始异常信息归类于公共组件异常信息。Determine whether the original exception information is represented in a format including the name of the microservice, the exception code, the exception message with the first variable, and the second variable. If so, classify the original exception information into the microservice exception information, otherwise, classify the The raw exception information is classified under the common component exception information.3.根据权利要求2所述的大规模微服务集群场景下的全局异常处理方法,其特征在于,对微服务异常信息进行处理得到第一拼接异常信息,对公共组件异常信息进行拼接处理,得到第二拼接异常信息,包括步骤:3. The global exception processing method in a large-scale microservice cluster scenario according to claim 2, wherein the microservice exception information is processed to obtain the first splicing exception information, and the common component exception information is spliced and processed to obtain the The second splicing exception information, including steps:对于微服务异常信息,根据微服务名称,判断发生异常的微服务来源,将微服务异常信息中的第一变量替换为第二变量,得到第一拼接异常信息;For the microservice exception information, according to the name of the microservice, determine the source of the abnormal microservice, replace the first variable in the microservice exception information with the second variable, and obtain the first splicing exception information;对于公共组件异常信息,将公共组件异常信息按照预定义格式进行拼接,得到第二拼接异常信息。For the common component exception information, the common component exception information is spliced according to a predefined format to obtain the second spliced exception information.4.根据权利要求1所述的大规模微服务集群场景下的全局异常处理方法,其特征在于:所述原始异常信息产生于微服务集群中的任一微服务在服务调用发生错误时,由被调用的微服务发出;或者产生于公共组件发生异常时,由访问和发现公共组件发生异常的微服务发出。4. The global exception handling method in a large-scale microservice cluster scenario according to claim 1, wherein the original exception information is generated by any microservice in the microservice cluster when an error occurs in the service call, and is generated by It is issued by the called microservice; or when an exception occurs in a public component, it is issued by a microservice that accesses and finds an exception in the public component.5.根据权利要求1所述的大规模微服务集群场景下的全局异常处理方法,其特征在于:所述原始异常信息、第一拼接异常信息和第二拼接异常信息以Map的形式存储,key为异常码、value为异常信息的内容,异常码定义为全局静态常量。5. The global exception handling method in a large-scale microservice cluster scenario according to claim 1, wherein the original exception information, the first splicing exception information and the second splicing exception information are stored in the form of Map, and the key is the exception code, value is the content of the exception information, and the exception code is defined as a global static constant.6.一种全局异常处理平台,其特征在于,包括:6. A global exception handling platform, characterized in that, comprising:detector模块,用于获取来自微服务集群的所有原始异常信息,并根据信息格式的区别,将获取的原始异常信息分类为微服务异常信息和公共组件异常信息;然后对微服务异常信息进行处理得到第一拼接异常信息,对公共组件异常信息进行处理,得到第二拼接异常信息;The detector module is used to obtain all the original exception information from the microservice cluster, and classify the obtained original exception information into microservice exception information and common component exception information according to the difference in the information format; and then process the microservice exception information to obtain The first splicing exception information, processing the common component exception information, to obtain the second splicing exception information;封装模块,用于将所述第一拼接异常信息和第二拼接异常信息发送至Spring框架切面上,进行异常封装处理,得到对应的第一封装异常信息和第二封装异常信息;An encapsulation module, configured to send the first splicing exception information and the second splicing exception information to the Spring framework aspect, and perform an exception encapsulation process to obtain corresponding first encapsulation exception information and second encapsulation exception information;反馈模块,用于返回第一封装异常信息和第二封装异常信息给对应的原始异常信息的发送方。The feedback module is configured to return the first packaged exception information and the second packaged exception information to the sender of the corresponding original exception information.7.根据权利要求6所述的一种全局异常处理平台,其特征在于:还包括service模块,用于接收异常信息查询请求并进行异常信息查询。7 . The global exception processing platform according to claim 6 , further comprising a service module for receiving an exception information query request and querying the exception information. 8 .8.根据权利要求6所述的一种全局异常处理平台,其特征在于:还包括存储模块,用于存储所有原始的异常信息、第一拼接异常信息和第二拼接异常信息。8 . The global exception processing platform according to claim 6 , further comprising a storage module for storing all original exception information, the first splicing exception information and the second splicing exception information. 9 .9.一种介质,存储有计算机可执行指令,其特征在于:所述指令在被处理单元执行时用于实现权利要求1至4中任一项所述方法。9. A medium storing computer-executable instructions, wherein the instructions, when executed by a processing unit, are used to implement the method of any one of claims 1 to 4.10.一种电子设备,其特征在于:包括处理单元和存储单元,存储有计算机可执行指令,所述指令在被所述处理单元执行时用于实现权利要求1至4中任一项所述方法。10. An electronic device, characterized in that it comprises a processing unit and a storage unit, and stores computer-executable instructions, which are used to implement the instructions in any one of claims 1 to 4 when executed by the processing unit. method.
CN202111653425.9A2021-12-302021-12-30 Global exception handling method and platform in large-scale microservice cluster scenariosPendingCN114296985A (en)

Priority Applications (2)

Application NumberPriority DateFiling DateTitle
CN202111653425.9ACN114296985A (en)2021-12-302021-12-30 Global exception handling method and platform in large-scale microservice cluster scenarios
PCT/CN2022/143273WO2023125773A1 (en)2021-12-302022-12-29Global exception handling method and platform in large-scale micro-service cluster scenario

Applications Claiming Priority (1)

Application NumberPriority DateFiling DateTitle
CN202111653425.9ACN114296985A (en)2021-12-302021-12-30 Global exception handling method and platform in large-scale microservice cluster scenarios

Publications (1)

Publication NumberPublication Date
CN114296985Atrue CN114296985A (en)2022-04-08

Family

ID=80973810

Family Applications (1)

Application NumberTitlePriority DateFiling Date
CN202111653425.9APendingCN114296985A (en)2021-12-302021-12-30 Global exception handling method and platform in large-scale microservice cluster scenarios

Country Status (2)

CountryLink
CN (1)CN114296985A (en)
WO (1)WO2023125773A1 (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication numberPriority datePublication dateAssigneeTitle
WO2023125773A1 (en)*2021-12-302023-07-06网络通信与安全紫金山实验室Global exception handling method and platform in large-scale micro-service cluster scenario

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication numberPriority datePublication dateAssigneeTitle
CN118916074B (en)*2024-10-092024-12-31杭州乒乓智能技术有限公司Interface fusing method, interface fusing apparatus, and computer-readable storage medium

Family Cites Families (7)

* Cited by examiner, † Cited by third party
Publication numberPriority datePublication dateAssigneeTitle
CN110535902A (en)*2019-07-162019-12-03平安普惠企业管理有限公司The processing method and electronic device of monitoring intelligent are realized based on micro services
CN110806921B (en)*2019-09-302023-02-07烽火通信科技股份有限公司OVS (optical virtual system) abnormity alarm monitoring system and method
CN111061628B (en)*2019-11-212023-09-01天翼数字生活科技有限公司Data analysis method, system, device, computer equipment and storage medium
CN111669425B (en)*2020-04-142022-12-09福建天泉教育科技有限公司Method for monitoring microservice interface and storage medium
CN113190372B (en)*2021-05-242023-02-07深圳赛安特技术服务有限公司Multi-source data fault processing method and device, electronic equipment and storage medium
CN113609012B (en)*2021-07-302024-03-29南京控维通信科技有限公司Method and system for normalized processing back-end exception reporting
CN114296985A (en)*2021-12-302022-04-08网络通信与安全紫金山实验室 Global exception handling method and platform in large-scale microservice cluster scenarios

Cited By (1)

* Cited by examiner, † Cited by third party
Publication numberPriority datePublication dateAssigneeTitle
WO2023125773A1 (en)*2021-12-302023-07-06网络通信与安全紫金山实验室Global exception handling method and platform in large-scale micro-service cluster scenario

Also Published As

Publication numberPublication date
WO2023125773A1 (en)2023-07-06

Similar Documents

PublicationPublication DateTitle
US11153412B1 (en)Systems and/or methods for non-intrusive injection of context for service mesh applications
CN106131213B (en)Service management method and system
US10439987B2 (en)Systems and methods for securing network traffic flow in a multi-service containerized application
Falazi et al.Smart contract invocation protocol (SCIP): A protocol for the uniform integration of heterogeneous blockchain smart contracts
CN106663033B (en)System and method for supporting a wraparound domain and proxy model and updating service information for cross-domain messaging in a transactional middleware machine environment
CN104317591B (en)A kind of web interface frame system and web method for processing business based on OSGi
US8850412B2 (en)System and method for using aspects to generate event data records
CN113630310A (en)Distributed high-availability gateway system
US20130013734A1 (en)Information on Demand Process Framework Method to Generate, Manage, Secure, and Deploy Browsers and Applications Accessible Web Services
CN102109984B (en) State machine processing method and system
WO2023125773A1 (en)Global exception handling method and platform in large-scale micro-service cluster scenario
US8230448B2 (en)Methods, systems and computer program products for web service interaction with a resource management system
CN113794652B (en)Data processing method, device, electronic equipment and storage medium
CN104184774A (en)Information processing method based on sandbox environment and system thereof
AllianceService-based architecture in 5G
CN114416314B (en)Service arrangement method based on API gateway
CN115550468B (en) Business request processing method, device, equipment and medium
WO2025091762A1 (en)Interconnection method, electronic device and storage medium
WO2021093671A1 (en)Task processing method, system, apparatus and device, and computer readable storage medium
CN114553928B (en) A business processing method and device, electronic equipment, and medium
CN119105887B (en) Message processing method, device, electronic device and storage medium
US12086141B1 (en)Coordination of services using PartiQL queries
US20240354175A1 (en)Actioning system for observability platforms
CN112073358B (en)Protocol conversion processing method and device based on Kubernetes
CN112073449B (en)Kubernetes-based environment switching processing method and equipment

Legal Events

DateCodeTitleDescription
PB01Publication
PB01Publication
SE01Entry into force of request for substantive examination
SE01Entry into force of request for substantive examination
CB02Change of applicant information

Country or region after:China

Address after:No. 9 Mozhou East Road, Nanjing City, Jiangsu Province, 211111

Applicant after:Zijinshan Laboratory

Address before:No. 9 Mozhou East Road, Jiangning Economic Development Zone, Jiangning District, Nanjing City, Jiangsu Province

Applicant before:Purple Mountain Laboratories

Country or region before:China

CB02Change of applicant information

[8]ページ先頭

©2009-2025 Movatter.jp