时间:2023-01-30 10:34:51 | 栏目:JAVA代码 | 点击:次
创建工程
pom.xml内容
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.tx.springboot</groupId> <artifactId>tx_demo2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>tx_demo2</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
启动类TxDemo2Application
@SpringBootApplication public class TxDemo2Application { public static void main(String[] args) { SpringApplication.run(TxDemo2Application.class, args); } }
测试类TestController
@RestController public class TestController { @RequestMapping("/hello") public String hello(){ return "hello"; } }
测试访问
输入:
C:\Users\rlsl180506\Desktop\tx_demo2>mvn spring-boot:run
测试访问
C:\Users\rlsl180506\Desktop\tx_demo2>mvn clean package
进入jar所在路径执行
C:\Users\rlsl180506\Desktop\tx_demo2\target>java -jar tx_demo2-0.0.1-SNAPSHOT.jar
测试结果:
首先我要在linux的docker环境下。其次把第三步打好的jar拷贝到linux下的指定目录,修改名字成tx_demo2.jar
mv tx_demo2-0.0.1-SNAPSHOT.jar tx_demo2.jar
创建DockerFile文件
FROM openjdk:8-jdk-alpine ARG JAR_FILE COPY ${JAR_FILE} app.jar EXPOSE 10001 ENTRYPOINT ["java","-jar","/app.jar"]
tx_demo2.jar和DockerFile在同一个路径构建镜像
docker build --build-arg JAR_FILE=tx_demo2.jar -t tx_demo2:1.0 .
启动容器:
docker run -p 8080:8080 tx_demo2:1.0
访问测试