CN116909786A - Processing method of inter-service call exception based on spring cloud gateway - Google Patents

Processing method of inter-service call exception based on spring cloud gateway Download PDF

Info

Publication number
CN116909786A
CN116909786A CN202310704854.7A CN202310704854A CN116909786A CN 116909786 A CN116909786 A CN 116909786A CN 202310704854 A CN202310704854 A CN 202310704854A CN 116909786 A CN116909786 A CN 116909786A
Authority
CN
China
Prior art keywords
exception
kafka
cloud gateway
abnormal information
class
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
CN202310704854.7A
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.)
QIMING INFORMATION TECHNOLOGY CO LTD
Original Assignee
QIMING INFORMATION TECHNOLOGY CO LTD
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 QIMING INFORMATION TECHNOLOGY CO LTD filed Critical QIMING INFORMATION TECHNOLOGY CO LTD
Priority to CN202310704854.7A priority Critical patent/CN116909786A/en
Publication of CN116909786A publication Critical patent/CN116909786A/en
Pending legal-status Critical Current

Links

Classifications

    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Abstract

The invention relates to a method for processing call exception among services based on spring cloud gateway, which comprises the following steps: defining a global filter, intercepting a request and acquiring exception information, formatting the exception information and sending the exception information to Kafka, storing the exception information, acquiring and processing the exception information by a consumer, and realizing high reliability and high expandability of an exception log. The invention can generate more detailed abnormal logs, is convenient for problem positioning and analysis, can be used for business analysis and accident recovery, and is convenient for users to quickly position the root cause of the problem; by utilizing a spring cloud gateway global filter, the exception logs can be centrally managed and analyzed by sending the exception logs to the Kafka for unified processing, so that the exception logs are prevented from being scattered in each service instance and are difficult to manage and analyze; by realizing global exception handling and a unified exception log recording mechanism, the problem of inconsistent exception handling in different service instances can be avoided, and the stability and maintainability of the system are improved.

Description

