1.SharedLoadBalancer(load_balancer.h):包含LoadBalancer指针_lb,AddServersInBatch
2.LoadBalancerWithNaming:继承SharedLoadBalancer和NamingServiceWatcher
2.1Init函数:SharedLoadBalancer::Init,new一个load balance对象(服务发现线程,也是操作actions->owner是NamingServiceThread线程对象->watchers->LoadBalanceWithNamingService(继承NamingServiceWatcher和SharedLoadBalancer)->OnAddedServers->AddServersInBatch(LoadBalance对象))
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->Start(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文章来源:https://www.toymoban.com/news/detail-815123.html
{
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);
}
}
}
10.RandomizedLoadBalancer:继承LoadBalancer文章来源地址https://www.toymoban.com/news/detail-815123.html
到了这里,关于brpc负载均衡load balance和服务发现name servicing的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!