經過內容尋址的方式,獲取接口的惟一hash,這樣就能夠實現像本地函數同樣的使用遠程接口。node
import { Observable } from 'ims-rxjs';
export abstract class ImsDemo {
abstract getIndex(name: string): Observable<any>;
abstract getIndex2(name: string): Observable<any>;
}
複製代碼
import { Injectable, Logger } from 'ims-core';
import { of } from 'ims-rxjs';
@Injectable()
export class ImsDemoImplClient extends ImsDemo {
constructor(public logger: Logger) {
super();
}
getIndex(name: string) {
this.logger.info(`request ImsDemo getIndex: ${name}`);
return of({
demo: 'test',
name: name,
});
}
getIndex2(name: string) {
this.logger.info(`request ImsDemo getIndex2: ${name}`);
return of({
demo: 'test',
name: name,
});
}
}
// 服務端大致過程
// 1.將ImsDemo提取hash特徵,獲取服務端地址hash
// 2.實例化接口實例
// 3.註冊服務地址
// 4.返回服務處理結果
let instance = new ImsDemoImplClient(logger)
app.use(`/${hash}/:method`,(req,res,next)=>{
let method = req.params.method;
let result = instance[method](...req.body);
res.end(result)
})
複製代碼
// 1.將ImsDemo提取hash特徵,獲取服務端地址
// 2.拼接地址`/${hash}/${method}`
// 3.發送請求,請求附帶參數是params
// 4.收到回覆獲得結果
injector.get(ImsDemo).getIndex('ims').subscribe(res=>{
console.log('http請求結果是',res)
})
複製代碼