项目结构
本项目采用maven构建,有三个模块,分别是pms-interfaces, pms-services, pms-portal。
pms-interfaces | 接口层,只能存放实体,枚举,接口。 |
pms-services | 服务层,使用 dubbo向外暴露服务 |
pms-portal | 用户界面层,使用前后端分离的方式来构建用户界面,它又分为frontend和spring mvc两层。其中spring mvc 的controller负责访问dubbo服务,然后向frontend提供restful服务。 |
项目结构截图如下:
使用docker-maven-plugin
- 其中parent POMs和 pms-interfaces不需要构造镜像,因此需要跳过。
<!--build plugin-->
<build>
<plugins>
<!--docker-maven-plugin-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<skipDockerBuild>true</skipDockerBuild>
</configuration>
</plugin>
</plugins>
</build>
- pms-services的配置
- POM配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--docker-maven-plugin-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<skipDockerBuild>false</skipDockerBuild>
<imageName>${project.name}:${project.version}</imageName>
<dockerDirectory>${project.basedir}/docker</dockerDirectory>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
- Dockerfile的定义,位置在项目目录下的docker文件夹中(${project.basedir}/docker)
# Dockerfile
FROM java:8-jre
# 设置工作目录
WORKDIR /app
# 将jar文件拷贝到镜像中。注:docker-maven-plugin 会将jar文件拷贝到构建目录target/docker/中
COPY ./pms-services-1.0-SNAPSHOT.jar /app/app.jar
EXPOSE 20880
CMD ["java", "-jar", "/app/app.jar"]
- pms-portal的配置
- POM配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--docker-maven-plugin-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<skipDockerBuild>false</skipDockerBuild>
<imageName>${project.name}:${project.version}</imageName>
<dockerDirectory>${project.basedir}/docker</dockerDirectory>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
- Dockerfile的定义,位置在项目目录下的docker文件夹中(${project.basedir}/docker)
# Dockerfile
FROM java:8-jre
# 设置工作目录
WORKDIR /app
# 将jar文件拷贝到镜像中。注:docker-maven-plugin 会将jar文件拷贝到构建目录target/docker/中
COPY ./pms-portal-1.0-SNAPSHOT.jar /app/app.jar
EXPOSE 8000
CMD ["java", "-jar", "/app/app.jar"]
- 注意:在使用docker-maven-plugin时,还需要在~/.m2/settings.xml中的pluginGroups节点下增加com.your.plugins
<!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
|-->
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
<pluginGroup>com.spotify</pluginGroup>
</pluginGroups>
- 开始构建镜像
- 在docker所在服务中配置tcp绑定
[root@localhost ~]# cd /etc/docker
[root@localhost ~]# vim daemon.json
{
"registry-mirrors": ["http://b24820b7.m.daocloud.io"],
"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
}
[root@localhost ~]# service docker restart
- 在开发环境中配置环境变量DOCKER_HOST
- 开始构建镜像
mvn clean package docker:build -Dmaven.test.skip=true
在服务器上运行容器并测试
- 依次运行pms-services, pms-portal
[root@localhost ~]# docker run -d pms-services:1.0-SNAPSHOT
[root@localhost ~]# docker run -d -p 8000:8000 pms-portal:1.0-SNAPSHOT
- 登录zookeeper gui查看dubbo服务注册信息
# provider
dubbo://172.17.0.2:20880/com.lovefly.pms.interfaces.service.TestService?anyhost=true&application=pms-services&dubbo=2.5.3&interface=com.lovefly.pms.interfaces.service.TestService&methods=echo&pid=1&revision=1.0-SNAPSHOT&side=provider×tamp=1504436089577
# consumer
consumer://172.17.0.3/com.lovefly.pms.interfaces.service.TestService?application=pms-portal&category=consumers&check=false&dubbo=2.5.3&interface=com.lovefly.pms.interfaces.service.TestService&methods=echo&pid=1&revision=1.0-SNAPSHOT&side=consumer×tamp=1504436100128
在浏览器中访问pms-portal
使用curl测试pms-portal
[root@localhost docker]# curl -X GET http://192.168.41.128:8000/api/echo?input=ping2222
ping2222: pong from service
[root@localhost docker]#
在容器中配置时区的两种方式
- 通过docker-compose的volumes配置,跟宿主服务器操持一致
volumes:
- "/etc/timezone:/etc/timezone:ro"
- "/etc/localtime:/etc/localtime:ro"
- 通过环境变量设置时区
# 设置时区
ENV TZ=Asia/Shanghai
在容器中设置hosts
- 通过docker-compose的extra_host配置
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
- 通过命令行参数--add-host设置
[root@localhost ~]# docker run -d --add-host "service.ielong.cn":172.17.0.4 --add-host "servicewx.ielong.cn":172.17.0.5 pms-services:1.0-SNAPSHOT
参考文档: