Nacos - 啓動中提到了註冊的入口,這裏就講一下注冊的細節。
Tomcat啓動成功後,會調用AbstractAutoServiceRegistration的onApplicationEvent方法,他會繼續調用AbstractAutoServiceRegistration#bind。git
public void bind(WebServerInitializedEvent event) { ApplicationContext context = event.getApplicationContext(); if (context instanceof ConfigurableWebServerApplicationContext) { if ("management".equals(((ConfigurableWebServerApplicationContext) context) .getServerNamespace())) { return; } } // 設置端口號 this.port.compareAndSet(0, event.getWebServer().getPort()); this.start(); }
public void start() { if (!isEnabled()) { if (logger.isDebugEnabled()) { logger.debug("Discovery Lifecycle disabled. Not starting"); } return; } // only initialize if nonSecurePort is greater than 0 and it isn't already running // because of containerPortInitializer below // 沒啓動過才註冊 if (!this.running.get()) { // 發佈InstancePreRegisteredEvent事件 this.context.publishEvent( new InstancePreRegisteredEvent(this, getRegistration())); // 註冊 register(); if (shouldRegisterManagement()) { // 註冊registerManagement registerManagement(); } // 發佈InstanceRegisteredEvent事件 this.context.publishEvent( new InstanceRegisteredEvent<>(this, getConfiguration())); // 設置狀態爲啓動 this.running.compareAndSet(false, true); } }
protected void register() { if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) { log.debug("Registration disabled."); return; } // 小於0從新設置端口 if (this.registration.getPort() < 0) { this.registration.setPort(getPort().get()); } super.register(); }
registration的注入在啓動的時候已經說過了。github
protected void register() { this.serviceRegistry.register(getRegistration()); }
封裝好Instance後,調用NacosNamingService的registerInstance方法註冊。Nacos - NacosNamingService初始化以及幾個其餘定時任務已經講過了。spring
public void register(Registration registration) { if (StringUtils.isEmpty(registration.getServiceId())) { log.warn("No service to register for nacos client..."); return; } // 獲取NacosNamingService NamingService namingService = namingService(); // 獲取serviceId String serviceId = registration.getServiceId(); String group = nacosDiscoveryProperties.getGroup(); // 獲取Instance,一些信息從registration讀取,一些從nacosDiscoveryProperties讀取 Instance instance = getNacosInstanceFromRegistration(registration); try { // 註冊 namingService.registerInstance(serviceId, group, instance); log.info("nacos registry, {} {} {}:{} register finished", group, serviceId, instance.getIp(), instance.getPort()); } catch (Exception e) { log.error("nacos registry, {} register failed...{},", serviceId, registration.toString(), e); // rethrow a RuntimeException if the registration is failed. // issue : https://github.com/alibaba/spring-cloud-alibaba/issues/1132 rethrowRuntimeException(e); } }
封裝心跳信息、開啓定時任務續約以及調用serverProxy註冊,這個url是/nacos/v1/ns/instance。segmentfault
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException { String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName); if (instance.isEphemeral()) { // 封裝心跳信息 BeatInfo beatInfo = beatReactor.buildBeatInfo(groupedServiceName, instance); // 開啓定時任務續約 beatReactor.addBeatInfo(groupedServiceName, beatInfo); } serverProxy.registerService(groupedServiceName, groupName, instance); }