0.3.0 • Published 3 years ago

brick-cluster-client v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

brick-cluster-client

基于brick-enginecluster-client工具包.用于给brick-engine多进程应用程序提供Leader/Follower功能支持.

+--------+   +--------+
| Client |   | Client |   ...
+--------+   +--------+
    |  \     /   |
    |    \ /     |
    |    / \     |
    |  /     \   |
+--------+   +--------+
| Server |   | Server |   ...
+--------+   +--------+

Install

npm install --save brick-cluster-client

Usage

const { defineProviderFactory } = require('brick-engine');
const {RegistryClient, clusterClientSetup, defineListener } = require('brick-cluster-client');

class Agent {
  /**
   * 代理进程模块构建函数
   * @param {RegistryClient} registry
   */
  constructor(registry) {
    this.registry = registry;
  }

  onAgentEvent(reg) {
    console.log(`Agent Event ${JSON.stringify(reg)}`);
    this.registry.publish({ eventName: 'worker', msg: 'publish with agent' });
  }

}

exports.Agent = Agent;

// 定义Agent对象构建工厂
defineProviderFactory(Agent, { deps: [{ id: RegistryClient }] });

// 定义绑定监听事件
defineListener(Agent, { reg: { eventName: 'agent' }, method: 'onAgentEvent' });

// 安装RegistryClient,以及监听器插件
clusterClientSetup(Agent, { registry: { isLeader: true, isBroadcast: true } });

Documentations

使用jsdoc生成注释文档

git clone https://github.com/kiba-zhao/brick-cluster-client.git
cd brick-cluster
npm install
npm run docs
open docs/index.html

License

MIT