package org.springblade.common.config; import java.net.InetAddress; import java.net.UnknownHostException; import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; /** * 服务器配置 * @author zhongrj * @since 2022-01-07 */ @Component public class ServerConfig implements ApplicationListener { private int serverPort; /** * 获取服务器 url * @return */ public String getUrl() { InetAddress address = null; try { address = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } return "http://"+address.getHostAddress() +":"+this.serverPort; } /** * 获取服务器 ip * @return */ public String getServerIp() { InetAddress address = null; try { address = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } return address.getHostAddress(); } /** * 获取服务器 hostname * @return */ public String getServerHostName() { InetAddress address = null; try { address = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } return address.getHostName(); } @Override public void onApplicationEvent(WebServerInitializedEvent event) { this.serverPort = event.getWebServer().getPort(); } }