Skip to content

Commit 962a58b

Browse files
authored
Merge pull request #50 from SpringCloud/develop
Develop
2 parents c6af3e4 + 2ac5243 commit 962a58b

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

moss-client/moss-client-1.x/src/main/java/org/xujin/moss/client/config/ManagementEnvironmentCustomizer.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@
88
import org.springframework.core.io.support.PropertiesLoaderUtils;
99

1010
import java.io.IOException;
11+
import java.net.InetAddress;
12+
import java.net.Socket;
1113
import java.util.Properties;
1214

1315
/**
1416
* 内嵌 Web 容器模式运行时,指定一些 Management 的属性配置
1517
*/
1618
public class ManagementEnvironmentCustomizer implements EnvironmentCustomizer<ConfigurableEnvironment> {
19+
20+
private static final String SPRINGBOOT_MANAGEMENT_PORT_KEY = "management.port";
21+
22+
private static final int SPRINGBOOT_MANAGEMENT_PORT_VALUE = 8081;
23+
1724
private static final Log logger = LogFactory.getLog(AdminEndpointApplicationRunListener.class);
1825
private static final String DEFAULT_PROPERTY = "META-INF/moss-client/bootstrap.properties";
1926
@Override
@@ -22,9 +29,25 @@ public void customize(ConfigurableEnvironment env) {
2229
Properties props;
2330
ClassPathResource resource = new ClassPathResource(DEFAULT_PROPERTY);
2431
props = PropertiesLoaderUtils.loadProperties(resource);
32+
props.put(SPRINGBOOT_MANAGEMENT_PORT_KEY, getManagementPort(env));
2533
env.getPropertySources().addLast(new PropertiesPropertySource("managementProperties", props));
2634
} catch (IOException e) {
2735
logger.error("Failed to load " + DEFAULT_PROPERTY);
2836
}
2937
}
38+
39+
private int getManagementPort(ConfigurableEnvironment env) {
40+
if (!"prod".equalsIgnoreCase(env.getProperty("spring.profiles.active"))) {
41+
try {
42+
//不是生产环境,使用Socket去连接如果能连接上表示端口被占用
43+
InetAddress Address = InetAddress.getByName("127.0.0.1");
44+
Socket socket = new Socket(Address, SPRINGBOOT_MANAGEMENT_PORT_VALUE);
45+
logger.info(SPRINGBOOT_MANAGEMENT_PORT_VALUE+":port is used,return:0");
46+
return 0;
47+
} catch (IOException e) {
48+
logger.info(SPRINGBOOT_MANAGEMENT_PORT_VALUE+":port is not used");
49+
}
50+
}
51+
return SPRINGBOOT_MANAGEMENT_PORT_VALUE;
52+
}
3053
}

moss-client/moss-client-2.x/src/main/java/org/xujin/moss/client/config/ManagementEnvironmentCustomizer.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
*/
2323
public class ManagementEnvironmentCustomizer implements EnvironmentCustomizer<ConfigurableEnvironment> {
2424

25+
26+
private static final int SPRINGBOOT_MANAGEMENT_PORT_VALUE = 8081;
27+
28+
private static final String SPRINGBOOT_MANAGEMENT_PORT_KEY = "management.server.port";
29+
2530
private static final Log logger = LogFactory.getLog(AdminEndpointApplicationRunListener.class);
2631

2732
private static final String DEFAULT_PROPERTY = "META-INF/moss-client/bootstrap.properties";
@@ -32,9 +37,25 @@ public void customize(ConfigurableEnvironment env) {
3237
Properties props;
3338
ClassPathResource resource = new ClassPathResource(DEFAULT_PROPERTY);
3439
props = PropertiesLoaderUtils.loadProperties(resource);
40+
props.put(SPRINGBOOT_MANAGEMENT_PORT_KEY, getManagementPort(env));
3541
env.getPropertySources().addLast(new PropertiesPropertySource("managementProperties", props));
3642
} catch (IOException e) {
3743
logger.error("Failed to load " + DEFAULT_PROPERTY);
3844
}
3945
}
46+
47+
private int getManagementPort(ConfigurableEnvironment env) {
48+
if (!"prod".equalsIgnoreCase(env.getProperty("spring.profiles.active"))) {
49+
try {
50+
//不是生产环境,使用Socket去连接如果能连接上表示端口被占用
51+
InetAddress Address = InetAddress.getByName("127.0.0.1");
52+
Socket socket = new Socket(Address, SPRINGBOOT_MANAGEMENT_PORT_VALUE);
53+
logger.info(SPRINGBOOT_MANAGEMENT_PORT_VALUE+":port is used,return:0");
54+
return 0;
55+
} catch (IOException e) {
56+
logger.info(SPRINGBOOT_MANAGEMENT_PORT_VALUE+":port is not used");
57+
}
58+
}
59+
return SPRINGBOOT_MANAGEMENT_PORT_VALUE ;
60+
}
4061
}

0 commit comments

Comments
 (0)