Commit 473eb18d authored by 孙傲's avatar 孙傲

支持直连k8s service

parent 8486d785
{
"name": "nodejs-yitong-sdk",
"version": "1.0.5",
"version": "1.0.7",
"description": "",
"main": "index.js",
"scripts": {
......
......@@ -28,8 +28,6 @@ class ApiError extends Error {
}
}
/**
* YiTong SDK HTTP
*/
......@@ -43,19 +41,42 @@ const http = axios.create({
httpAgent: new require('http').Agent({ keepAlive: true }),
httpsAgent: new require('https').Agent({ keepAlive: true })
});
// koa opentracing
// https://github.com/fapspirit/axios-opentracing
http.interceptors.request.use(function (config) {
if(global.serviceMap) {
let paths = config.url.split("/")
if(paths.length >= 4){
let pathKey = paths[0] + "//" + paths[2] + "/" + paths[3] + "/"
if(global.serviceMap[pathKey]) {
config.url = config.url.replace(pathKey, global.serviceMap[pathKey])
// URL转换 域名 转 k8s serviceName
if (process.env.YT_SERVICE_RULES) {
const { serviceRuleMap } = require(process.env.YT_SERVICE_RULES);
if (_.size(serviceRuleMap) > 0) {
let rules = new Map();
for (const key of Object.keys(serviceRuleMap)) {
const value = serviceRuleMap[key];
rules.set(key, value);
if (key.startsWith("http://")) {
rules.set(`https://${key.substring(7)}`, value);
}
}
let rulesCache = new Map();
http.interceptors.request.use(function (config) {
let url = config.url;
if (url) {
let newUrl = rulesCache.get(config.url);
if (!newUrl) {
for (const [key, value] of rules) {
if (url.startsWith(key)) {
newUrl = `${value}${url.substring(key.length)}`
rulesCache.set(url, newUrl)
}
}
}
if (newUrl) {
config.url = newUrl;
}
}
return config;
});
}
}
// koa opentracing
// https://github.com/fapspirit/axios-opentracing
http.interceptors.request.use(function (config) {
if (config.context && config.context.span) {
let tracer = opentracing.globalTracer();
let span = tracer.startSpan(`${config.method}: ${config.url}`, {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment