本文主要是介绍brpc负载均衡load balance和服务发现name servicing,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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*>::iteratorit = _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);}}}
这篇关于brpc负载均衡load balance和服务发现name servicing的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!