packageorg.springframework.cloud.client.serviceregistrypublicabstractclassAbstractAutoServiceRegistration<RextendsRegistration>implementsAutoServiceRegistration,ApplicationContextAware,ApplicationListener<WebServerInitializedEvent>{// ...忽略一万行//1、监听服务web服务初始化事件@Override@SuppressWarnings("deprecation")publicvoidonApplicationEvent(WebServerInitializedEvent event){bind(event);}@Deprecatedpublicvoidbind(WebServerInitializedEvent event){ApplicationContext context = event.getApplicationContext();if(context instanceofConfigurableWebServerApplicationContext){if("management".equals(((ConfigurableWebServerApplicationContext) context).getServerNamespace())){return;}}this.port.compareAndSet(0, event.getWebServer().getPort());//2、调用启动方法this.start();}publicvoidstart(){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 belowif(!this.running.get()){this.context.publishEvent(newInstancePreRegisteredEvent(this,getRegistration()));//3、注册register();if(shouldRegisterManagement()){registerManagement();}this.context.publishEvent(newInstanceRegisteredEvent<>(this,getConfiguration()));this.running.compareAndSet(false,true);}}protectedvoidregister(){//4忽略几步调用,调用具体的注册实现,这里是NacosServiceRegistrythis.serviceRegistry.register(getRegistration());}}