Processing method of inter-service call exception based on spring cloud gateway
Technical Field
The invention relates to the technical field of inter-service call, in particular to a spring cloud gateway-based inter-service call exception processing method.
Background
spring cloud gateway is a brand new item of spring group, which is a gateway developed based on technologies such as spring 5.0,spring boot 2.0 and project reactor, and aims to provide a simple and effective unified API route management manner for a micro-service architecture, spring cloud gateway is a real mode communication frame netty with high performance based on webflux frame bottom layer, spring cloud gateway not only provides a unified route manner, but also provides basic functions of the gateway based on a filter chain manner, for example: safety, monitoring/index and current limiting spring cloud gateway has the following features: based on spring frame 5,project boot 2.0; integrating a hystrix circuit breaker; integration spring cloud discovery client; the predictes and filters act on specific routes, and the predictes and filters are easy to write; high-level functions of some gateways are provided: dynamic routing, current limiting, path rewriting.
The problem that the abnormal logs are scattered in each service instance and the abnormal processing in different service instances is inconsistent in the conventional inter-service call, so that the abnormal information is difficult to manage and analyze, the stability and maintainability of a system are reduced, and in order to solve the problem, the invention provides a method for processing the inter-service call abnormality based on spring cloud gateway.
Disclosure of Invention
The invention provides a method for processing call exceptions among services based on spring cloud gateway in order to solve the technical problem that exception logs are scattered in each service instance and exception handling in different service instances is inconsistent.
A processing method of call exception among services based on spring cloud gateway comprises the following steps:
s1, defining a global filter;
s2, intercepting the request and acquiring abnormal information;
s3, formatting the abnormal information and sending the abnormal information to Kafka;
s4, storing abnormal information;
s5, the consumer acquires and processes the abnormal information;
s6, achieving high reliability and high expandability of the abnormal log.
Further, a method for handling call exceptions between services based on spring cloud gateway, the step S1 includes the following sub-steps:
s11, creating a Java class to realize a GlobalFilter interface, wherein the Java class comprises a class named CustomghobalFilter;
s12, adding @ Component comments on the custom filter class, and declaring the @ Component comments as a Spring Component so as to be managed by a Spring container;
s13, writing logic of a custom filter in a filter () method.
Further, a method for handling call exceptions between services based on spring cloud gateway, the step S2 includes the following sub-steps:
s21, creating a Java class as a section class for defining interceptor logic, wherein the Java class is marked by using an @ Aspect annotation;
s22, in the section class, using @ afterthreading annotation to define an entry point expression, and designating that the method is executed when the exception is thrown;
s23, compiling interception logic;
s24, capturing abnormal information by using a try-catch block;
s25, writing self-defined filtering logic in a filter () method of the filter, and operating the request according to the requirement, wherein the operation comprises interception, modification and verification.
Further, a method for handling call exceptions between services based on spring cloud gateway, the step S3 includes the following sub-steps:
s31, converting the abnormal information into a JSON format;
s32, adding additional information, including a time stamp and a request path;
s33, transmitting the abnormal information in the JSON format to the Topic of Kafka.
Further, in a method for processing call exceptions between services based on spring cloud gateway, the specific step of step S4 is to define a Topic and store an exception log.
Further, a method for handling call exceptions between services based on spring cloud gateway, the step S5 includes the following sub-steps:
s51, defining a consumer class, and monitoring Topic of Kafka by using @ KafkaListener annotation;
s52, processing logic for realizing abnormal information in the consumer class;
s53, configuring relevant parameters of Kafka consumers.
Further, a method for handling call exception between services based on spring cloud gateway, the processing logic in the substep S52 includes storing information in a database, and notifying related personnel by means of a right button or a short message.
Further, a method for handling call exceptions between services based on spring cloud gateway is provided, wherein the relevant parameters in the substep S53 include a consumption group ID, a consumption location and a reverse serialization manner.
Further, a method for processing call exceptions between services based on spring cloud gateway is provided, and the implementation manner of the step S6 includes configuring copies and partitions of Kafka, configuring producers and consumers of Kafka, and using Kafka Connect and Kafka Streams. pring Cloud: a micro-service framework based on Spring Boot can help a developer to quickly construct a distributed system. Spring Cloud provides a series of components including service registration and discovery, load balancing, circuit breakers, etc., which can help developers easily build highly available, highly reliable distributed systems.
API gateway: a service of a client-oriented Application Program Interface (API) can process and forward a request of a client, and simultaneously provides functions of security, monitoring, load balancing and the like. The API gateway can simplify the request flow of the client, can realize the functions of flow control, degradation, current limiting and the like, and improves the reliability of the system.
Exception handling: an important development skill can help developers find and solve problems in a system in time, in a distributed system, exception handling becomes more complex due to the presence of calls between multiple services. Therefore, unified handling and recording of exceptions is critical.
Log processing: the log is an important component in the distributed system, and can help a developer to know the running condition of the system and discover and solve the problems in time. In a distributed system, due to the existence of calls among multiple services, logs need to be recorded uniformly, and call chain tracking is realized so as to quickly locate the source of the problem.
spring cloud gateway: an API gateway is mainly responsible for routing, forwarding and filtering among services, and if the services in the API gateway are abnormal, the abnormality needs to be properly processed so as to be convenient for quick positioning and solving of the problems.
The invention has the beneficial effects that:
the spring cloud gateway global filter is used for acquiring global exception information and recording the global exception information, so that service call exceptions can be effectively processed, more detailed exception logs can be generated, the exception logs comprise information such as exception types, request URLs, request parameters, exception stacks and the like, problem location and analysis can be conveniently carried out, the method can be used for service analysis, accident recovery and convenient use of personnel to quickly locate the root cause of the problem;
by utilizing a spring cloud gateway global filter, the exception logs can be centrally managed and analyzed by sending the exception logs to the Kafka for unified processing, so that the exception logs are prevented from being scattered in each service instance and are difficult to manage and analyze;
by realizing global exception handling and a unified exception log recording mechanism, the problem of inconsistent exception handling in different service instances can be avoided, and the stability and maintainability of the system are improved.
Drawings
FIG. 1 is a flow chart of the present invention;
FIG. 2 is a flow chart of step S1 of the present invention;
FIG. 3 is a flow chart of step S2 of the present invention;
FIG. 4 is a flowchart of step S3 of the present invention;
fig. 5 is a flowchart of step S5 of the present invention.
Description of the embodiments
For a clearer understanding of technical features, objects, and effects of the present invention, a specific embodiment of the present invention will be described with reference to the accompanying drawings.
As shown in fig. 1, a method for processing call exception among services based on spring cloud gateway includes the following steps:
s1, defining a global filter;
s2, intercepting the request and acquiring abnormal information;
s3, formatting the abnormal information and sending the abnormal information to Kafka;
s4, storing abnormal information;
s5, the consumer acquires and processes the abnormal information;
s6, achieving high reliability and high expandability of the abnormal log.
In this embodiment, a method for processing an inter-service call exception based on spring cloud gateway, the step S1 includes the following sub-steps:
s11, creating a Java class to realize a GlobalFilter interface, wherein the Java class comprises a class named CustomghobalFilter;
s12, adding @ Component comments on the custom filter class, and declaring the @ Component comments as a Spring Component so as to be managed by a Spring container;
s13, writing logic of a custom filter in a filter () method.
In this embodiment, a method for processing an inter-service call exception based on spring cloud gateway, the step S2 includes the following sub-steps:
s21, creating a Java class as a section class for defining interceptor logic, wherein the Java class is marked by using an @ Aspect annotation;
s22, in the section class, using @ afterthreading annotation to define an entry point expression, and designating that the method is executed when the exception is thrown;
s23, compiling interception logic;
s24, capturing abnormal information by using a try-catch block;
s25, writing self-defined filtering logic in a filter () method of the filter, and operating the request according to the requirement, wherein the operation comprises interception, modification and verification.
In this embodiment, a method for processing an inter-service call exception based on spring cloud gateway, the step S3 includes the following sub-steps:
s31, converting the abnormal information into a JSON format;
s32, adding additional information, including a time stamp and a request path;
s33, transmitting the abnormal information in the JSON format to the Topic of Kafka.
In this embodiment, a method for handling call exceptions between services based on spring cloud gateway is provided, and the specific step of step S4 is to define a Topic and store an exception log.
In this embodiment, a method for handling an inter-service call exception based on spring cloud gateway, the step S5 includes the following sub-steps:
s51, defining a consumer class, and monitoring Topic of Kafka by using @ KafkaListener annotation;
s52, processing logic for realizing abnormal information in the consumer class;
s53, configuring relevant parameters of Kafka consumers.
In this embodiment, a method for handling an inter-service call exception based on spring cloud gateway, where the processing logic in the substep S52 includes storing information in a database, and notifying related personnel by means of a right key or a short message.
In this embodiment, a method for handling an inter-service call exception based on spring cloud gateway is provided, where the relevant parameters in the substep S53 include a consumption group ID, a consumption location, and a reverse serialization manner.
In this embodiment, a method for processing an inter-service call exception based on spring cloud gateway, where the implementation of step S6 includes configuring a copy and partition of Kafka, configuring a producer and consumer of Kafka, and using Kafka Connect and Kafka Streams.
In this embodiment, a global filter may be defined by implementing a GlobalFilter interface in spring cloud gateway, according to a method for handling inter-service call exceptions based on spring cloud gateway. The global filter can intercept the request and execute corresponding logic after the request is finished, and in the global filter, the request, the response and the exception information can be acquired and processed.
In this embodiment, a method for handling inter-service call exceptions based on spring cloud gateway is to execute a plurality of global filters in the order they are executed in code if defined. If it is necessary to change their execution order, their order can be specified by implementing an Ordered interface. In addition, if multiple global filters are defined, it is necessary to ensure that their execution order does not affect the correctness of the business logic.
In this embodiment, a method for processing call exceptions between services based on spring cloud gateway uses Kafka Connect to implement persistence and conversion of data and uses Kafka Streams to implement real-time stream processing and data analysis.
In this embodiment, a method for handling call exceptions between services based on spring cloud gateway may perform operations such as logging, modifying requests, adding request headers, etc. when a request enters and a response returns in step S13. Note that the filter () method of GatewayFilterChain is to be invoked to continue executing the filter chain. The custom filter can perform more complex operations, such as verifying the validity of the request, authenticating the request, and the like, and corresponding business logic can be added in the filter according to actual requirements.
In this embodiment, a method for handling call exceptions between services based on Spring cloud gateway is generally declared as a Spring Component using @ Component annotation in step S21.
In this embodiment, in step S22, in the 'point_cut' attribute, an entry point expression may be used to match the method that needs interception.
In this embodiment, in step S23, in the intercepting method of the section class, related information of the request, such as a request parameter, a request path, etc., may be obtained through a join point parameter. And acquiring the thrown abnormal information through the Exception parameter. Exception information may be logged or other logic may be executed, as desired.
In this embodiment, in step S24, custom filter logic is written in the try block, and the abnormal code may be thrown. The captured exception is handled in the catch block. Here, exception handling is performed, such as logging, returning a custom error response, etc. Specific logic defining exception handling in the 'handleException ()' method may be selected, such as logging, returning a custom error response, etc. The intercepted request can be processed in the try block, and normal calling information is recorded as a log.
In this embodiment, in step S25, the processing method of the inter-service call exception based on spring cloud gateway may obtain, through the 'server webexchange' object, the relevant information of the request, such as the request path, the request method, the request header, the request parameter, and the like. Some custom operations are performed in the filtering logic, such as print logs, add request headers, modify request bodies, etc.
The method and the system acquire and record global exception information by using the spring cloud gateway global filter, can effectively process call exceptions among services, can generate more detailed exception logs, comprise information such as exception types, request URLs, request parameters, exception stacks and the like, are convenient to locate and analyze problems, can be used for service analysis and accident recovery, and are convenient for users to quickly locate the root cause of the problems; by utilizing a spring cloud gateway global filter, the exception logs can be centrally managed and analyzed by sending the exception logs to the Kafka for unified processing, so that the exception logs are prevented from being scattered in each service instance and are difficult to manage and analyze; by realizing global exception handling and a unified exception log recording mechanism, the problem of inconsistent exception handling in different service instances can be avoided, and the stability and maintainability of the system are improved.
The foregoing has shown and described the basic principles and main features of the present invention and the advantages of the present invention. It will be understood by those skilled in the art that the present invention is not limited to the embodiments described above, and that the above embodiments and descriptions are merely illustrative of the principles of the present invention, and various changes and modifications may be made without departing from the spirit and scope of the invention, which is defined in the appended claims. The scope of the invention is defined by the appended claims and equivalents thereof.

