@rockerjs/rpc
v1.0.2
Published
RPC for Rockerjs, including Dubbo and HTTP client
Downloads
31
Readme
业务方定义接口
Http接口定义可用装饰器
- Resource
业务方在任意位置定义需要使用到的接口,依赖 @vdian/rpc 包提供的 Resource、Resource 装饰器。 Resource唯一参数为用户自定义选项(object),具体可查看 request 官方文档中的参数 https://github.com/request/request#requestoptions-callback。 例:
import { Resource } from '@vdian/rpc';
export abstract class HttpRequest {
@Resource({ url: '/promotion/item.auctionForWinner/1.0' })
auctionForWinner(activityId: number, itemId: number, bizType: string): any {}
}
// 目前 thor 的传参结构是 param: JSON.stringify({你的参数})。
// 中间件在这里做了一层封装,因此业务方定义的参数实际上是在 param 中需要传递的参数。
// 仔细检查,切勿写错参数名。
Dubbo接口定义可用装饰器
- Resource
- Param 所有参数必须调用 Param 传递类型!
Resource 装饰器的用法
import { Resource } from '@vdian/rpc';
// Resource 定义服务端类目、version(默认 1.0.0)
@Resource({service: 'com.vdian.DemoService', version: '1.0.0'})
export class DubboTest {
getName() {}
}
Param 装饰器定义
type paramDefined = {
type?: string,
key?: {
[propName: string]: paramDefined
} | string,
detail?: {
[propName: string]: paramDefined
} | string
} | string
Param 装饰器的几种用法
注:List内的Long类型必须定义,js整型默认会被转化为int
- 参数为基本类型
import { Resource, Param } from '@vdian/rpc';
@Resource({service: 'com.vdian.DemoService', version: '1.0.0'})
export class DubboTest {
getNameById(@Param("Long") id: number) {}
}
- 参数为封装类型时
import { Resource, Param } from '@vdian/rpc';
type demoParamInTs = {
userId: number,
id: number,
type: string
}
@Resource({service: 'com.vdian.DemoService', version: '1.0.0'})
export class DubboTest {
getNameByDetail(@Param('com.vdian.demoParam') param: demoParamInTs){}
}
- 参数为 List,内部为 Long、封装类
import { Resource, Param } from '@vdian/rpc';
@Resource({service: 'com.vdian.DemoService', version: '1.0.0'})
export class DubboTest {
getNamesByIds(@Param({type: "List", detail: "Long"}) param: Array<number>){}
getNamesByIds(@Param({type: "List", detail: "com.vdian.demoParamInner"}) param: any){}
}
- 参数为嵌套封装类
import { Resource, Param } from '@vdian/rpc';
@Resource({service: 'com.vdian.DemoService', version: '1.0.0'})
export class DubboTest {
getNamesByIds(@Param({
type: "com.vdian.demoParam",
detail: {
innerParam: "com.vdian.demoParamInner"
}
}) param: Array<number>){}
}
- 参数为List,内部为嵌套封装类。使用 * 标记 List 内所有字段
import { Resource, Param } from '@vdian/rpc';
@Resource({service: 'com.vdian.DemoService', version: '1.0.0'})
export class DubboTest {
getNamesByIds(@Param({type: "List", detail: {
"*": {
type: "com.vdian.demoParamInner"
detail: {
id: "Long"
}
}
}}) param: any){}
}
- 参数为 HashMap, 键需指定类型
import { Resource, Param } from '@vdian/rpc';
@Resource({service: 'com.vdian.DemoService', version: '1.0.0'})
export class DubboTest {
beanHashMap(@Param({
type: "HashMap",
key: "com.vdian.DemoParamKey"
}) customerObj: any) {}
}
初始化配置
Rpc.config(dubboConfig, httpConfig?, trace?)
dubboConfig
查询服务信息 http://admin.dubbo.daily.vdian.net/governance/services#
type dubboConfig = {
application: {name: "当前应用名"},
dubboVer: "调用远程服务器上的 dubbo 版本"
dependencies: ["业务方定义的接口"]
env: "daily | pre | prod 默认 daily"
}
httpConfig
type {
interface: Function | Array<Function> | Array<{service: Function, baseUrl?: string}>,
baseUrl?: string
}
例:
import { DubboTest } from './interface/dubboInterface';
import { HttpRequest } from './interface/httpInterface';
Rpc.config({
application: {name: 'testServices'},
dubboVer: '3.0.4',
dependencies: [DubboTest],
env: 'daily'
}, {
baseUrl: 'https://thor.weidian.com',
interface: HttpRequest,
})
调用 API
import { Get, Param, Request } from '@vdian/rocker-mvc';
import { Inject} from '@vdian/rocker';
import { DubboTest } from './interface/dubboInterface';
export default class Biz{
@Inject
private dubboTest: DubboTest
@Get({ url: '/dubbo', render: './template/index.vue' })
async get(@Request _ctx) {
const customerObj = {
userId: "123456",
appId: "wdbuyer",
pubType: 1,
weexSDKVersion: "5.1.0"
};
const res = await this.dubboTest.getWeexUrl(customerObj)
}
}