DockerMaven插件
微服务部署有两种方法
(1)手动部署:
首先基于源码打包生成jar包(或war包),将jar包(或war包)上传至虚拟机并拷贝至JDK容器。
(2)通过Maven插件自动部署。
对于数量众多的微服务,手动部署无疑是非常麻烦的做法,并且容易出错。所以我们这里学习如何自动部署,这也是企业实际开发中经常使用的方法。
Maven插件自动部署步骤
(1)修改宿主机的docker配置
修改宿主机的docker配置,让其可以远程访问
[root@discovery ~]# vim /lib/systemd/system/docker.service
......
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
......
其中ExecStart=后添加配置 ‐H tcp://0.0.0.0:2375 ‐H unix:///var/run/docker.sock
修改后如下:
(2)刷新配置,重启服务
[root@discovery ~]# systemctl daemon-reload
[root@discovery ~]# systemctl restart docker
[root@discovery ~]# docker start registry
(3)在eureka工程pom.xml 增加配置
<build>
<finalName>app</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.mooc.house.eurekaserver.EurekaSeverApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
</goals>
</execution>
</executions>
</plugin>
<!-- docker的maven插件,官网 https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>192.168.2.10:5000/${project.artifactId}:${project.version}</imageName>
<baseImage>jdk1.8</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>
<dockerHost>http://192.168.2.10:2375</dockerHost>
</configuration>
</plugin>
</plugins>
</build>
说明:以上配置会自动生成Dockerfile
FROM jdk1.8
ADD app.jar /
ENTRYPOINT ["java","-jar","/app.jar"]
(4)在windows执行打包命令
在windows的命令提示符下,进入eureka工程所在的目录,输入以下命令,进行打包和上传镜像
mvn clean package docker:build -DpushImage
执行后,会有如下输出,代码正在上传
......
378bc4046127: Pushing [========================================> ] 32.38MB/40.11MB
378bc4046127: Pushing [==========================================> ] 34.08MB/40.11MB
378bc4046127: Pushing [=============================================> ] 36.21MB/40.11MB
378bc4046127: Pushing [===============================================> ] 37.91MB/40.11MB
378bc4046127: Pushing [=================================================> ] 40.04MB/40.11MB
378bc4046127: Pushing [==================================================>] 40.11MB
378bc4046127: Pushed
......
浏览器访问 http://192.168.184.135:5000/v2/_catalog ,输出
(5)进入宿主机 , 查看镜像
[root@discovery ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.2.10:5000/eureka-sever 1.0.0-SNAPSHOT 770c1bb00970 About a minute ago 629MB
192.168.2.10:5000/jdk1.8 latest 365aa3d368ee 4 hours ago 589MB
说明:输出如上内容,表示eureka-sever微服务已经做成镜像;
(6) 启动容器
[root@discovery ~]# docker run -d --name=eureka-sever -p 8666:8666 192.168.2.10:5000/eureka-sever:1.0.0-SNAPSHOT
[root@discovery ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a067a7df5a2 192.168.2.10:5000/eureka-sever:1.0.0-SNAPSHOT "java -jar /app.jar" 6 seconds ago Up 4 seconds 0.0.0.0:8666->8666/tcp eureka-sever
d14a85281d0f registry "/entrypoint.sh /etc…" 6 hours ago Up 20 minutes 0.0.0.0:5000->5000/tcp registry
[root@discovery ~]#