Claims (9)

1. A method for processing call exception among services based on spring cloud gateway, which is characterized by comprising the following steps:
s1, defining a global filter;
s2, intercepting the request and acquiring abnormal information;
s3, formatting the abnormal information and sending the abnormal information to Kafka;
s4, storing abnormal information;
s5, the consumer acquires and processes the abnormal information;
s6, achieving high reliability and high expandability of the abnormal log.
2. The method for handling call exceptions between services based on spring cloud gateway according to claim 1, wherein said step S1 comprises the sub-steps of:
s11, creating a Java class to realize a GlobalFilter interface, wherein the Java class comprises a class named CustomghobalFilter;
s12, adding @ Component comments on the custom filter class, and declaring the @ Component comments as a Spring Component so as to be managed by a Spring container;
s13, writing logic of a custom filter in a filter () method.
3. A method for handling inter-service call exceptions based on spring cloud gateway as claimed in claim 1, characterized in that said step S2 comprises the sub-steps of:
s21, creating a Java class as a section class for defining interceptor logic, wherein the Java class is marked by using an @ Aspect annotation;
s22, in the section class, using @ afterthreading annotation to define an entry point expression, and designating that the method is executed when the exception is thrown;
s23, compiling interception logic;
s24, capturing abnormal information by using a try-catch block;
s25, writing self-defined filtering logic in a filter () method of the filter, and operating the request according to the requirement, wherein the operation comprises interception, modification and verification.
4. A method for handling inter-service call exceptions based on spring cloud gateway as claimed in claim 1, characterized in that said step S3 comprises the sub-steps of:
s31, converting the abnormal information into a JSON format;
s32, adding additional information, including a time stamp and a request path;
s33, transmitting the abnormal information in the JSON format to the Topic of Kafka.
5. The method for handling call exceptions between services based on spring cloud gateway as claimed in claim 1, wherein the specific step of step S4 is to define a Topic and store an exception log.
6. A method for handling inter-service call exceptions based on spring cloud gateway as claimed in claim 1, characterized in that said step S5 comprises the sub-steps of:
s51, defining a consumer class, and monitoring Topic of Kafka by using @ KafkaListener annotation;
s52, processing logic for realizing abnormal information in the consumer class;
s53, configuring relevant parameters of Kafka consumers.
7. The method of claim 6, wherein the processing logic in the substep S52 includes storing information in a database, notifying the relevant personnel by means of a right button or a text message.
8. The method of claim 6, wherein the parameters associated with step S53 include a consuming group ID, a consuming location, and a reverse serialization method.
9. The method according to claim 1, wherein the implementation of step S6 includes configuring copies and partitions of Kafka, configuring producers and consumers of Kafka, and using Kafka Connect and Kafka Streams.
CN202310704854.7A 2023-06-14 2023-06-14 Processing method of inter-service call exception based on spring cloud gateway Pending CN116909786A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202310704854.7A CN116909786A (en) 2023-06-14 2023-06-14 Processing method of inter-service call exception based on spring cloud gateway

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202310704854.7A CN116909786A (en) 2023-06-14 2023-06-14 Processing method of inter-service call exception based on spring cloud gateway

