vein-rpc

Vein RPC远程方法调用模块

Usage no npm install needed!

<script type="module">
  import veinRpc from 'https://cdn.skypack.dev/vein-rpc';
</script>

README

Vein-RPC

介绍

橡芮科技Vein RPC远程功能调用模块,在同一局域网中可自动发现Master节点

模块划分

Master模块

RPC服务管理模块,用于RPC中心节点

Provider模块

RPC服务提供模块,用于提供RPC方法供服务消费者调用

Consumer模块

RPC服务消费模块,可对在Master中心节点中注册的远程服务方法进行调用

代码示例

Master
(async () => {
    const master = new Master({port: 65388, token: 'test-token-not-necessary'})
    await master.launch()
    console.log('master launched')
})()
Provider
(async () => {
    const functionMap = new Map()
    functionMap.set({a: true, b: false}, async (data) => {
        return data.x + data.y
    })

    const provider = new Provider({
        patternFunctionMap: functionMap,
        serviceName: 'com.example.service1',
        port: 65388,
        token: 'test-token-not-necessary'
    })
    console.log('Provider ready')
})()
Consumer
(async () => {
    const consumer = new Consumer({port: 65388, token: 'test-token-not-necessary'})
    const res = await consumer.invoke('com.example.service1', {
        a: true,
        b: false,
        x: 10,
        y: 99
    }, {
        allowRetry: true,
        maxRetry: 10,
        retryDelay: 100,
        ignoreConnectionError: true,
        timeout: 300000,
        offlinePending: true
    })
    console.log('result', res)
})()

Class

Master
new Master([options])
  • options?: { token?: string, port?: number }
master.fetchProviders()

获取服务中心节点所持有的Providers信息

Provider
new Provider(options)
  • options: { serviceName: string, patternFunctionMap: PatternFunctionMap, registries?: Array | string, token?: string, port?: number }
Type 数据类型 说明
Pattern { [key: string]: any }
PatternFunctionMap Map<Pattern, (input: { [key: string]: any }) => Promise>
provider.fetchRegistries()

获取服务提供者所连接的Registries信息

Consumer
new Consumer([options])
  • options?: { registries?: Array | string, port?: number, token?: string }
consumer.registryInfo()

获取服务消费者当前连接的Registries信息

async consumer.invoke(serviceName, data [, options])
  • serviceName: string

  • data: { [key: string]: any }

  • options: InvokeOptions

选项 数据类型 默认值 说明
allowRetry boolean true 是否允许请求失败时重试
maxRetry number 10 最大重试次数
ignoreConnectionError boolean true 是否忽略连接错误
retryDelay number 100 重试时间间隔(毫秒)
timeout number 300000 请求超时时间(毫秒)
offlinePending boolean true 是否在未连接至Registry时将操作搁置

注意事项

  • 在使用PM2的cluster模式时可能会出现未知的问题