initial commit

jfu
j.fu 6 months ago
parent 57320aaa5a
commit 865bf23b9d

@ -0,0 +1,9 @@
FROM openjdk:23-slim-bullseye
WORKDIR /opt/app
ARG JAR_FILE=target/spring-boot-web.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT [ "java","-jar","app.jar" ]
EXPOSE 8080

@ -0,0 +1,15 @@
构建docker镜像
```shell
docker build -t nudt-demo:1.0 .
```
运行docker镜像
```shell
docker run -d -p 8080:8080 -e demo.server.name=test1 nudt-demo:1.0
```
注意哦,其中`demo.server.name`是容器的名称,可自定义,例如,要启动两个容器实例,可执行:
```shell
docker run -d --network nudt --network-alias server1 -p 8080:8080 -e demo.server.name=server1 nudt-demo:1.0
docker run -d --network nudt --network-alias server2 -p 8081:8080 -e demo.server.name=server2 nudt-demo:1.0
```
其中,`--network nudt`表示将当前容器加入名为nudt的网络如果该网络不存在需要提前使用`docker network create nudt`命令创建网络;`--network-alias <alias-name>`表示将当前容器的网络主机名设置为`alias-name`

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/>
</parent>
<groupId>edu.nudt</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>spring-boot-web</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,27 @@
package edu.nudt.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.beans.factory.annotation.Value;
@SpringBootApplication
@Controller
public class DemoApplication {
@Value("${demo.server.name}")
private String serverName;
@GetMapping("/")
public String index(final Model model) {
model.addAttribute("title", "Backend Server: " + serverName);
model.addAttribute("msg", "欢迎来到分布式计算的世界!");
return "index";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

@ -0,0 +1,3 @@
spring.application.name=demo
demo.server.name="001"

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"/>
<link data-th-href="@{/css/main.css?{id}(id=${timestamp})}" rel="stylesheet">
<title>Hello Docker + Spring Boot</title>
</head>
<body>
<!-- <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="#">Mkyong.com</div></a>
</nav> -->
<main role="main" class="container">
<div class="starter-template">
<h1 th:text="${title}">Default title.</h1>
<p th:text="${msg}">Default text.</p>
</div>
</main>
<script data-th-src="@{/js/main.js}"></script>
</body>
</html>

@ -0,0 +1,13 @@
package edu.nudt.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
}
}
Loading…
Cancel
Save