This is an automated email from the ASF dual-hosted git repository.
zrlw pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.3 by this push:
new 9718408bb8 Support Preferred Network Interface via Spring Environment
and Fix Early Host Resolution (#15604)
9718408bb8 is described below
commit 9718408bb8dcf84e76d72c42e9743ce938f18178
Author: 子衿 <[email protected]>
AuthorDate: Wed Aug 6 13:19:19 2025 +0800
Support Preferred Network Interface via Spring Environment and Fix Early
Host Resolution (#15604)
---
...DubboNetInterfaceConfigApplicationListener.java | 59 ++++++++++++++++
.../event/WelcomeLogoApplicationListener.java | 6 +-
.../src/main/resources/META-INF/spring.factories | 3 +-
...oNetInterfaceConfigApplicationListenerTest.java | 78 ++++++++++++++++++++++
4 files changed, 142 insertions(+), 4 deletions(-)
diff --git
a/dubbo-spring-boot-project/dubbo-spring-boot/src/main/java/org/apache/dubbo/spring/boot/context/event/DubboNetInterfaceConfigApplicationListener.java
b/dubbo-spring-boot-project/dubbo-spring-boot/src/main/java/org/apache/dubbo/spring/boot/context/event/DubboNetInterfaceConfigApplicationListener.java
new file mode 100644
index 0000000000..0b06ca6ecc
--- /dev/null
+++
b/dubbo-spring-boot-project/dubbo-spring-boot/src/main/java/org/apache/dubbo/spring/boot/context/event/DubboNetInterfaceConfigApplicationListener.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.spring.boot.context.event;
+
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.common.utils.StringUtils;
+
+import
org.springframework.boot.context.event.ApplicationContextInitializedEvent;
+import org.springframework.context.ApplicationListener;
+import org.springframework.core.Ordered;
+import org.springframework.core.annotation.Order;
+import org.springframework.core.env.ConfigurableEnvironment;
+
+/**
+ * @since 3.3
+ */
+@Order(Ordered.HIGHEST_PRECEDENCE + 20) // After
LoggingApplicationListener#DEFAULT_ORDER
+public class DubboNetInterfaceConfigApplicationListener
+ implements ApplicationListener<ApplicationContextInitializedEvent> {
+
+ @Override
+ public void onApplicationEvent(ApplicationContextInitializedEvent event) {
+ ConfigurableEnvironment environment =
event.getApplicationContext().getEnvironment();
+ String preferredNetworkInterface =
+
System.getProperty(CommonConstants.DubboProperty.DUBBO_PREFERRED_NETWORK_INTERFACE);
+ if (StringUtils.isBlank(preferredNetworkInterface)) {
+ preferredNetworkInterface =
+
environment.getProperty(CommonConstants.DubboProperty.DUBBO_PREFERRED_NETWORK_INTERFACE);
+ if (StringUtils.isNotBlank(preferredNetworkInterface)) {
+ System.setProperty(
+
CommonConstants.DubboProperty.DUBBO_PREFERRED_NETWORK_INTERFACE,
preferredNetworkInterface);
+ }
+ }
+ String ignoredNetworkInterface =
+
System.getProperty(CommonConstants.DubboProperty.DUBBO_NETWORK_IGNORED_INTERFACE);
+ if (StringUtils.isBlank(ignoredNetworkInterface)) {
+ ignoredNetworkInterface =
+
environment.getProperty(CommonConstants.DubboProperty.DUBBO_NETWORK_IGNORED_INTERFACE);
+ if (StringUtils.isNotBlank(ignoredNetworkInterface)) {
+ System.setProperty(
+
CommonConstants.DubboProperty.DUBBO_NETWORK_IGNORED_INTERFACE,
ignoredNetworkInterface);
+ }
+ }
+ }
+}
diff --git
a/dubbo-spring-boot-project/dubbo-spring-boot/src/main/java/org/apache/dubbo/spring/boot/context/event/WelcomeLogoApplicationListener.java
b/dubbo-spring-boot-project/dubbo-spring-boot/src/main/java/org/apache/dubbo/spring/boot/context/event/WelcomeLogoApplicationListener.java
index 28a86e7b62..885f8d75c9 100644
---
a/dubbo-spring-boot-project/dubbo-spring-boot/src/main/java/org/apache/dubbo/spring/boot/context/event/WelcomeLogoApplicationListener.java
+++
b/dubbo-spring-boot-project/dubbo-spring-boot/src/main/java/org/apache/dubbo/spring/boot/context/event/WelcomeLogoApplicationListener.java
@@ -22,7 +22,7 @@ import org.apache.dubbo.common.logger.LoggerFactory;
import java.util.concurrent.atomic.AtomicBoolean;
-import
org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
+import
org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
@@ -38,12 +38,12 @@ import static
org.apache.dubbo.spring.boot.util.DubboUtils.LINE_SEPARATOR;
* @since 2.7.0
*/
@Order(Ordered.HIGHEST_PRECEDENCE + 20 + 1) // After
LoggingApplicationListener#DEFAULT_ORDER
-public class WelcomeLogoApplicationListener implements
ApplicationListener<ApplicationEnvironmentPreparedEvent> {
+public class WelcomeLogoApplicationListener implements
ApplicationListener<ApplicationContextInitializedEvent> {
private static AtomicBoolean processed = new AtomicBoolean(false);
@Override
- public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
+ public void onApplicationEvent(ApplicationContextInitializedEvent event) {
// Skip if processed before, prevent duplicated execution in
Hierarchical ApplicationContext
if (processed.get()) {
diff --git
a/dubbo-spring-boot-project/dubbo-spring-boot/src/main/resources/META-INF/spring.factories
b/dubbo-spring-boot-project/dubbo-spring-boot/src/main/resources/META-INF/spring.factories
index e2fad464e7..6bd78ab1f8 100644
---
a/dubbo-spring-boot-project/dubbo-spring-boot/src/main/resources/META-INF/spring.factories
+++
b/dubbo-spring-boot-project/dubbo-spring-boot/src/main/resources/META-INF/spring.factories
@@ -1,5 +1,6 @@
org.springframework.context.ApplicationListener=\
-org.apache.dubbo.spring.boot.context.event.WelcomeLogoApplicationListener
+org.apache.dubbo.spring.boot.context.event.WelcomeLogoApplicationListener,\
+
org.apache.dubbo.spring.boot.context.event.DubboNetInterfaceConfigApplicationListener
org.springframework.boot.env.EnvironmentPostProcessor=\
org.apache.dubbo.spring.boot.env.DubboDefaultPropertiesEnvironmentPostProcessor
org.springframework.context.ApplicationContextInitializer=\
diff --git
a/dubbo-spring-boot-project/dubbo-spring-boot/src/test/java/org/apache/dubbo/spring/boot/context/event/DubboNetInterfaceConfigApplicationListenerTest.java
b/dubbo-spring-boot-project/dubbo-spring-boot/src/test/java/org/apache/dubbo/spring/boot/context/event/DubboNetInterfaceConfigApplicationListenerTest.java
new file mode 100644
index 0000000000..8a6c82bac2
--- /dev/null
+++
b/dubbo-spring-boot-project/dubbo-spring-boot/src/test/java/org/apache/dubbo/spring/boot/context/event/DubboNetInterfaceConfigApplicationListenerTest.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.spring.boot.context.event;
+
+import org.apache.dubbo.common.utils.SystemPropertyConfigUtils;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.WebApplicationType;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import
org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
+import org.springframework.context.ApplicationListener;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.MapPropertySource;
+import org.springframework.core.env.MutablePropertySources;
+
+import static
org.apache.dubbo.common.constants.CommonConstants.DubboProperty.DUBBO_NETWORK_IGNORED_INTERFACE;
+import static
org.apache.dubbo.common.constants.CommonConstants.DubboProperty.DUBBO_PREFERRED_NETWORK_INTERFACE;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * @since 3.3
+ */
+public class DubboNetInterfaceConfigApplicationListenerTest {
+
+ private static final String USE_NETWORK_INTERFACE_NAME = "eth0";
+
+ private static final String IGNORED_NETWORK_INTERFACE_NAME = "eth1";
+
+ @Test
+ public void testOnApplicationEvent() {
+
+ SpringApplicationBuilder builder =
+ new
SpringApplicationBuilder(DubboNetInterfaceConfigApplicationListenerTest.class);
+ builder.listeners(new NetworkInterfaceApplicationListener());
+ builder.web(WebApplicationType.NONE);
+ SpringApplication application = builder.build();
+ application.run();
+
+ String preferredNetworkInterface =
+
SystemPropertyConfigUtils.getSystemProperty(DUBBO_PREFERRED_NETWORK_INTERFACE);
+ String ignoredNetworkInterface =
SystemPropertyConfigUtils.getSystemProperty(DUBBO_NETWORK_IGNORED_INTERFACE);
+ assertEquals(USE_NETWORK_INTERFACE_NAME, preferredNetworkInterface);
+ assertEquals(IGNORED_NETWORK_INTERFACE_NAME, ignoredNetworkInterface);
+ }
+
+ static class NetworkInterfaceApplicationListener
+ implements
ApplicationListener<ApplicationEnvironmentPreparedEvent> {
+
+ @Override
+ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent
applicationEnvironmentPreparedEvent) {
+ ConfigurableEnvironment environment =
applicationEnvironmentPreparedEvent.getEnvironment();
+ MutablePropertySources propertySources =
environment.getPropertySources();
+
+ Map<String, Object> map = new HashMap<>();
+ map.put(DUBBO_PREFERRED_NETWORK_INTERFACE,
USE_NETWORK_INTERFACE_NAME);
+ map.put(DUBBO_NETWORK_IGNORED_INTERFACE,
IGNORED_NETWORK_INTERFACE_NAME);
+ propertySources.addLast(new
MapPropertySource("networkInterfaceConfig", map));
+ }
+ }
+}