对于Docker
的概念和操作可以看之前的文章:
在进行实操前,先要在个人电脑安装Docker环境,没有环境的可以点这里 根据教程装环境。
仅用插件打镜像 这里的项目基于Spring Cloud 教程一:Eureka 这一篇构建的项目基础上展开:
修改eureka-service
的defaultZone配置, 这里不能使用localhost,可以使用ip地址,也可之使用hosts文件中配置的主机,这里直接使用ip地址:
1 2 3 4 5 6 7 8 9 10 11 server: port: 9090 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://192.168.30.5:9090/eureka
在pom.xml中添加插件配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <imageName>${project.artifactId}:${project.version}</imageName> <baseImage>openjdk:11-jre-slim</baseImage> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build>
配置完成后,在控制台运行命令
1 mvn clean package docker:build
如果控制台报这个错误:
1 No plugin found for prefix 'docker' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (e:\maven\repository), nexus (https://nexus.nroad.com.cn/repository/maven-public)] -> [Help 1]
请在maven的配置文件中添加下面内容:
1 2 3 4 5 <settings> <pluginGroups> <pluginGroup>com.spotify</pluginGroup> </pluginGroups> </settings>
获取部分打包日志如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Pulling from library/openjdk 8d691f585fa8: Pull complete 3da6fe7ff2ef: Pull complete e22147996cc0: Pull complete 8df48a2d4467: Pull complete Digest: sha256:d7593b97425e5faaa67d1c556b6b9f6b18282ba73640206bfdc73f65f7245fa0 Status: Downloaded newer image for openjdk:11-jre-slim ---> 0e452dba629c Step 2/3 : ADD /eureka-server-0.0.1-SNAPSHOT.jar // ---> ad4570bb025a Step 3/3 : ENTRYPOINT ["java", "-jar", "/eureka-server-0.0.1-SNAPSHOT.jar"] ---> Running in fbdd61b36a8a Removing intermediate container fbdd61b36a8a ---> ce64f5b52ae4 ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null} Successfully built ce64f5b52ae4 Successfully tagged eureka-server:0.0.1-SNAPSHOT
构建成功后,控制台执行docker images
查看是否有刚刚构建的镜像。
启动新构建的镜像:
1 docker run -p 9090:9090 eureka-server:0.0.1-SNAPSHOT
浏览器查看 http://localhost:9090
Dockerfile打镜像 上面演示了如何直接用插件构建Docker镜像, 接下来我们采用更有可读性的Dockerfile来构建Docker镜像。
修改eureka-client
的defaultZone配置, 这里不能使用localhost,直接使用ip地址:
1 2 3 4 5 6 7 8 9 10 11 server: port: 8040 eureka: client: service-url: defaultZone: http://192.168.30.5:9090/eureka/ spring: application: name: erueka-client
在pom.xml中添加插件配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <imageName>${project.artifactId}:${project.version}</imageName> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build>
这里的dockerDirectory
是配置Dockerfile
文件的位置。
在src/main/docker下新建Dockerfile文件,内容如下:
1 2 3 4 5 FROM openjdk:11-jre-slim VOLUME /tmp ADD eureka-client-0.0.1-SNAPSHOT.jar app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
和直接使用插件一样构建镜像,并且启动镜像:
1 2 mvn clean package docker:build docker run -p 8040:8040 eureka-client:0.0.1-SNAPSHOT
然后看eureka-service的页面是否已经注册上。
使用docker compose部署已有镜像服务 使用docker compose
来编排部署微服务, 可以统一管理,避免单独部署的麻烦。
新建docker-compose.yml
文件, 内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 version: "3" services: eureka-server: image: eureka-server:0.0.1-SNAPSHOT ports: - 9090:9090 eureka-client: image: eureka-client:0.0.1-SNAPSHOT ports: - 8040:8040 depends_on: - eureka-server
在文件所在目录执行命令
查看 http://localhost:9090 , 检查eureka-client是否注册上。
点击进入源码仓库