SpringCloud中NacosNamingService的作用詳解
NacosNamingService初始化
NacosNamingService類(lèi)完成服務(wù)實(shí)例注冊(cè),撤銷(xiāo)與獲取服務(wù)實(shí)例操作。
生成方式:NacosNamingService初始化采用單例模式,使用反射生成。
NacosServiceRegistry bean初始化時(shí),在構(gòu)造中,根據(jù)nacos的屬性配置文件,通過(guò)反射,初始化了NacosNamingService。
NacosServiceRegistry
public NacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) {
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
this.namingService = nacosDiscoveryProperties.namingServiceInstance();
}NacosDiscoveryProperties
public NamingService namingServiceInstance() {
if (null != namingService) {
return namingService;
}
try {
namingService = NacosFactory.createNamingService(getNacosProperties());
}
catch (Exception e) {
log.error("create naming service error!properties={},e=,", this, e);
return null;
}
return namingService;
}NamingFactory
public static NamingService createNamingService(Properties properties) throws NacosException {
try {
Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosNamingService");
Constructor constructor = driverImplClass.getConstructor(Properties.class);
NamingService vendorImpl = (NamingService)constructor.newInstance(properties);
return vendorImpl;
} catch (Throwable e) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);
}
}NacosNamingService初始化過(guò)程
NacosNamingService初始化時(shí),主要是進(jìn)行了以下操作:
- 初始化命名空間,默認(rèn)為public
- 初始化服務(wù)端地址.xxx:8848
- 初始化naocs 服務(wù)端兩個(gè)webcontext,"/nacos//v1/ns"和"/nacos//v1/ns//instance"
- 初始化服務(wù)實(shí)例信息緩存目錄,默認(rèn)為用戶(hù)目錄下"/nacos/naming/public"
- 初始化日志
- 初始化事件分發(fā)器
- 初始化NamingProxy,像服務(wù)端發(fā)起請(qǐng)求的代理
- 初始化心跳信息,使用ScheduledThreadPoolExecutor初始化守護(hù)線(xiàn)程池,nacos默認(rèn)5秒像服務(wù)端發(fā)起一次心跳,
- 初始化主機(jī)信息,默認(rèn)不使用本地緩存。
public NacosNamingService(Properties properties) {
init(properties);
}
private void init(Properties properties) {
namespace = InitUtils.initNamespaceForNaming(properties);
initServerAddr(properties);
InitUtils.initWebRootContext();
initCacheDir();
initLogName(properties);
eventDispatcher = new EventDispatcher();
serverProxy = new NamingProxy(namespace, endpoint, serverList);
serverProxy.setProperties(properties);
beatReactor = new BeatReactor(serverProxy, initClientBeatThreadCount(properties));
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, isLoadCacheAtStart(properties), initPollingThreadCount(properties));
}NacosNamingService心跳啟動(dòng)
客戶(hù)端向服務(wù)端發(fā)起請(qǐng)求時(shí),會(huì)先通過(guò)定時(shí)任務(wù)啟動(dòng)心跳,默認(rèn)心跳為5秒一次
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
if (instance.isEphemeral()) {
BeatInfo beatInfo = new BeatInfo();
beatInfo.setServiceName(NamingUtils.getGroupedName(serviceName, groupName));
beatInfo.setIp(instance.getIp());
beatInfo.setPort(instance.getPort());
beatInfo.setCluster(instance.getClusterName());
beatInfo.setWeight(instance.getWeight());
beatInfo.setMetadata(instance.getMetadata());
beatInfo.setScheduled(false);
long instanceInterval = instance.getInstanceHeartBeatInterval();
beatInfo.setPeriod(instanceInterval == 0 ? DEFAULT_HEART_BEAT_INTERVAL : instanceInterval);
//啟動(dòng)心跳任務(wù)
beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo);
}
serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance);
}public void addBeatInfo(String serviceName, BeatInfo beatInfo) {
NAMING_LOGGER.info("[BEAT] adding beat: {} to beat map.", beatInfo);
String key = buildKey(serviceName, beatInfo.getIp(), beatInfo.getPort());
BeatInfo existBeat = null;
//fix #1733
if ((existBeat = dom2Beat.remove(key)) != null) {
existBeat.setStopped(true);
}
dom2Beat.put(key, beatInfo);//開(kāi)啟定時(shí)心跳任務(wù)
executorService.schedule(new BeatTask(beatInfo), beatInfo.getPeriod(), TimeUnit.MILLISECONDS);
MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size());
}獲取服務(wù)實(shí)例
@Override
public List<Instance> getAllInstances(String serviceName, String groupName, List<String> clusters, boolean subscribe) throws NacosException {
ServiceInfo serviceInfo;
if (subscribe) {
serviceInfo = hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","));//本地map獲取
} else {
serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","));//遠(yuǎn)程服務(wù)獲取
}
List<Instance> list;
if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
return new ArrayList<Instance>();
}
return list;
}服務(wù)實(shí)例注銷(xiāo)
@Override
public void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException {
if (instance.isEphemeral()) {
beatReactor.removeBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), instance.getIp(), instance.getPort());
}
serverProxy.deregisterService(NamingUtils.getGroupedName(serviceName, groupName), instance);
}到此這篇關(guān)于SpringCloud中NacosNamingService的作用詳解的文章就介紹到這了,更多相關(guān)NacosNamingService的作用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloud實(shí)現(xiàn)全鏈路灰度發(fā)布的示例詳解
- SpringCloud中的Eureka注冊(cè)中心詳細(xì)解讀
- SpringCloud中的Ribbon負(fù)載均衡詳細(xì)解讀
- SpringCloud?Gateway實(shí)現(xiàn)請(qǐng)求解密和響應(yīng)加密的過(guò)程解析
- SpringCloud中的OpenFeign調(diào)用解讀
- SpringCloudAlibaba Nacos開(kāi)啟鑒權(quán)解決跳過(guò)登錄頁(yè)面問(wèn)題
- SpringCloud?Feign集成AOP的常見(jiàn)問(wèn)題與解決
相關(guān)文章
解決ApplicationContext獲取不到Bean的問(wèn)題
這篇文章主要介紹了解決ApplicationContext獲取不到Bean的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
springboot swagger不顯示接口的問(wèn)題及解決
這篇文章主要介紹了springboot swagger不顯示接口的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
SpringBoot 使用Mybatis分頁(yè)插件實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringBoot 使用Mybatis分頁(yè)插件實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
idea報(bào)錯(cuò):程序包org.springframework.web.bind.annotation不存在
在用本地的maven倉(cāng)庫(kù)的時(shí)候會(huì)org.springframework.web.bind.annotation不存在的錯(cuò)誤,本文就詳細(xì)的介紹一下解決方法,感興趣的可以了解下2023-08-08
使用Runtime 調(diào)用Process.waitfor導(dǎo)致的阻塞問(wèn)題
這篇文章主要介紹了使用Runtime 調(diào)用Process.waitfor導(dǎo)致的阻塞問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
適用于Java初學(xué)者的學(xué)習(xí)路線(xiàn)圖
這篇文章主要介紹了學(xué)習(xí)Java的路線(xiàn)圖的五個(gè)必經(jīng)階段,還有一些作者的想法分享給大家,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

