Nacos源码NacosAutoServiceRegistration
生活随笔
收集整理的這篇文章主要介紹了
Nacos源码NacosAutoServiceRegistration
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
NacosAutoServiceRegistration源碼如圖:
可以看到在初始化時,其父類AbstractAutoServiceRegistration也被初始化了。
AbstractAutoServiceRegistration如圖:
可以看到它實現了ApplicationListener接口,監聽Spring容器啟動過程中的事件。
在監聽到WebServerInitializedEvent(web服務初始化完成)的事件后,執行了bind 方法。
其中的bind方法如下:
public void bind(WebServerInitializedEvent event) {// 獲取 ApplicationContextApplicationContext context = event.getApplicationContext();// 判斷服務的 namespace,一般都是nullif (context instanceof ConfigurableWebServerApplicationContext) {if ("management".equals(((ConfigurableWebServerApplicationContext) context).getServerNamespace())) {return;}}// 記錄當前 web 服務的端口this.port.compareAndSet(0, event.getWebServer().getPort());// 啟動當前服務注冊流程this.start(); }?其中的start方法流程:
public void start() {if (!isEnabled()) {if (logger.isDebugEnabled()) {logger.debug("Discovery Lifecycle disabled. Not starting");}return;}// 當前服務處于未運行狀態時,才進行初始化if (!this.running.get()) {// 發布服務開始注冊的事件this.context.publishEvent(new InstancePreRegisteredEvent(this, getRegistration()));// ☆☆☆☆開始注冊☆☆☆☆register();if (shouldRegisterManagement()) {registerManagement();}// 發布注冊完成事件this.context.publishEvent(new InstanceRegisteredEvent<>(this, getConfiguration()));// 服務狀態設置為運行狀態,基于AtomicBooleanthis.running.compareAndSet(false, true);}}?其中最關鍵的register()方法就是完成服務注冊的關鍵,代碼如下:
protected void register() {this.serviceRegistry.register(getRegistration()); }此處的this.serviceRegistry就是NacosServiceRegistry:
總結
以上是生活随笔為你收集整理的Nacos源码NacosAutoServiceRegistration的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nacos源码NacosServiceR
- 下一篇: Nacos源码NacosServiceR