Ignore the WebServerInitializedEvent if the name space of the spring context is management (#7236)
Close#7230
This commit is contained in:
parent
62c778e995
commit
e1487164ff
@ -89,6 +89,8 @@ public class ServerMemberManager implements ApplicationListener<WebServerInitial
|
|||||||
|
|
||||||
private static final String SERVER_PORT_PROPERTY = "server.port";
|
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 String MEMBER_CHANGE_EVENT_QUEUE_SIZE_PROPERTY = "nacos.member-change-event.queue.size";
|
||||||
|
|
||||||
private static final int DEFAULT_MEMBER_CHANGE_EVENT_QUEUE_SIZE = 128;
|
private static final int DEFAULT_MEMBER_CHANGE_EVENT_QUEUE_SIZE = 128;
|
||||||
@ -441,6 +443,12 @@ public class ServerMemberManager implements ApplicationListener<WebServerInitial
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(WebServerInitializedEvent event) {
|
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);
|
getSelf().setState(NodeState.UP);
|
||||||
if (!EnvUtil.getStandaloneMode()) {
|
if (!EnvUtil.getStandaloneMode()) {
|
||||||
GlobalExecutor.scheduleByCommon(this.infoReportTask, DEFAULT_TASK_DELAY_TIME);
|
GlobalExecutor.scheduleByCommon(this.infoReportTask, DEFAULT_TASK_DELAY_TIME);
|
||||||
|
@ -22,11 +22,15 @@ import com.alibaba.nacos.common.notify.NotifyCenter;
|
|||||||
import com.alibaba.nacos.sys.env.EnvUtil;
|
import com.alibaba.nacos.sys.env.EnvUtil;
|
||||||
import com.alibaba.nacos.sys.utils.InetUtils;
|
import com.alibaba.nacos.sys.utils.InetUtils;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
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 org.springframework.core.env.ConfigurableEnvironment;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
@ -54,6 +58,9 @@ public class ServerMemberManagerTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private EventPublisher eventPublisher;
|
private EventPublisher eventPublisher;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private WebServerInitializedEvent mockEvent;
|
||||||
|
|
||||||
private ServerMemberManager serverMemberManager;
|
private ServerMemberManager serverMemberManager;
|
||||||
|
|
||||||
private static final AtomicBoolean EVENT_PUBLISH = new AtomicBoolean(false);
|
private static final AtomicBoolean EVENT_PUBLISH = new AtomicBoolean(false);
|
||||||
@ -153,4 +160,14 @@ public class ServerMemberManagerTest {
|
|||||||
public void testGetServerList() {
|
public void testGetServerList() {
|
||||||
assertEquals(2, serverMemberManager.getServerList().size());
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user