Problems and scenarios
There are scenarios in the business that need to be distributed. For example, a WeChat callback message can only specify one address, so it is expected that a message can be copied and sent to other services. Service B and Service A that receives WeChat callback information are processed together.
This article will introduce the relevant content of golang reuse in detail, and share it for your reference and learning. I won’t say much below, let’s take a look at the detailed introduction together.
Solution
The first thing I considered was direct forwarding. useReverseProxyDirectly forwarded by Service A to Service B. However, WeChat involves verification and other issues, so it is very troublesome to completely adjust it. So change your thinking and plan to post the content directly to Service B.
But it's a readcloser. When we readAll, we will say that the information inside cannot be read again.
How can I use the copy?
where c represents the context of http
// Read out the request content var bodyBytes []byte if != nil { bodyBytes, _ = () } // Write what you just read into = ((bodyBytes))
1. We first read the body from it and save it into a variable.
2. Then write the data usage method in the variable back into it.
/pkg/io/ioutil/#NopCloser
NopCloser returns a ReadCloser with a no-op Close method wrapping the provided Reader r.
NopCloser wraps Reader r with an operationless Close method to return a ReadCloser interface.
This way we can use it again to process it.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.