This article is a mirror article of machine translation, please click here to jump to the original article.

View: 31840|Reply: 2

[Source] Spring Cloud records all request interface logs to MongoDB

[Copy link]
Posted on 8/15/2021 9:47:53 PM | | | |
In microservice development, logging is a very basic task, recording interface request log information, which helps us troubleshoot problems and help reproduce problems. The log content includes the link, request method, useragent, header, current microservice node machine IP, node machine name, running environment, requester IP, time consumption, and other information.

Using the Filter blocker of the Spring Cloud framework, you can intercept the request information records of all interfaces and persist the request records in MongoDB.



Interface log structure

First, create a new object and define the structure of the log, the code is as follows:

Log filter

Create a new RequestLoggerFilter filter, inherit the Filter interface, and implement the doFilter method, this filter has the highest priority, and the filter priority can be set through the @Order annotation.Filters log POST/PUT commits (excludes multipart/form-data file commit records), the code is as follows:

Spring Cloud obtains the client IP address
https://www.itsvse.com/thread-10022-1-1.html

Spring Cloud Filter fetches the requested content via the ContentCachingRequestWrapper
https://www.itsvse.com/thread-10030-1-1.html

Spring Boot's HandlerInterceptor interceptor
https://www.itsvse.com/thread-10001-1-1.html

Logs persist to MongoDB

In order not to affect the normal use of the interface, the interface log will be dropped into the thread-safe queue, the program will open a thread, the thread will consume the data in the queue, store the data in MongoDB, and use maven to introduce dependencies.

CentOS 7 installation mongodb 5.0.1 tutorial
https://www.itsvse.com/thread-10025-1-1.html

Linux correctly closes the mongodb database
https://www.itsvse.com/thread-10021-1-1.html
For more information about mongodb, see org.springframework.boot.autoconfigure.mongo.MongoProperties

application.yml The configuration is as follows:

URI format: username: root, password: aaaaaaa, database address: 127.0.0.1, port number: 27017, database name: test
For example: mongodb://root:aaaaaa@127.0.0.1:27017/test

IfThe username or password contains a special symbol, so use the URL encoding, otherwise an error will be reported!

Call MongoTemplate to persist data into the database with the following code:

(End)





Previous:OpenSSL is deployed from visa documents to IIS and SLB
Next:"Dynamic Replacement" for .NET Plugins
 Landlord| Posted on 11/18/2021 11:14:19 AM |
Record the response log as shown in the following figure:





Posted on 10/14/2022 3:19:47 PM |
I studied, just in time
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com