package com.shuidihuzhu.sdb.cps.admin.manager;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Container;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
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 java.util.List;
/**
* StartAppCommand
*
* @author zhi
* @version 1.0
* @date 2021-08-16 17:19
*/
@Component
@Slf4j
public class StartAppCommand implements CommandLineRunner {
@Resource
private ServletWebServerApplicationContext servletWebServerApplicationContext;
@Override
public void run(String... args) throws Exception {
log.info("");
log.info("~ ~ ~ ~ ~ ~ ~ ~ ~ start app success ~ ~ ~ ~ ~ ~ ~ ~ ~");
String path = StringUtils.EMPTY;
WebServer webServer = servletWebServerApplicationContext.getWebServer();
int port = webServer.getPort();
if (webServer instanceof TomcatWebServer) {
TomcatWebServer tomcatWebServer = (TomcatWebServer)webServer;
path = getContextPath(tomcatWebServer.getTomcat());
}
log.info("port: {} path: {} \n", port, path);
List<String> host = getHost();
final String showPath = path;
host.forEach(v -> log.info("{}", String.format("http://%s:%d%s", v, port, showPath)));
log.info("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n");
}
private String getContextPath(Tomcat tomcat) {
String path = StringUtils.EMPTY;
Container[] children = tomcat
.getHost()
.findChildren();
if (ArrayUtils.isNotEmpty(children)) {
Container child = children[0];
if (child instanceof StandardContext) {
path = ((StandardContext)child).getPath();
}
}
return path;
}
private List<String> getHost() throws UnknownHostException {
List<String> list = Lists.newArrayList();
InetAddress addr = InetAddress.getLocalHost();
list.add(addr.getHostName());
list.add(addr.getHostAddress());
list.addAll(getLocalIpList());
return list;
}
private List<String> getLocalIpList() {
List<String> ipList = Lists.newArrayList();
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress inetAddress = addresses.nextElement();
// IPV4
if (inetAddress instanceof Inet4Address) {
ipList.add(inetAddress.getHostAddress());
}
}
}
} catch (SocketException e) {
log.error("socket error ", e);
}
return ipList;
}
}
StartAppCommand -springboot启动事件捕捉
最新推荐文章于 2024-08-23 10:28:52 发布