十四、Docker 微服务实战
1、创建SpringBoot项目 hello_docker
1.1、项目结构
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liuchao</groupId>
<artifactId>hello_docker</artifactId>
<version>1.0-SNAPSHOT</version>
<name>hello_docker</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
</parent>
<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>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<!--spring cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<!--deploy 时忽略-->
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
- application.yml
## 激活对应的profile
spring:
profiles:
active: local
application:
name: hello_docker
---
#################################本地环境##########################
server:
port: 6001
config:
activate:
on-profile: local
---
- TestController
package com.liuchao.liuchao.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author liuchao
* @date 2022/12/13
*/
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping("/test01")
public String test01() {
return "01";
}
}
- DockerApplication
package com.liuchao.liuchao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author liuchao
* @date 2022/12/13
*/
@SpringBootApplication
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
}
2、编写Dockerfile文件
Dockerfile中几个点
- 自动安装jdk1.8
- 将hello_word项目生成的jar上传至容器
- 启动hello_word.jar
- 暴露端口 我这块用的端口是6001,即暴露6001端口
Dockerfile内容
# 基础镜像使用java
FROM java:8
# 作者
MAINTAINER liuchao
# VOLUME 指定临时文件目录为/tmp,在主机/var/lib/docker目录下创建了一个临时文件并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为hello_docker.jar
ADD hello_docker-1.0-SNAPSHOT.jar hello_docker.jar
# 运行jar包
RUN bash -c 'touch /hello_docker.jar'
ENTRYPOINT ["java","-jar","/hello_docker.jar"]
#暴露6001端口作为微服务
EXPOSE 6001
3、构建镜像
4、创建并运行容器
命令:docker run -d -p 6001:6001 hello_docker:1.0
5、访问测试
至此Docker 打包SpringBoot项目为镜像,创建并运行容器成功