主要問題:
如果它是由公共代理服務器創建的,我是否需要在每個內部微服務請求中傳遞標頭才能將其包含在其他微服務中?
例子:
結構:

在 Nginx.conf 中:
proxy_set_header X-Request-ID $request_id;
當我請求這些微服務中的任何一個時,我可以使用req.headers["x-request-id"]
但是,當我喜歡時:

// calling ms2 from ms1
const response = await fetch("http://ms2/");
在ms1(第一跳)中,我可以像之前顯示的那樣獲得標題,但在ms2中我不能。
我知道快遞服務器同時處理一堆請求,正因為如此。但無論如何,當您每次都需要通過內部請求傳遞標頭時,感覺很奇怪:
// calling ms2 from ms1
const response = await fetch("http://ms2/", {
headers: {
"X-Request-ID": req.headers["x-request-id"],
},
});
// when do like this I can get the value in ms2.
有什么辦法嗎,或者就是這樣?
uj5u.com熱心網友回復:
如果您需要對ms2(a) 上特定端點的每個請求執行請求ms1,您可以創建一個 Express 中間件,它至少可以省去每次手動執行請求的麻煩:
const makeMs2Request = async (req, res, next) => {
const response = await fetch("http://ms2/", {
headers: {
"X-Request-ID": req.headers["x-request-id"],
}
);
req.ms2data = await response.json();
next();
});
app.get('/my/endpoint/on/ms1', makeMs2Request, (req, res) => {
// here you can use `req.ms2data`
…
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/505157.html
上一篇:如何禁用Nuxt的內置錯誤處理?
