面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB

环境准备

创建工程

面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB_第1张图片

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";
    }

}

第一种:直接main方法启动TxDemo2Application

面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB_第2张图片
测试访问
面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB_第3张图片

第二种:通过maven插件来启动

输入:

C:\Users\rlsl180506\Desktop\tx_demo2>mvn spring-boot:run

面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB_第4张图片
面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB_第5张图片
测试访问
面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB_第6张图片

第三种 打jar包来访问

C:\Users\rlsl180506\Desktop\tx_demo2>mvn clean package

面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB_第7张图片
进入jar所在路径执行

C:\Users\rlsl180506\Desktop\tx_demo2\target>java -jar tx_demo2-0.0.1-SNAPSHOT.jar

面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB_第8张图片
测试结果:
面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB_第9张图片

第四种 通过docker容器虚拟化运行

首先我要在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 

面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB_第10张图片
访问测试
面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB_第11张图片
更多完整微服务内容都在Q群中:264572737
面试题:springboot的三种启动方式是什么,还能说出第四种可以多给2000RMB_第12张图片

你可能感兴趣的:(高级java讲师介绍,架构师专题,视频教程)