Skip to content
Permalink
Browse files
context支持自定义的扩展attachments
  • Loading branch information
hufeng committed Jul 7, 2018
1 parent 6953a64 commit dd58b1b042ec3e7af2c6038a7fb6c18d20f5b120
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 29 deletions.
@@ -26,3 +26,16 @@ const dubbo = (module.exports = new Dubbo({
console.log('end makecostTime->', endTime - startTime);
});
*/

// dubbo.use(async function trace(ctx, next) {
// const uuid = Date.now();
// ctx.attachments = {
// uuid,
// };

// ctx.attachments = {
// userId: uuid,
// };

// await next();
// });
@@ -34,7 +34,6 @@ public String sayHello(String name) {

@Override
public String echo() {
System.out.println("receive....");
return "pang";
}

@@ -16,7 +16,7 @@
*/

import {dubboInvoker, matcher} from 'dubbo-invoker';
import {Dubbo, java} from 'dubbo2.js';
import {Context, Dubbo, java} from 'dubbo2.js';
import {BasicTypeProvider} from './providers/com/alibaba/dubbo/demo/BasicTypeProvider';
import {DemoProvider} from './providers/com/alibaba/dubbo/demo/DemoProvider';
import {ErrorProvider} from './providers/com/alibaba/dubbo/demo/ErrorProvider';
@@ -37,18 +37,32 @@ const dubbo = new Dubbo<typeof service>({
});

//use middleware
dubbo.use(async function test(ctx, next) {
dubbo.use(async function costtime(ctx, next) {
const startTime = Date.now();
await next();
const endTime = Date.now();
const {
request: {dubboInterface, methodName},
} = ctx;

console.log(
`invoke ${dubboInterface}#${methodName} costTime: ${endTime - startTime}`,
);
});

dubbo.use(async function trace(ctx: Context, next) {
const uuid = Date.now();
//auto merge attchments when assign more
ctx.attachments = {
uuid,
};

ctx.attachments = {
userId: uuid,
};
await next();
});

//dubbo-invoker set version
dubbo.use(
dubboInvoker(
@@ -25,14 +25,14 @@ const log = debug('dubbo:context');
export default class Context<T = any> {
private constructor() {
log('new Context');

//trace id
this._uuid = '';
this._pid = NO_PID;
this._timeoutId = null;
this._isSupportedDubbox = false;
this._body = {err: null, res: null};
this._application = {name: 'dubbo2.js'};
this._attachments = {};
this._request = <IContextRequestParam>{
requestId: id(),
};
@@ -93,13 +93,10 @@ export default class Context<T = any> {
*/
private _pid: number;

private static _checkHessianParam(param: any): param is IHessianType {
return (
typeof param === 'object' &&
typeof param['$class'] !== 'undefined' &&
typeof param['$'] !== 'undefined'
);
}
/**
* 扩展attachments,支持自定义一些属性可以放在dubbo的encoder底层协议的attachment字段中
*/
private _attachments: Object;

static create<T = any>() {
return new Context<T>();
@@ -299,4 +296,32 @@ export default class Context<T = any> {
get isSupportedDubbox() {
return this._isSupportedDubbox;
}

//=====================attachments=======================
/**
* 设置当前的attachments
* @param key
* @param value
*/
set attachments(param: Object) {
log('set attachments->%o', param);
//auto merge
this._attachments = {...this._attachments, ...param};
}

/**
* 获取当前的attachments
*/
get attachments(): Object {
return this._attachments;
}

//===============private method==========================
private static _checkHessianParam(param: any): param is IHessianType {
return (
typeof param === 'object' &&
typeof param['$class'] !== 'undefined' &&
typeof param['$'] !== 'undefined'
);
}
}
@@ -131,6 +131,7 @@ export default class Dubbo<TService = Object>
//proxy methods
Object.keys(methods).forEach(name => {
proxyObj[name] = async (...args: any[]) => {
log('%s create context', name);
//创建dubbo调用的上下文
const ctx = Context.create();
ctx.application = application;
@@ -20,6 +20,7 @@ import Hessian from 'hessian.js';
import {toBytes8} from './byte';
import Context from './context';
import {DubboEncodeError} from './err';
import {isDevEnv} from './util';

const log = debug('dubbo:hessian:encoderV2');

@@ -45,10 +46,12 @@ const DUBBO_DEFAULT_PAY_LOAD = 8 * 1024 * 1024; // 8M
export default class DubboEncoder {
constructor(ctx: Context) {
this._ctx = ctx;
log(
'dubbo encode param request:%s',
JSON.stringify(this._ctx.request, null, 2),
);
if (isDevEnv) {
log(
'dubbo encode param request:%s',
JSON.stringify(this._ctx.request, null, 2),
);
}
}

private readonly _ctx: Context;
@@ -205,34 +208,37 @@ export default class DubboEncoder {
group,
timeout,
version,
uuid,
application: {name},
attachments,
} = this._ctx;

//merge dubbo attachments and customize attachments
const map = {
path: path,
interface: dubboInterface,
version: version || '0.0.0',
...{
path: path,
interface: dubboInterface,
version: version || '0.0.0',
},
...attachments,
};

group && (map['group'] = group);
timeout && (map['timeout'] = timeout);
//全链路跟踪
log(`trace uuid-> ${uuid}`);
uuid && (map['QM_UUID'] = uuid);
name && (map['application'] = name);

let attachments = {
let attachmentsHashMap = {
$class: 'java.util.HashMap',
$: map,
};

log(
'request#%d attachment %s',
requestId,
JSON.stringify(attachments, null, 2),
);
if (isDevEnv) {
log(
'request#%d attachment %s',
requestId,
JSON.stringify(attachmentsHashMap, null, 2),
);
}

return attachments;
return attachmentsHashMap;
}
}

0 comments on commit dd58b1b

Please sign in to comment.