Skip to content

自定义服务发现实现

zōng yǔ edited this page Nov 23, 2018 · 2 revisions

开发自定义 Registry

自定义 Registry 需要实现下面一些接口

// simple_registry.js
'use strict';

const Base = require('sdk-base');

class SimpleRegistry extends Base {
  constructor(options) {
    super(options);
    this.ready(true);
  }

  async register(config) { ... }

  async unRegister(config) { ... }

  subscribe(config, listener) { ... }

  unSubscribe(config, listener) { ... }

  close() { ... }
}

module.exports = SimpleRegistry;

然后用 cluster-client 封装起来

'use strict';

const RegistryBase = require('sofa-rpc-node/lib/registry/base');
const DataClient = require('./simple_registry');

class ClusterRegistry extends RegistryBase {
  get DataClient() {
    return DataClient;
  }
}

module.exports = ClusterRegistry

覆盖默认的 Registry

exports.sofaRpc = {
  registryClass: require('your-registry'),
  registry: {
    // 这里可以配置自定义的配置,这个节点不能为 null
  },
};

例子

https://github.com/eggjs/egg-sofa-rpc/tree/72709adbb81645b412e6fb39a7c2c697f9c6c894/test/fixtures/apps/custom-registry