ControlManagerCenterApplication.java 2.67 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.Enumeration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication
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 {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = networkInterfaces.nextElement();
                // System.out.println(netInterface.getName());
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress ipTmp = addresses.nextElement();
                    if (ipTmp != null && ipTmp instanceof Inet4Address && ipTmp.isSiteLocalAddress() && !ipTmp.isLoopbackAddress()
                        && ipTmp.getHostAddress().indexOf(":") == -1 && !ipTmp.getHostAddress().endsWith(".1")) {
                        return ipTmp;
                    }
                }
            }
            return InetAddress.getLocalHost();
        } catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }

}