ServerUtils.java 1.17 KB
package com.dituhui.pea.util;

import com.dituhui.pea.log.PlatformLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

/**
 * 服务器辅助工具
 */
@Component
public class ServerUtils implements ApplicationListener<WebServerInitializedEvent> {


    @Autowired
    private PlatformLogger platformLogger;

    /**
     * 服务端口
     */
    private String serverPort;

    /**
     * 服务IP
     */
    private String serverIP = "0.0.0.0";

    public ServerUtils() {
        try {
            serverIP = InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
            platformLogger.error("获取IP失败");
        }
    }


    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        this.serverPort = String.valueOf(event.getWebServer().getPort());
    }

    public String getServerPort() {
        return this.serverPort;
    }

    public String getServerIP() {
        return this.serverIP;
    }

}