yarn add @qnpm/qtrace-node-degradation
or
npm install @qnpm/qtrace-node-degradation
使用方式跟@qnpm/qtrace-node一样,没有任何改变 使用方式
注⚠️: 此包使用于node8.0.0以下的项目
yarn add @qnpm/q-fetch-degradation
or
npm install @qnpm/q-fetch-degradation
@qnpm/q-fetch-degradation库的使用需要业务传递当前请求的上下文,如下:
const { axios, request } = require('@qnpm/q-fetch-degradation');
const router = require('koa-router')();
const testAxios = (ctx) => {
// koa,egg等框架取qtraceTofetch方式为 ctx.qtraceTofetch , express框架为 req.qtraceTofetch
axios.get('http://10.93.152.200:8080/test-dubbo',{ qtraceTofetch: ctx.qtraceTofetch }).then(res=>{
}).catch(err=>{
});
}
const testAxiosCreate = (ctx) => {
const instance = axios.create({
baseURL: 'http://10.93.152.200:8080'
});
instance.get('/test-dubbo',{qtraceTofetch:ctx.qtraceTofetch}).then(res=>{
}).catch(err=>{
});
}
const testRequest = (ctx) => {
request({ url:"http://10.93.117.136:8080/test-simple-delay?delay=" + delay,qtraceTofetch: ctx.qtraceTofetch }, function (error, response, body) {});
}
const testRequestGet = (ctx) => {
request.get({url:"http://10.93.117.136:8080/test-simple-delay?delay=" + delay, qtraceTofetch: ctx.qtraceTofetch},function (error, response, body) {})
}
router.get('/', function (ctx, next) {
testAxios(ctx) // axios get
testAxiosCreate(ctx) // axios create
testRequest(ctx) // request
testRequestGet(ctx) // request get
ctx.body = 'test trace fetch';
})
module.exports = router;
注⚠️: koa,egg等框架取qtraceTofetch方式为 ctx.qtraceTofetch , express框架为 req.qtraceTofetch
qtrace中间件 | 请求库 | 适用node版本 |
---|---|---|
@qnpm/qtrace-node | @qnpm/q-fetch | 8.0.0以上 |
@qnpm/qtrace-node-degradation | @qnpm/q-fetch-degradation | 8.0.0以下 |
如果业务 有对请求库做二次封装,且封装的库需要同时在node8.0以上的项目和node8.0以下的项目适用, 则要判断node版本来决定加载 @qnpm/q-fetch库还是 @qnpm/q-fetch-degradation ,如下:
'use strict';
const semver = require('semver');
if(process && semver.gte(process.versions.node, '8.0.0')){
module.exports = require('@qnpm/q-fetch');
}else{
module.exports = require('@qnpm/q-fetch-degradation');
}