1.0.8 • Published 4 years ago

vein-rpc v1.0.8

Weekly downloads
2
License
MIT
Repository
-
Last release
4 years ago

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 }
PatternFunctionMapMap<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

选项数据类型默认值说明
allowRetrybooleantrue是否允许请求失败时重试
maxRetrynumber10最大重试次数
ignoreConnectionErrorbooleantrue是否忽略连接错误
retryDelaynumber100重试时间间隔(毫秒)
timeoutnumber300000请求超时时间(毫秒)
offlinePendingbooleantrue是否在未连接至Registry时将操作搁置

注意事项

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