diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md index 33d4682ca..9420eefad 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md @@ -154,7 +154,7 @@ Nacos Discovery Starter 默认集成了 Ribbon ,所以对于使用了 Ribbon ### 服务注册 Spring Cloud Nacos Discovery 遵循了 spring cloud common 标准,实现了 AutoServiceRegistration、ServiceRegistry、Registration 这三个接口。 -在 spring cloud 应用的启动阶段,监听了 ServletWebServerInitializedEvent 事件,当Web容器初始化完成后,即收到 ServletWebServerInitializedEvent 事件后,会触发注册的动作,调用 ServiceRegistry 的 register 方法,将服务注册到 Nacos Server。 +在 spring cloud 应用的启动阶段,监听了 WebServerInitializedEvent 事件,当Web容器初始化完成后,即收到 WebServerInitializedEvent 事件后,会触发注册的动作,调用 ServiceRegistry 的 register 方法,将服务注册到 Nacos Server。 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md index cd6e4bcfe..355cf19ae 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md @@ -158,7 +158,7 @@ The code of `nacos-discovery-consumer-example` project will be analyzed below, d Spring Cloud Nacos Discovery follows the spring cloud common standard and implements three interfaces: AutoServiceRegistration, ServiceRegistry, and Registration. -During the startup phase of the spring cloud application, the ServletWebServerInitializedEvent event is watched. When the ServletWebServerInitializedEvent event is received after the Web container is initialized, the registration action is triggered, and the ServiceRegistry register method is called to register the service to the Nacos Server. +During the startup phase of the spring cloud application, the WebServerInitializedEvent event is watched. When the WebServerInitializedEvent event is received after the Web container is initialized, the registration action is triggered, and the ServiceRegistry register method is called to register the service to the Nacos Server. diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistration.java index 8459b11c2..8802edd6e 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistration.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistration.java @@ -22,6 +22,8 @@ import java.util.concurrent.atomic.AtomicInteger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext; +import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent; import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistration; @@ -102,13 +104,15 @@ public class NacosAutoServiceRegistration return this.order; } - @EventListener(ServletWebServerInitializedEvent.class) - public void onApplicationEvent(ServletWebServerInitializedEvent event) { + @EventListener(WebServerInitializedEvent.class) + public void onApplicationEvent(WebServerInitializedEvent event) { int localPort = event.getWebServer().getPort(); - if (this.port.get() == 0) { + ApplicationContext context = event.getApplicationContext(); + + if(!(context instanceof ConfigurableWebServerApplicationContext) || !"management".equals(((ConfigurableWebServerApplicationContext)context).getServerNamespace())) { logger.info("Updating port to {}", localPort); - this.port.compareAndSet(0, localPort); - start(); + this.port.compareAndSet(0, event.getWebServer().getPort()); + this.start(); } }