1、创建微服务
-
创建空项目并在其中创建一个Module
这里使用start.aliyun.com
代替start.spring.io
,这样可以使用jdk8。
-
选择需要的配置或者直接在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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hpu</groupId> <artifactId>docker</artifactId> <version>0.0.1-SNAPSHOT</version> <name>docker</name> <description>docker</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-jdbc</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-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <!-- <dependency>--> <!-- <groupId>org.mybatis.spring.boot</groupId>--> <!-- <artifactId>mybatis-spring-boot-starter</artifactId>--> <!-- <version>2.2.2</version>--> <!-- </dependency>--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </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.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
-
修改 resource下application.properties配置文件
server.port=6001
-
DockerApplication启动类
package com.hpu.docker; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class DockerApplication { public static void main(String[] args) { SpringApplication.run(DockerApplication.class, args); } }
-
OrderController业务类
package com.hpu.docker.demos.web; 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; /** * @auther zyn * @create 2024-05-19 17:43 */ @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(); } }
-
执行主启动本地调试
-
打jar包上传至节点
scp /Users/***/zyn/JavaProject/Docker/docker/target/docker-0.0.1-SNAPSHOT.jar [email protected]:/root/service
2、通过Dockerfile发布微服务
-
编写Dockerfile
# 基础镜像使用java FROM java:8 # 根据操作系统架构不同,x86选择java:8,aarch64选择openjdk:8-jdk,否则报错The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested #FROM openjdk:8-jdk # 作者 MAINTAINER zyn # VOLUME 指定临时文件目录为/tmp,在主机/var/lib/docker目录下创建了一个临时文件并链接到容器的/tmp VOLUME /tmp # 将jar包添加到容器中并更名为zyn_docker.jar ADD docker-0.0.1-SNAPSHOT.jar zyn_docker.jar # 运行jar包 #RUN bash -c 'touch /zyn_docker.jar' ENTRYPOINT ["java","-jar","/zyn_docker.jar"] #暴露8080端口作为微服务 EXPOSE 6001
-
构建镜像
[root@zyn01 service]#docker build -t zynservice:1.0 . [root@zyn01 service]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zynservice 1.0 990e31e212d2 28 minutes ago 544MB
-
运行容器
docker run -d -p 6001:6001 zynservice:1.0
-
访问测试
curl 192.168.49.101:6001/order/index 服务端口号: 6001 8706d157-a12b-416c-9325-4e544bcc5e0a curl 192.168.49.101:6001/order/docker hello docker 6001 d20bba4e-0787-4021-917e-cb39a26b413d