diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/event/TraceEvent.java b/common/src/main/java/com/alibaba/nacos/common/trace/event/TraceEvent.java index 9d216ce24..dbc934166 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/event/TraceEvent.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/event/TraceEvent.java @@ -24,7 +24,10 @@ import com.alibaba.nacos.common.notify.Event; * @author yanda */ public class TraceEvent extends Event { - private String type; + + private static final long serialVersionUID = -3065900892505697062L; + + private final String type; private final long eventTime; diff --git a/core/src/main/java/com/alibaba/nacos/core/trace/NacosCombinedTraceSubscriber.java b/core/src/main/java/com/alibaba/nacos/core/trace/NacosCombinedTraceSubscriber.java index 9d69d5eb8..bdf2839b8 100644 --- a/core/src/main/java/com/alibaba/nacos/core/trace/NacosCombinedTraceSubscriber.java +++ b/core/src/main/java/com/alibaba/nacos/core/trace/NacosCombinedTraceSubscriber.java @@ -74,14 +74,23 @@ public class NacosCombinedTraceSubscriber extends SmartSubscriber { if (null == subscribers) { return; } + TraceEvent traceEvent = (TraceEvent) event; for (NacosTraceSubscriber each : subscribers) { - try { - each.onEvent((TraceEvent) event); - } catch (Exception ignored) { + if (null != each.executor()) { + each.executor().execute(() -> onEvent0(each, traceEvent)); + } else { + onEvent0(each, traceEvent); } } } + private void onEvent0(NacosTraceSubscriber subscriber, TraceEvent event) { + try { + subscriber.onEvent(event); + } catch (Exception ignored) { + } + } + public void shutdown() { NotifyCenter.deregisterSubscriber(this); } diff --git a/core/src/test/java/com/alibaba/nacos/core/trace/NacosCombinedTraceSubscriberTest.java b/core/src/test/java/com/alibaba/nacos/core/trace/NacosCombinedTraceSubscriberTest.java index 0788e8ddc..77007df85 100644 --- a/core/src/test/java/com/alibaba/nacos/core/trace/NacosCombinedTraceSubscriberTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/trace/NacosCombinedTraceSubscriberTest.java @@ -29,17 +29,23 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.invocation.InvocationOnMock; import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; import org.springframework.test.util.ReflectionTestUtils; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.concurrent.Executor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -105,4 +111,21 @@ public class NacosCombinedTraceSubscriberTest { verify(mockSubscriber, never()).onEvent(event2); verify(mockSubscriber2, never()).onEvent(event2); } + + @Test + public void testOnEventWithExecutor() { + Executor executor = mock(Executor.class); + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocationOnMock) throws Throwable { + invocationOnMock.getArgument(0, Runnable.class).run(); + return null; + } + }).when(executor).execute(any(Runnable.class)); + when(mockSubscriber.executor()).thenReturn(executor); + RegisterInstanceTraceEvent event = new RegisterInstanceTraceEvent(1L, "", true, "", "", "", "", 1); + combinedTraceSubscriber.onEvent(event); + verify(mockSubscriber).onEvent(event); + verify(mockSubscriber2).onEvent(event); + } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/factory/impl/ConnectionBasedClientFactory.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/factory/impl/ConnectionBasedClientFactory.java index 2c28a9246..61eeb0674 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/factory/impl/ConnectionBasedClientFactory.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/factory/impl/ConnectionBasedClientFactory.java @@ -46,7 +46,7 @@ public class ConnectionBasedClientFactory implements ClientFactory