邮件API
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.mini.capi.config.component;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class AppNetworkInfo {
|
||||
|
||||
// 获取主机名
|
||||
public String getHostName() throws UnknownHostException {
|
||||
return InetAddress.getLocalHost().getHostName();
|
||||
}
|
||||
|
||||
// 获取本地 IP 地址(可能是内网 IP)
|
||||
public String getLocalIp() throws UnknownHostException {
|
||||
return InetAddress.getLocalHost().getHostAddress();
|
||||
}
|
||||
|
||||
// 获取所有有效 IPv4 地址(过滤回环地址、IPv6,支持多网卡)
|
||||
public List<String> getAllIps() throws SocketException {
|
||||
List<String> ips = new ArrayList<>();
|
||||
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||
while (interfaces.hasMoreElements()) {
|
||||
NetworkInterface ni = interfaces.nextElement();
|
||||
// 跳过虚拟网卡、禁用的网卡
|
||||
if (ni.isLoopback() || !ni.isUp()) {
|
||||
continue;
|
||||
}
|
||||
Enumeration<InetAddress> addresses = ni.getInetAddresses();
|
||||
while (addresses.hasMoreElements()) {
|
||||
InetAddress addr = addresses.nextElement();
|
||||
// 只保留 IPv4 地址
|
||||
if (addr instanceof Inet4Address) {
|
||||
ips.add(addr.getHostAddress());
|
||||
}
|
||||
}
|
||||
}
|
||||
return ips;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.mini.capi.config.component;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.io.File;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
|
||||
@Component
|
||||
public class AppPathInfo {
|
||||
|
||||
// 获取当前工作目录(执行启动命令的目录)
|
||||
public String getWorkDir() {
|
||||
return System.getProperty("user.dir");
|
||||
}
|
||||
|
||||
// 获取 Jar 包所在目录(仅 Jar 运行时有效;IDE 中返回 classes 目录父级)
|
||||
public String getJarDir() throws URISyntaxException {
|
||||
URL jarUrl = getClass().getProtectionDomain().getCodeSource().getLocation();
|
||||
File jarFile = new File(jarUrl.toURI());
|
||||
return jarFile.getParentFile().getAbsolutePath();
|
||||
}
|
||||
|
||||
// 获取类路径下的资源目录(IDE 中是 target/classes,Jar 中是 jar 内部路径)
|
||||
public String getResourceDir() {
|
||||
URL resourceUrl = getClass().getClassLoader().getResource("");
|
||||
if (resourceUrl != null) {
|
||||
return new File(resourceUrl.getPath()).getAbsolutePath();
|
||||
}
|
||||
return "unknown-resource-dir";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.mini.capi.config.component;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.RuntimeMXBean;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class AppProcessInfo {
|
||||
|
||||
// 获取进程 PID(兼容 JDK 8+)
|
||||
public long getPid() {
|
||||
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
|
||||
String name = mxBean.getName(); // 格式:PID@主机名
|
||||
return Long.parseLong(name.split("@")[0]);
|
||||
}
|
||||
|
||||
// 获取 JVM 启动参数(如 -Xms、-Xmx、-D 系统属性)
|
||||
public List<String> getJvmArgs() {
|
||||
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
|
||||
return mxBean.getInputArguments();
|
||||
}
|
||||
|
||||
// 获取已使用堆内存(单位:MB)
|
||||
public long getUsedHeapMemory() {
|
||||
long total = Runtime.getRuntime().totalMemory() / 1024 / 1024;
|
||||
long free = Runtime.getRuntime().freeMemory() / 1024 / 1024;
|
||||
return total - free;
|
||||
}
|
||||
|
||||
// 获取最大堆内存(单位:MB)
|
||||
public long getMaxHeapMemory() {
|
||||
return Runtime.getRuntime().maxMemory() / 1024 / 1024;
|
||||
}
|
||||
|
||||
// 获取程序运行时间(单位:秒)
|
||||
public long getUptime() {
|
||||
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
|
||||
return mxBean.getUptime() / 1000;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.mini.capi.config.component;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringBootVersion;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AppRuntimeInfo implements EnvironmentAware{
|
||||
|
||||
// 应用名(默认 unknown-app)
|
||||
@Getter
|
||||
@Value("${spring.application.name:unknown-app}")
|
||||
private String appName;
|
||||
// 服务端口(默认 8080,支持随机端口)
|
||||
@Getter
|
||||
@Value("${server.port:8080}")
|
||||
private Integer serverPort;
|
||||
// 激活的环境(默认 default)
|
||||
@Getter
|
||||
@Value("${spring.profiles.active:default}")
|
||||
private String activeProfile;
|
||||
|
||||
// JDK 版本
|
||||
@Getter
|
||||
@Value("${java.version:unknown}")
|
||||
private String jdkVersion;
|
||||
|
||||
// Spring 环境上下文(用于灵活获取配置)
|
||||
private Environment environment;
|
||||
|
||||
// 获取 Spring Boot 版本
|
||||
public String getSpringBootVersion() {
|
||||
return SpringBootVersion.getVersion();
|
||||
}
|
||||
|
||||
// 获取所有激活的环境(支持多环境,如 dev,test)
|
||||
public String[] getActiveProfiles() {
|
||||
return environment.getActiveProfiles();
|
||||
}
|
||||
|
||||
// 灵活获取配置项(支持默认值)
|
||||
public String getConfigValue(String key, String defaultValue) {
|
||||
return environment.getProperty(key, defaultValue);
|
||||
}
|
||||
|
||||
// 实现 EnvironmentAware 接口,注入 Environment
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user