Skip to content

Commit 9718408

Browse files
authored
Support Preferred Network Interface via Spring Environment and Fix Early Host Resolution (#15604)
1 parent 629baf7 commit 9718408

4 files changed

Lines changed: 142 additions & 4 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.spring.boot.context.event;
18+
19+
import org.apache.dubbo.common.constants.CommonConstants;
20+
import org.apache.dubbo.common.utils.StringUtils;
21+
22+
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
23+
import org.springframework.context.ApplicationListener;
24+
import org.springframework.core.Ordered;
25+
import org.springframework.core.annotation.Order;
26+
import org.springframework.core.env.ConfigurableEnvironment;
27+
28+
/**
29+
* @since 3.3
30+
*/
31+
@Order(Ordered.HIGHEST_PRECEDENCE + 20) // After LoggingApplicationListener#DEFAULT_ORDER
32+
public class DubboNetInterfaceConfigApplicationListener
33+
implements ApplicationListener<ApplicationContextInitializedEvent> {
34+
35+
@Override
36+
public void onApplicationEvent(ApplicationContextInitializedEvent event) {
37+
ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
38+
String preferredNetworkInterface =
39+
System.getProperty(CommonConstants.DubboProperty.DUBBO_PREFERRED_NETWORK_INTERFACE);
40+
if (StringUtils.isBlank(preferredNetworkInterface)) {
41+
preferredNetworkInterface =
42+
environment.getProperty(CommonConstants.DubboProperty.DUBBO_PREFERRED_NETWORK_INTERFACE);
43+
if (StringUtils.isNotBlank(preferredNetworkInterface)) {
44+
System.setProperty(
45+
CommonConstants.DubboProperty.DUBBO_PREFERRED_NETWORK_INTERFACE, preferredNetworkInterface);
46+
}
47+
}
48+
String ignoredNetworkInterface =
49+
System.getProperty(CommonConstants.DubboProperty.DUBBO_NETWORK_IGNORED_INTERFACE);
50+
if (StringUtils.isBlank(ignoredNetworkInterface)) {
51+
ignoredNetworkInterface =
52+
environment.getProperty(CommonConstants.DubboProperty.DUBBO_NETWORK_IGNORED_INTERFACE);
53+
if (StringUtils.isNotBlank(ignoredNetworkInterface)) {
54+
System.setProperty(
55+
CommonConstants.DubboProperty.DUBBO_NETWORK_IGNORED_INTERFACE, ignoredNetworkInterface);
56+
}
57+
}
58+
}
59+
}

dubbo-spring-boot-project/dubbo-spring-boot/src/main/java/org/apache/dubbo/spring/boot/context/event/WelcomeLogoApplicationListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import java.util.concurrent.atomic.AtomicBoolean;
2424

25-
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
25+
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
2626
import org.springframework.context.ApplicationListener;
2727
import org.springframework.core.Ordered;
2828
import org.springframework.core.annotation.Order;
@@ -38,12 +38,12 @@
3838
* @since 2.7.0
3939
*/
4040
@Order(Ordered.HIGHEST_PRECEDENCE + 20 + 1) // After LoggingApplicationListener#DEFAULT_ORDER
41-
public class WelcomeLogoApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
41+
public class WelcomeLogoApplicationListener implements ApplicationListener<ApplicationContextInitializedEvent> {
4242

4343
private static AtomicBoolean processed = new AtomicBoolean(false);
4444

4545
@Override
46-
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
46+
public void onApplicationEvent(ApplicationContextInitializedEvent event) {
4747

4848
// Skip if processed before, prevent duplicated execution in Hierarchical ApplicationContext
4949
if (processed.get()) {

dubbo-spring-boot-project/dubbo-spring-boot/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
org.springframework.context.ApplicationListener=\
2-
org.apache.dubbo.spring.boot.context.event.WelcomeLogoApplicationListener
2+
org.apache.dubbo.spring.boot.context.event.WelcomeLogoApplicationListener,\
3+
org.apache.dubbo.spring.boot.context.event.DubboNetInterfaceConfigApplicationListener
34
org.springframework.boot.env.EnvironmentPostProcessor=\
45
org.apache.dubbo.spring.boot.env.DubboDefaultPropertiesEnvironmentPostProcessor
56
org.springframework.context.ApplicationContextInitializer=\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.spring.boot.context.event;
18+
19+
import org.apache.dubbo.common.utils.SystemPropertyConfigUtils;
20+
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
import org.junit.jupiter.api.Test;
25+
import org.springframework.boot.SpringApplication;
26+
import org.springframework.boot.WebApplicationType;
27+
import org.springframework.boot.builder.SpringApplicationBuilder;
28+
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
29+
import org.springframework.context.ApplicationListener;
30+
import org.springframework.core.env.ConfigurableEnvironment;
31+
import org.springframework.core.env.MapPropertySource;
32+
import org.springframework.core.env.MutablePropertySources;
33+
34+
import static org.apache.dubbo.common.constants.CommonConstants.DubboProperty.DUBBO_NETWORK_IGNORED_INTERFACE;
35+
import static org.apache.dubbo.common.constants.CommonConstants.DubboProperty.DUBBO_PREFERRED_NETWORK_INTERFACE;
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
38+
/**
39+
* @since 3.3
40+
*/
41+
public class DubboNetInterfaceConfigApplicationListenerTest {
42+
43+
private static final String USE_NETWORK_INTERFACE_NAME = "eth0";
44+
45+
private static final String IGNORED_NETWORK_INTERFACE_NAME = "eth1";
46+
47+
@Test
48+
public void testOnApplicationEvent() {
49+
50+
SpringApplicationBuilder builder =
51+
new SpringApplicationBuilder(DubboNetInterfaceConfigApplicationListenerTest.class);
52+
builder.listeners(new NetworkInterfaceApplicationListener());
53+
builder.web(WebApplicationType.NONE);
54+
SpringApplication application = builder.build();
55+
application.run();
56+
57+
String preferredNetworkInterface =
58+
SystemPropertyConfigUtils.getSystemProperty(DUBBO_PREFERRED_NETWORK_INTERFACE);
59+
String ignoredNetworkInterface = SystemPropertyConfigUtils.getSystemProperty(DUBBO_NETWORK_IGNORED_INTERFACE);
60+
assertEquals(USE_NETWORK_INTERFACE_NAME, preferredNetworkInterface);
61+
assertEquals(IGNORED_NETWORK_INTERFACE_NAME, ignoredNetworkInterface);
62+
}
63+
64+
static class NetworkInterfaceApplicationListener
65+
implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
66+
67+
@Override
68+
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) {
69+
ConfigurableEnvironment environment = applicationEnvironmentPreparedEvent.getEnvironment();
70+
MutablePropertySources propertySources = environment.getPropertySources();
71+
72+
Map<String, Object> map = new HashMap<>();
73+
map.put(DUBBO_PREFERRED_NETWORK_INTERFACE, USE_NETWORK_INTERFACE_NAME);
74+
map.put(DUBBO_NETWORK_IGNORED_INTERFACE, IGNORED_NETWORK_INTERFACE_NAME);
75+
propertySources.addLast(new MapPropertySource("networkInterfaceConfig", map));
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)