refactor: demo/app-simple 更名为 demo/app
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package cn.odboy.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package cn.odboy.demo;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.net.InetAddress;
|
||||
import java.time.Duration;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.SpringVersion;
|
||||
import org.springframework.boot.SpringBootVersion;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 运行时信息接口,用于确认镜像里的 JDK / 时区 / 端口等是否符合预期。
|
||||
*/
|
||||
@RestController
|
||||
public class DemoController {
|
||||
|
||||
private static final ZoneId TZ8 = ZoneId.of("Asia/Shanghai");
|
||||
private static final DateTimeFormatter TIME_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss XXX");
|
||||
|
||||
@Value("${app.name:spring demo}")
|
||||
private String appName;
|
||||
|
||||
@Value("${server.port:0}")
|
||||
private int port;
|
||||
|
||||
/** 返回 JDK、Spring 版本、主机名、时区与运行时长,浏览器可直接看 JSON */
|
||||
@GetMapping({"/", "/api/info"})
|
||||
public Map<String, Object> info() {
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
long uptimeMillis = ManagementFactory.getRuntimeMXBean().getUptime();
|
||||
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("message", appName + " is running");
|
||||
body.put("java", System.getProperty("java.version"));
|
||||
body.put("javaHome", System.getProperty("java.home"));
|
||||
body.put("vm", System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version"));
|
||||
body.put("spring", SpringVersion.getVersion());
|
||||
body.put("application", "Spring Boot " + SpringBootVersion.getVersion());
|
||||
body.put("os", System.getProperty("os.name") + " " + System.getProperty("os.arch"));
|
||||
body.put("hostname", hostname());
|
||||
body.put("port", port);
|
||||
body.put("zoneId", ZoneId.systemDefault().getId());
|
||||
body.put("time", ZonedDateTime.now(TZ8).format(TIME_FMT));
|
||||
body.put("uptime", Duration.ofMillis(uptimeMillis).toString());
|
||||
body.put("memory", runtime.totalMemory() / 1024 / 1024 + "MB / max " + runtime.maxMemory() / 1024 / 1024 + "MB");
|
||||
return body;
|
||||
}
|
||||
|
||||
/** 带路径参数的示例接口 */
|
||||
@GetMapping("/api/hello/{name}")
|
||||
public Map<String, Object> hello(@PathVariable String name) {
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("message", "Hello, " + name + "!");
|
||||
body.put("time", ZonedDateTime.now(TZ8).format(TIME_FMT));
|
||||
return body;
|
||||
}
|
||||
|
||||
private static String hostname() {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostName();
|
||||
} catch (Exception e) {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cn.odboy.demo;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* 页面路由,模板见 src/main/resources/templates/index.html。
|
||||
*/
|
||||
@Controller
|
||||
public class PageController {
|
||||
|
||||
private final DemoController demoController;
|
||||
|
||||
public PageController(DemoController demoController) {
|
||||
this.demoController = demoController;
|
||||
}
|
||||
|
||||
/** 渲染 templates/index.html;只有 /page 返回页面,接口仍返回 JSON */
|
||||
@GetMapping("/page")
|
||||
public String page(Model model) {
|
||||
Map<String, Object> info = demoController.info();
|
||||
model.addAttribute("info", info);
|
||||
return "index";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
server:
|
||||
port: ${APP_PORT:8084}
|
||||
shutdown: graceful
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: springboot-jar17
|
||||
lifecycle:
|
||||
timeout-per-shutdown-phase: 20s
|
||||
|
||||
app:
|
||||
name: springboot-jar17
|
||||
|
||||
# 健康检查只需要端口探测,actuator 只暴露 health
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: health
|
||||
endpoint:
|
||||
health:
|
||||
show-details: never
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: INFO
|
||||
@@ -0,0 +1,27 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>springboot-jar17 运行信息</title>
|
||||
<style>
|
||||
body { font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; margin: 40px; color: #222; }
|
||||
h1 { font-size: 20px; }
|
||||
table { border-collapse: collapse; min-width: 520px; }
|
||||
th, td { border: 1px solid #ddd; padding: 6px 12px; text-align: left; }
|
||||
th { background: #f5f5f5; width: 160px; }
|
||||
code { background: #f5f5f5; padding: 1px 4px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>springboot-jar17 运行信息</h1>
|
||||
<table>
|
||||
<tr th:each="entry : ${info}">
|
||||
<th th:text="${entry.key}">key</th>
|
||||
<td th:text="${entry.value}">value</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>
|
||||
其他接口:<code>/api/info</code>、<code>/api/hello/{name}</code>、<code>/actuator/health</code>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user