ControlManagerCenterApplication.java
3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.huaheng.control.management;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@SpringBootApplication
@EnableScheduling
public class ControlManagerCenterApplication {
public static void main(String[] args) {
ConfigurableApplicationContext application = SpringApplication.run(ControlManagerCenterApplication.class, args);
Environment env = application.getEnvironment();
String ip = getLocalHostExactAddress().getHostAddress();
String port = env.getProperty("server.port");
String profiles = env.getProperty("spring.profiles.active");
String path = env.getProperty("server.servlet.context-path");
log.info("\n----------------------------------------------------------------------------------\n\n\t"
+ "Application Huaheng Control Management Center is running!\n\n\t"
+ "WEB swagger local: \t\thttp://localhost:" + port + path + "/swagger-ui/index.html\n\t"
+ "WEB swagger external: \thttp://" + ip + ":" + port + path + "/swagger-ui/index.html\n\n\t"
+ "The following profiles are active: [" + profiles + "]\n\n"
+ "-----------------------------------------------------------------------------------");
}
public static InetAddress getLocalHostExactAddress() {
try {
List<InetAddress> candidateAddresses = new ArrayList<>();
Enumeration<NetworkInterface> interfaces;
interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
// 可选:跳过虚拟接口或非活动接口
if (!networkInterface.isUp() || networkInterface.isLoopback() || networkInterface.isVirtual()) {
continue;
}
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
// 优先考虑IPv4
if (address instanceof Inet4Address) {
// 排除环回地址
if (!address.isLoopbackAddress()) {
// 按优先级分类
if (!address.isLinkLocalAddress()) { // 排除链路本地地址(169.254.x.x)
candidateAddresses.add(address);
}
}
}
}
}
// 按优先级选择地址
if (!candidateAddresses.isEmpty()) {
// 1. 优先选择站点本地地址(私有地址)
for (InetAddress addr : candidateAddresses) {
if (addr.isSiteLocalAddress()) {
return addr;
}
}
// 2. 如果没有私有地址,返回第一个全局地址
return candidateAddresses.get(0);
}
} catch (SocketException e) {
log.error("获取本机IP地址异常", e);
}
return null;
}
}