package com.sf.confirmation.core.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.water.common.exception.SysException;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* @author 80004084
* @date 2019/9/30 15:18
* @description :获取ip和端口号
*/
@Component
public class InternetInfo implements ApplicationListener<WebServerInitializedEvent> {

  private final static Logger LOGGER = LoggerFactory.getLogger(InternetInfo.class);

  private int serverPort;

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

  public int getPort() {
    return this.serverPort;
  }

  public String getLocalHostAddress() {
    InetAddress address = null;
  try {
    address = InetAddress.getLocalHost();
  } catch (UnknownHostException e) {
    LOGGER.error("UnknownHostException:",e);
    throw new SysException("获取服务器ip和端口失败");
  }
  return address.getHostAddress() + ":" + getPort();
  }

}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-06
  • 2022-02-04
  • 2021-11-23
  • 2021-11-06
  • 2022-01-23
猜你喜欢
  • 2021-09-29
  • 2021-12-15
  • 2021-08-18
  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
  • 2021-11-15
相关资源
相似解决方案