3分钟
模板字符串1
v1.12.9
版本开始,whistle支持类似es6的模板字符串,通过模板字符串可以读取请求的一些信息并设置到规则中:
pattern1 protocol://`xxx${reqCookie.cookieName}yyy`
www.test.com/api http://`${clientIp}:8080`
pattern3 protocol://`{test.json}`
test.json:
{
"url": "${url}",
"port": "${port}",
"version": "${version}",
"search": "${url.search}",
"query": "${url.query}",
"queryValue": "${url.query.name}",
"host": "${url.host}",
"hostname": "${url.hostname}",
"path": "${url.path}",
"pathname": "${url.pathname}",
"reqId": "${reqId}",
"now": ${now},
"method": "${method}",
"xff": "${reqHeaders.x-test}",
"other": "${reqHeaders.other}",
"cookie": "${reqCookie}",
"cookieValue": "${reqCookie.cookieName}",
"clientIp": "${clientIp}"
}
这里 test.json
在规则中一定要用模板字符串引入:
protocol://`{test.json}`
如下配置:
www.test.com/api http://`${clientIp}:8080`
10.12.2.1` 的请求 `https://www.test.com/api/test` 会转成 `http://10.12.2.1:8080/test
学员评价