docker微服务简单打包部署

  • ​​1、通过IDEA新建微服务模块​​
  • ​​1.1 目录结构如下:​​
  • ​​1.2 pom.xml​​
  • ​​1.3 配置文件​​
  • ​​1.4 主启动类​​
  • ​​1.5 业务类​​
  • ​​2、通过Dockerfile发布微服务部署到docker容器​​
  • ​​2.1 IDEA工具里面搞定微服务jar包​​
  • ​​2.2 编写Dockerfile​​
  • ​​2.3 构建镜像​​
  • ​​2.4 查看构建的镜像​​
  • ​​2.5 运行容器​​
  • ​​2.6 访问测试​​

这里只是测试单个微服务模块怎样打包成镜像并运行,后面会使用容器编排技术。

1、通过IDEA新建微服务模块

1.1 目录结构如下:

docker微服务简单打包部署_spring

1.2 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 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>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.atguigu.docker</groupId>
<artifactId>docker_boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>docker_boot</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.16</druid.version>
<mapper.version>4.1.5</mapper.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>
<dependencies>
<!--SpringBoot通用依赖模块-->
<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>
<!--test-->
<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.3 配置文件

application.properties或者yml文件都可以,我这里用的properties

server.port=6001

1.4 主启动类

@SpringBootApplication
public class DockerBootApplication {

public static void main(String[] args) {
SpringApplication.run(DockerBootApplication.class, args);
}

}

1.5 业务类

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 docker"+"\t"+port+"\t"+ UUID.randomUUID().toString();
}

@RequestMapping(value ="/order/index",method = RequestMethod.GET)
public String index()
{
return "服务端口号: "+"\t"+port+"\t"+UUID.randomUUID().toString();
}

}

2、通过Dockerfile发布微服务部署到docker容器

2.1 IDEA工具里面搞定微服务jar包

docker微服务简单打包部署_微服务_02

docker微服务简单打包部署_微服务_03

2.2 编写Dockerfile

Dockerfile内容如下:

# 基础镜像使用java
FROM java:8
# 作者
MAINTAINER xtt
# VOLUME 指定临时文件目录为/tmp,在主机/var/lib/docker目录下创建了一个临时文件并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为xtt_docker.jar
ADD docker_boot-0.0.1-SNAPSHOT.jar xtt_docker.jar
# 运行jar包
RUN bash -c 'touch /xtt_docker.jar'
ENTRYPOINT ["java","-jar","/xtt_docker.jar"]
#暴露6001端口作为微服务
EXPOSE 6001

将微服务jar包和Dockerfile文件上传到同一个目录下 /mydocker

docker微服务简单打包部署_intellij-idea_04

2.3 构建镜像

docker build -t xtt_docker:1.6 .

docker微服务简单打包部署_intellij-idea_05

2.4 查看构建的镜像

docker images

docker微服务简单打包部署_微服务_06

2.5 运行容器

docker run -d -p 6001:6001 xtt_docker:1.6

docker微服务简单打包部署_spring_07

2.6 访问测试

docker微服务简单打包部署_spring_08

docker微服务简单打包部署_docker_09

没有任何的问题,这里只是简单测试下单个服务模块怎样打包,后面将会使用容器编排一次性打包一堆微服务。