Skip to content

feat: 增加balancerFilter优先匹配 #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

tyouzu1
Copy link

@tyouzu1 tyouzu1 commented Apr 22, 2022

#96

在使用registry的情况下,如果有较多的地址,在这里希望能够进行部分地址增加优先级优先掉用

我的需求: 如地址1、2、3 、4、5、6。希望能够先掉用 1、2、3,并且兜底不可用情况,重新走之前的逻辑 随机掉用1-6

所以在此增加一个balancerFilter,在_loadbalancer.select之前先掉用一次,筛选出需要优先掉用的地址,优先连接一次。不可用时使用原逻辑。

例子

const { RpcClient } = require('sofa-rpc-node').client;
const { ZookeeperRegistry } = require('sofa-rpc-node').registry;
const logger = console;

// 1. 创建 zk 注册中心客户端
const registry = new ZookeeperRegistry({
  logger,
  address: '127.0.0.1:2181',
});

async function invoke() {
  // 2. 创建 RPC Client 实例
  const client = new RpcClient({
    logger,
    registry,
  });
  // 3. 创建服务的 consumer
  const consumer = client.createConsumer({
    interfaceName: 'com.nodejs.test.TestService',
    balancerFilter: addressList => {
        // 可以重写addressList,进行filter可以实现优先级功能
        // 如: 优先匹配`host === '127.0.0.1:13202'`的地址
        return addressList.filter(v => v.host === '127.0.0.1:13202');
    },
  });
  // 4. 等待 consumer ready(从注册中心订阅服务列表...)
  await consumer.ready();

  // 5. 执行泛化调用
  const result = await consumer.invoke('plus', [ 1, 2 ], { responseTimeout: 3000 });
  console.log('1 + 2 = ' + result);
}

invoke().catch(console.error);

@tyouzu1
Copy link
Author

tyouzu1 commented Apr 22, 2022

@fengmk2 麻烦帮忙看一下啦,谢谢

@fengmk2 fengmk2 added the enhancement New feature or request label Apr 22, 2022
@fengmk2 fengmk2 self-assigned this Apr 22, 2022
@codecov
Copy link

codecov bot commented Apr 22, 2022

Codecov Report

Merging #97 (55d857d) into master (1921e7d) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #97      +/-   ##
==========================================
+ Coverage   98.31%   98.33%   +0.02%     
==========================================
  Files          38       38              
  Lines        2311     2344      +33     
  Branches      367      378      +11     
==========================================
+ Hits         2272     2305      +33     
  Misses         39       39              
Impacted Files Coverage Δ
lib/client/address_group.js 99.13% <100.00%> (+0.03%) ⬆️
lib/client/loadbalancer/base.js 97.80% <100.00%> (+0.24%) ⬆️
lib/client/loadbalancer/weight_rr.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1921e7d...55d857d. Read the comment docs.

@tyouzu1
Copy link
Author

tyouzu1 commented Aug 17, 2022

@fengmk2 老哥这个还有合进去的可能么

@tyouzu1
Copy link
Author

tyouzu1 commented Aug 17, 2022

@fengmk2 老哥这个还有合进去的可能么

如果可以的话,或者简单点这样,#100 把createLoadBalancer增加到参数里。把createLoadBalancer透出也方便去定制功能

const defaultOptions = {
  loadbalancerClass: 'roundRobin',
  retryFaultInterval: 30000, // 30s 重新尝试连接下不可用的地址
  waitConnect: true,
  createLoadBalancer,
};
class AddressGroup extends Base {
  constructor(options = {}) {
    ...
    this._loadbalancer = options.createLoadBalancer(this);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants