StartAppCommand -springboot启动事件捕捉

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;
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值