brpc负载均衡load balance和服务发现name servicing

发布时间:2024年01月22日

1.SharedLoadBalancer(load_balancer.h):包含LoadBalancer指针_lb,AddServersInBatch

2.LoadBalancerWithNaming:继承SharedLoadBalancer和NamingServiceWatcher

2.1Init函数:SharedLoadBalancer::Init,new一个load balance对象

2.2GetNamingServiceThread函数:(details/naming_service_thread.cpp):

2.2.0生成一个naming service对象

const NamingService* source_ns = NamingServiceExtension()->Find(protocol);

2.2.1g_nsthread_map如果没有初始化,则创建一个并且初始化

2.2.2查看当前g_nsthread_map是否创建过service_name和protocol的为hash key的线程,没有创建过new_thread为true,创建一个NamingServiceThread(主要成员变量是actions和_watchers)

2.2.3nsthread->Star(sources_ns->New),bthread_start_urgent创建一个协程RunThis->Run->_ns->RunNamingService,_ns是NamingService的对象(可以看下6和7节)

3.Channel:包含LoadBalancerWithNaming的指针_lb,new?LoadBalancerWithNaming,lb->Init

4.RandomizedLoadBalancer:继承LoadBalancer,包含_db_servers

5.NamingService

6.PeriodicNamingService:继承NamingService,方法RunNamingService是一个死循环

?actions->ResetServers(servers);

7.BaiduNamingService:继承PeriodicNamingService,实现GetServers方法

8.NamingServiceThread:(details/naming_service_thread.cpp)

? ? Actions _actions;
? ? std::map<NamingServiceWatcher*, const NamingServiceFilter*> _watchers;

主要函数:AddWatcher()

9.Actions:(details/naming_service_thread.cpp)

_owner:存储的是NamingServiceThread对象指针,在NamingServiceThread构造函数设置_actions(this)

NamingServiceThread::Actions::ResetServers函数中:这里面的owner->watcher就是LoadBalancerWithNaming对象,OnAddedServers->AddServersInBatch,OnRemovedServers->RemoveServersInBatch

    {
        BAIDU_SCOPED_LOCK(_owner->_mutex);
        _last_servers.swap(_servers);
        _owner->_last_sockets.swap(_sockets);
        for (std::map<NamingServiceWatcher*,
                      const NamingServiceFilter*>::iterator
                 it = _owner->_watchers.begin();
             it != _owner->_watchers.end(); ++it) {
            if (!_removed_sockets.empty()) {
                it->first->OnRemovedServers(removed_ids);
            }

            std::vector<ServerId> added_ids;
            ServerNodeWithId2ServerId(_added_sockets, &added_ids, it->second);
            if (!_added_sockets.empty()) {
                it->first->OnAddedServers(added_ids);
            }
        }
    }

文章来源:https://blog.csdn.net/INGNIGHT/article/details/135735827
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。