搭建SpringBoot项目
搭建一个简单的SpringBoot项目:
- 创建maven工程,pom为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <?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>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> </parent>
<groupId>org.study</groupId> <artifactId>test-docker</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>docker_boot</module> </modules>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties>
</project>
|
- 新建Module,pom为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <?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"> <parent> <artifactId>test-docker</artifactId> <groupId>org.study</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>docker_boot</artifactId>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</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-actuator</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>
|
- 编写一个配置文件
- 编写主启动类
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.study;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class DockerBootApplication { public static void main(String[] args) { SpringApplication.run(DockerBootApplication.class, args); } }
|
- 编写一个Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.study.controller;
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
@RestController public class OrderController { @Value("${server.port}") private String port;
@RequestMapping("/order/docker") public String helloDocker() { return "hello world \t" + port + "\t" + UUID.randomUUID().toString(); }
@RequestMapping(value = "/order/index", method = RequestMethod.GET) public String index() { return "服务端口号:" + "\t" + port + "\t" + UUID.randomUUID().toString(); } }
|
在Idea中运行没有问题时,将其使用maven的package
打成jar包。
发布微服务项目到Docker容器
- 将项目jar包上传到服务器
- 编写Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| FROM openjdk:8-oracle MAINTAINER lee
VOLUME /tmp
ADD docker_boot-1.0-SNAPSHOT.jar /springboot_docker.jar
RUN bash -c 'touch /springboot_docker.jar' ENTRYPOINT ["java", "-jar", "/springboot_docker.jar"]
EXPOSE 6001
|
将微服务jar包和Dockerfile文件上传到同一个目录下/mydocker
- 构建镜像
1
| docker build -t springboot_docker:1.0 .
|
- 启动容器:
1
| docker run -d -p 6001:6001 --name springboot springboot_docker:1.0
|