Ignore the WebServerInitializedEvent if the name space of the spring context is management (#7236)

Close#7230
This commit is contained in:
onewe 2021-11-16 11:13:11 +08:00 committed by GitHub
parent 62c778e995
commit e1487164ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -89,6 +89,8 @@ public class ServerMemberManager implements ApplicationListener<WebServerInitial
private static final String SERVER_PORT_PROPERTY = "server.port";
private static final String SPRING_MANAGEMENT_CONTEXT_NAMESPACE = "management";
private static final String MEMBER_CHANGE_EVENT_QUEUE_SIZE_PROPERTY = "nacos.member-change-event.queue.size";
private static final int DEFAULT_MEMBER_CHANGE_EVENT_QUEUE_SIZE = 128;
@ -441,6 +443,12 @@ public class ServerMemberManager implements ApplicationListener<WebServerInitial
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
String serverNamespace = event.getApplicationContext().getServerNamespace();
if (SPRING_MANAGEMENT_CONTEXT_NAMESPACE.equals(serverNamespace)) {
// ignore
// fix#issue https://github.com/alibaba/nacos/issues/7230
return;
}
getSelf().setState(NodeState.UP);
if (!EnvUtil.getStandaloneMode()) {
GlobalExecutor.scheduleByCommon(this.infoReportTask, DEFAULT_TASK_DELAY_TIME);

View File

@ -22,11 +22,15 @@ import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.utils.InetUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import javax.servlet.ServletContext;
@ -54,6 +58,9 @@ public class ServerMemberManagerTest {
@Mock
private EventPublisher eventPublisher;
@Mock
private WebServerInitializedEvent mockEvent;
private ServerMemberManager serverMemberManager;
private static final AtomicBoolean EVENT_PUBLISH = new AtomicBoolean(false);
@ -153,4 +160,14 @@ public class ServerMemberManagerTest {
public void testGetServerList() {
assertEquals(2, serverMemberManager.getServerList().size());
}
@Test
public void testEnvSetPort() {
ServletWebServerApplicationContext context = new ServletWebServerApplicationContext();
context.setServerNamespace("management");
Mockito.when(mockEvent.getApplicationContext()).thenReturn(context);
serverMemberManager.onApplicationEvent(mockEvent);
int port = EnvUtil.getPort();
Assert.assertEquals(port, 8848);
}
}