您现在的位置是:首页 >技术杂谈 >10-Docker发布微服务网站首页技术杂谈
10-Docker发布微服务
简介10-Docker发布微服务
搭建SpringBoot项目
搭建一个简单的SpringBoot项目:
- 创建maven工程,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">
<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>
- 新建Module,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>
- 编写一个配置文件
server:
port: 6001
- 编写主启动类
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);
}
}
- 编写一个Controller
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 " + port + " " + UUID.randomUUID().toString();
}
@RequestMapping(value = "/order/index", method = RequestMethod.GET)
public String index() {
return "服务端口号:" + " " + port + " " + UUID.randomUUID().toString();
}
}
在Idea中运行没有问题时,将其使用maven的package
打成jar包。
发布微服务项目到Docker容器
- 将项目jar包上传到服务器
- 编写Dockerfile
FROM openjdk:8-oracle
MAINTAINER Snow [wb137725@163.com]
# 在主机 /var/lib/docker目录下创建一个临时文件,并链接到容器的 /tmp
VOLUME /tmp
# 将jar包添加到容器中,并命名为 springboot_docker.jar
ADD docker_boot-1.0-SNAPSHOT.jar springboot_docker.jar
# 运行jar包
RUN bash -c 'touch /springboot_docker.jar'
ENTRYPOINT ["java", "-jar", "/springboot_docker.jar"]
# SpringBoot项目配置的端口号为9999,需要将9999暴露出去
EXPOSE 9999
- 构建镜像
docker build -t springboot_docker:1.0 .
- 启动容器:
docker run -d -p 9999:9999 --name springboot springboot_docker:1.0
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。