目录
- 代码结构
- 重要的类
代码结构
IDEA中,一共30个类文件。
重要的类
1, NacosAutoServiceRegistration.java 继承一个抽象类 AbstractAutoServiceRegistration.java,这个抽象类属于spring-cloud-commons项目。这个抽象类实现了ApplicationListener接口,监听WebServerInitializedEvent事件。web服务器初始化的时候,发布WebServerInitializedEvent事件,此时回调方法生效。
public void onApplicationEvent(WebServerInitializedEvent event) {
this.bind(event);
}
public void bind(WebServerInitializedEvent event) {
ApplicationContext context = event.getApplicationContext();
if (!(context instanceof ConfigurableWebServerApplicationContext) || !"management".equals(((ConfigurableWebServerApplicationContext)context).getServerNamespace())) {
this.port.compareAndSet(0, event.getWebServer().getPort());
this.start();
}
}