ServerUtils.java
1.17 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
package com.dituhui.mp.util;
import com.dituhui.mp.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;
}
}