Publications (1)

Publication Number Publication Date
CN116909786A true CN116909786A (en) 2023-10-20

Family

ID=88351949

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202310704854.7A Pending CN116909786A (en) 2023-06-14 2023-06-14 Processing method of inter-service call exception based on spring cloud gateway

Country Status (1)

Country Link
CN (1) CN116909786A (en)

Similar Documents

Publication Publication Date Title
US10528454B1 (en) Intelligent automation of computer software testing log aggregation, analysis, and error remediation
US6584491B1 (en) Arrangement for monitoring a progress of a message flowing through a distributed multiprocess system
WO2020029375A1 (en) Interface testing method and system, and computer device and storage medium
US9442822B2 (en) Providing a visual representation of a sub-set of a visual program
CN109614290A (en) Process exception information recording method and system in container
CN110784527A (en) Information management system and method
US20100198636A1 (en) System and method for auditing governance, risk, and compliance using a pluggable correlation architecture
US11218534B2 (en) Distributed system management
CN112511218B (en) Satellite ground station monitoring system based on microservice
CN109460307B (en) Micro-service calling tracking method and system based on log embedded point
CN114745295A (en) Data acquisition method, device, equipment and readable storage medium
CN114189274A (en) Satellite ground station monitoring system based on microservice
CN116893945A (en) Operation and maintenance cloud platform service early warning method, system and device
CN114510356A (en) Log tracking method, device, equipment and medium for calling operation between microservices
CN110008086B (en) Log generation method and device and client
CN116909786A (en) Processing method of inter-service call exception based on spring cloud gateway
CN114172821B (en) Service state synchronization method and device and server
US11582345B2 (en) Context data management interface for contact center
WO2021036987A1 (en) Method and device for achieving operation and maintenance monitoring
CN114723397A (en) Flow execution method and device
Cole Hands-on Microservices with C#: Designing a Real-world, Enterprise-grade Microservice Ecosystem with the Efficiency of C# 7
Rojas et al. Web and Telco Service Integration: A Dynamic and Adaptable Approach
US11645137B2 (en) Exception management in heterogenous computing environment
CN116109112B (en) Service data processing method, device, medium and equipment based on aggregation interface
US11775401B1 (en) Intelligent coordination of log analysis and repair processes in a multi-cloud system

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination