ControlManagerCenterApplication.java 3.68 KB
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;
    }
}