12-Docker发布微服务
Docker发布微服务
搭建SpringBoot项目
- 新建一个SpringBoot项目
- 选择依赖项
Spring Web
和Spring Boot Actuator
- 在
com.qi.docker_boot
下创建controller
目录,并在该目录下创建OrderController
的java类
OrderController
java类的内容如下:
package com.qi.docker_boot.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;
/**
* @ClassName OrderController
* @Description TODO
* @Author Bowen
* @Date 2024/10/30 0:01
* @Version 1.0
**/
@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();
}
}
- 修改
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>
<groupId>com.qi</groupId>
<artifactId>docker_boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>docker_boot</name>
<description>docker_boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.qi.docker_boot.DockerBootApplication</mainClass>
<skip>false</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 修改配置文件
application.properties
# 应用服务 WEB 访问端口
server.port=6001
- 查看主启动类(自动生成的)
package com.qi.docker_boot;
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);
}
}
- 在IDEA中运行并查看
- 查看Web端
http://127.0.0.1:6001/order/docker
http://127.0.0.1:6001/order/index
在IDEA中运行没有问题时,将其使用maven的package
打成jar包
发布微服务项目到Docker容器
-
将项目jar包上传到服务器
-
编写Dockerfile
FROM openjdk:8
MAINTAINER qi
# 在主机 /var/lib/docker目录下创建一个临时文件,并链接到容器的 /tmp
VOLUME /tmp
# 将jar包添加到容器中,并命名为 springboot_docker.jar
ADD docker_boot-0.0.1-SNAPSHOT.jar /springboot_docker.jar
# 运行jar包
RUN bash -c 'touch /springboot_docker.jar'
ENTRYPOINT ["java", "-jar", "/springboot_docker.jar"]
# SpringBoot项目配置的端口号为6001,需要将6001暴露出去
EXPOSE 6001
- 构建镜像
docker build -t springboot_docker:1.0 .
- 启动容器
docker run -d -p 6001:6001 --name springboot springboot_docker:1.0
查看Web
http://192.168.149.142:6001/order/docker
http://192.168.149.142:6001/order/index
http://ip:6001/order/index
中的ip
使用ifconfig
查找
你可能会遇到以下问题:
jdk8无法pull
错误代码
[root@docker spring-docker]# docker build -t springboot_docker:1.0 .
[+] Building 63.3s (2/2) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 558B 0.0s
=> ERROR [internal] load metadata for docker.io/library/openjdk:8-oracle 63.2s
------
> [internal] load metadata for docker.io/library/openjdk:8-oracle:
------
Dockerfile:1
--------------------
1 | >>> FROM openjdk:8-oracle
2 | MAINTAINER qi
3 |
--------------------
ERROR: failed to solve: openjdk:8-oracle: failed to do request: Head "https://registry.hub.docker.com/v2/library/openjdk/manifests/8-oracle?ns=docker.io": dial tcp 65.49.26.97:443: connect: connection refused
解决方案
使用代理下载
成功下载示例
jar包无法启动
错误代码
docker_boot-0.0.1-SNAPSHOT.jar中没有主清单属性
解决方法
更改pom.xml
文件,然后重新构建即可