一、通过IDEA新建一个普通微服务模块
1、建Moduel
<?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>
2、改POM
<?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>
3、写YML
server:port: 6001
4、主启动
package com.study;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author tengyer 2022/05/06 16:34*/
@SpringBootApplication
public class DockerBootApplication {public static void main(String[] args) {SpringApplication.run(DockerBootApplication.class, args);}
}
5、业务类
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;/*** @author tengyer 2022/05/06 16:35*/
@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();}
}
二、通过Dockerfile发布微服务部署到docker容器
1、IDEA工具里面打包微服务jar包
在IDEA中正常运行,将其使用maven的package打包成jar包并上传到/mydocker/目录下。
2、编写Dockerfile
在/mydocker/目录下新建Dockerfile文件
vim Dockerfile# 基础镜像使用java
FROM java:8
# 作者
MAINTAINER fanhe
# VOLUME指定临时文件目录为/tmp,在主机/var/lib/docker目录下创建一个临时文件并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为springboot_docker.jar
ADD docker_boot-0.0.1-SNAPSHOT.jar /fanhe_docker.jar
# 运行jar包
RUN bash -c 'touch /fanhe_docker.jar'
ENTRYPOINT ["java", "-jar", "/fanhe_docker.jar"]
# 暴露6001端口作为微服务
EXPOSE 6001
3、构建镜像
docker build -t fanhe_docker:1.6 .
4、运行容器
docker run -d -p 6001:6001 --name springboot fanhe_docker:1.6