欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 会展 > 12-Docker发布微服务

12-Docker发布微服务

2024/11/5 3:16:17 来源:https://blog.csdn.net/qq_59621600/article/details/143375405  浏览:    关键词:12-Docker发布微服务

12-Docker发布微服务

Docker发布微服务

搭建SpringBoot项目

  • 新建一个SpringBoot项目

image-20241030001910004

  • 选择依赖项Spring WebSpring Boot Actuator

image-20241030002221969

  • com.qi.docker_boot下创建controller目录,并在该目录下创建OrderController的java类

image-20241030002441466

  • OrderControllerjava类的内容如下:
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中运行并查看

image-20241030003035295

  • 查看Web端

http://127.0.0.1:6001/order/docker

image-20241030003132966

http://127.0.0.1:6001/order/index

image-20241030003156051

在IDEA中运行没有问题时,将其使用maven的package打成jar包

image-20241030003413430

发布微服务项目到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

image-20241030210817825

查看Web

http://192.168.149.142:6001/order/docker

image-20241030211957377

http://192.168.149.142:6001/order/index

image-20241030212019297

http://ip:6001/order/index中的ip使用ifconfig查找

你可能会遇到以下问题:

jdk8无法pull

错误代码

image-20241030211130109

[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-oracle2 |     MAINTAINER qi3 |     
--------------------
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
解决方案

使用代理下载

image-20241030211225607

成功下载示例

image-20241030211343586

jar包无法启动

错误代码
docker_boot-0.0.1-SNAPSHOT.jar中没有主清单属性
解决方法

更改pom.xml文件,然后重新构建即可

image-20241030211822813

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com