在实际开发中,一般我们的服务是集群部署,有时候需要去读取服务所在的ip地址。
1.获取项目的本机地址
1.1 作用
- 获取ip地址,一般可作为本集群中服务的唯一标识。
- 报警时,可输出服务所在的机器ip,快速定位问题。
1.2 获取方法
public class IpUtil{
public static String getLinuxLocalIp() {
String ip = "";
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
String name = intf.getName();
if (!name.contains("docker") && !name.contains("lo")) {
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String ipaddress = inetAddress.getHostAddress();
if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
ip = ipaddress;
}
}
}
}
}
} catch (SocketException ex) {
ip = "127.0.0.1";
log.error("获取ip异常",ex);
}
return ip;
}
}
2. 获取项目运行时jar包的名称
public static void main(String[] args) {
try {
java.net.URL url = BigObjectMyBatisInterceptor.class.getProtectionDomain().getCodeSource().getLocation();
String urlName = java.net.URLDecoder.decode(url.getPath(), "utf-8");
//解析url的Name字段
if (urlName.contains("jar")) {
String[] jars = StringUtils.splitByWholeSeparator(urlName, ".jar");
String jarName = jars[0];
String[] strings = StringUtils.splitByWholeSeparator(jarName, File.separator);
String projectName = strings[strings.length - 1];
}
} catch (Exception e) {
log.error("",e);
}
}
2. 获取当前主机信息&&获取进程信息
2.1 获取当前主机信息
/**
* 获取当前主机信息(内网IP及主机名),用来获取server相关信息的工具方法
*/
public class HostInfoUtils {
private static final String IP;
private static final String HOST_NAME;
static {
try {
InetAddress host = InetAddress.getLocalHost();
HOST_NAME = Optional.ofNullable(host).map(InetAddress::getHostName).orElse(null);
IP = InetAddress.getByName(HOST_NAME).getHostAddress();
} catch (UnknownHostException e) {
throw new UncheckedIOException(e.getMessage(), e);
}
}
public static String getHostIp() {
return IP;
}
public static String getHostName() {
return HOST_NAME;
}
}
2.2 获取进程信息
@Slf4j
public class ProcessInfoUtils {
private static final String UNKNOWN_BOOTSTRAP = "n/a";
private static volatile Integer pid;
private static final int UNKNOWN_PID = -1;
private static volatile String bootstrap;
private static String MAIN_CLASS;
static {
try {
MAIN_CLASS = StringUtils.split(System.getProperty("sun.java.command"), ' ')[0];
} catch (Exception e) {
log.error("", e);
MAIN_CLASS = null;
}
}
public static String getMainClass() {
return MAIN_CLASS;
}
public static int getPid() {
if (pid == null) {
synchronized (ProcessInfoUtils.class) {
if (pid == null) {
try {
String name = ManagementFactory.getRuntimeMXBean().getName();
pid = Integer.parseInt(name.substring(0, name.indexOf('@')));
} catch (Exception e) {
pid = UNKNOWN_PID;
}
}
}
}
return pid;
}
public static String getBootstrap() {
if (bootstrap == null) {
synchronized (ProcessInfoUtils.class) {
if (bootstrap == null) {
try {
bootstrap = ofNullable(getProperty("sun.java.command"))
.orElse(UNKNOWN_BOOTSTRAP);
} catch (Throwable e) {
bootstrap = UNKNOWN_BOOTSTRAP;
}
}
}
}
return bootstrap;
}
}
网友评论