Última actividad 1772103084

监听WebServer初始化事件,打印程序运行相关信息,将该文件下载并粘贴到项目代码目录中即可生效

Revisión ef8e3adbc821ef519a505c3ab21d94dcb819bd58

AdvancedStartupLogger.java Sin formato
1package com.example.startup;
2
3import lombok.extern.slf4j.Slf4j;
4import org.springframework.boot.web.context.WebServerInitializedEvent;
5import org.springframework.context.ApplicationListener;
6import org.springframework.core.env.Environment;
7import org.springframework.stereotype.Component;
8
9import java.lang.management.ManagementFactory;
10import java.net.DatagramSocket;
11import java.net.Inet4Address;
12import java.net.InetAddress;
13import java.net.NetworkInterface;
14import java.time.Duration;
15import java.time.Instant;
16import java.util.ArrayList;
17import java.util.Arrays;
18import java.util.Enumeration;
19import java.util.List;
20
21@Slf4j
22@Component
23public class AdvancedStartupLogger implements ApplicationListener<WebServerInitializedEvent> {
24
25 private final Environment env;
26 private static final Instant START_TIME = Instant.now();
27
28 public AdvancedStartupLogger(Environment env) {
29 this.env = env;
30 }
31
32 @Override
33 public void onApplicationEvent(WebServerInitializedEvent event) {
34
35 int port = event.getWebServer().getPort();
36 String protocol = resolveProtocol();
37 String contextPath = resolveContextPath();
38 String appName = env.getProperty("spring.application.name", "application");
39 String profiles = Arrays.toString(env.getActiveProfiles());
40 String pid = getPid();
41 String javaVersion = System.getProperty("java.version");
42 String outboundIp = getOutboundIp();
43 boolean docker = isDocker();
44 boolean k8s = isK8s();
45
46 long startupMs = Duration.between(START_TIME, Instant.now()).toMillis();
47
48 List<String> allIps = getAllNetworkIps();
49 long maxHeapMb = Runtime.getRuntime().maxMemory() / 1024 / 1024;
50
51 String actuatorPath = env.getProperty("management.endpoints.web.base-path", "actuator");
52
53 log.info("\n" +
54 "================= 🚀 SERVICE STARTED =================\n" +
55 "App Name : {}\n" +
56 "PID : {}\n" +
57 "Profiles : {}\n" +
58 "Java Version : {}\n" +
59 "Startup Time : {} ms\n" +
60 "Max Heap : {} MB\n\n" +
61 "Access URLs:\n" +
62 "Local : {}://localhost:{}{}\n" +
63 "External : {}://{}:{}{}\n" +
64 "Actuator : {}://{}:{}{}{}\n\n" +
65 "All Network IPs : {}\n" +
66 "Docker : {}\n" +
67 "Kubernetes : {}\n" +
68 "=======================================================",
69 appName,
70 pid,
71 profiles,
72 javaVersion,
73 startupMs,
74 maxHeapMb,
75 protocol,
76 port,
77 contextPath,
78 protocol,
79 outboundIp,
80 port,
81 contextPath,
82 protocol,
83 outboundIp,
84 port,
85 contextPath,
86 actuatorPath,
87 allIps,
88 docker,
89 k8s
90 );
91
92 logStructuredJson(appName, port, outboundIp, startupMs);
93 }
94
95 private String resolveProtocol() {
96 return env.getProperty("server.ssl.key-store") != null ? "https" : "http";
97 }
98
99 private String resolveContextPath() {
100 return env.getProperty("server.servlet.context-path", "");
101 }
102
103 private String getPid() {
104 return ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
105 }
106
107 private boolean isDocker() {
108 return new java.io.File("/.dockerenv").exists();
109 }
110
111 private boolean isK8s() {
112 return System.getenv("KUBERNETES_SERVICE_HOST") != null;
113 }
114
115 private String getOutboundIp() {
116 try (DatagramSocket socket = new DatagramSocket()) {
117 socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
118 return socket.getLocalAddress().getHostAddress();
119 } catch (Exception e) {
120 return "Unknown";
121 }
122 }
123
124 private List<String> getAllNetworkIps() {
125 List<String> ips = new ArrayList<>();
126 try {
127 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
128 while (interfaces.hasMoreElements()) {
129 NetworkInterface ni = interfaces.nextElement();
130 if (!ni.isUp() || ni.isLoopback()) continue;
131
132 Enumeration<InetAddress> addresses = ni.getInetAddresses();
133 while (addresses.hasMoreElements()) {
134 InetAddress addr = addresses.nextElement();
135 if (addr instanceof Inet4Address) {
136 ips.add(addr.getHostAddress());
137 }
138 }
139 }
140 } catch (Exception ignored) {
141 }
142 return ips;
143 }
144
145 private void logStructuredJson(String appName, int port, String ip, long startupMs) {
146 log.info("STARTUP_JSON: {{\"app\":\"{}\",\"port\":{},\"ip\":\"{}\",\"startupMs\":{}}}",
147 appName, port, ip, startupMs);
148 }
149}
150