SpringBoot&Dubbo APP In Docker

这几天在实践 Dubbo 服务容器化, 踩了好多坑, 为此坚持写个文档记录下以作纪念.

构建 Dubbo 应用

为了快速搭建 Dubbo 应用, 我利用 Dubbo 提供的 idea 插件 intellij-idea-plugin, 直接制作了一个Demo 应用.

应用第一次编译错误

第一次编译时, 发现编译报错.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for com.sky.dubbo:demoApi:0.0.1-SNAPSHOT: Could not find artifact com.sky.dubbo:demo:pom:0.0.1-SNAPSHOT and 'parent.relativePath' points at no local POM @ line 14, column 13
[FATAL] Non-resolvable parent POM for com.sky.dubbo:demoProvider:0.0.1-SNAPSHOT: Could not find artifact com.sky.dubbo:demo:pom:0.0.1-SNAPSHOT and 'parent.relativePath' points at no local POM @ line 14, column 13
@
[ERROR] The build could not read 2 projects -> [Help 1]
[ERROR]
[ERROR] The project com.sky.dubbo:demoApi:0.0.1-SNAPSHOT (/Users/sky/other/git/demo/demoApi/pom.xml) has 1 error
[ERROR] Non-resolvable parent POM for com.sky.dubbo:demoApi:0.0.1-SNAPSHOT: Could not find artifact com.sky.dubbo:demo:pom:0.0.1-SNAPSHOT and 'parent.relativePath' points at no local POM @ line 14, column 13 -> [Help 2]
[ERROR]
[ERROR] The project com.sky.dubbo:demoProvider:0.0.1-SNAPSHOT (/Users/sky/other/git/demo/demoProvider/pom.xml) has 1 error
[ERROR] Non-resolvable parent POM for com.sky.dubbo:demoProvider:0.0.1-SNAPSHOT: Could not find artifact com.sky.dubbo:demo:pom:0.0.1-SNAPSHOT and 'parent.relativePath' points at no local POM @ line 14, column 13 -> [Help 2]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException

看报错发现是找不到 parent.relativePath, 是因为它在本地 m2 没有找到此 pom
为此, 可以把 demoApi 和 demoProvider 的 pom 修改一下, 再编译即可通过了.

1
2
3
4
5
6
7
8
9
<parent>
<groupId>com.sky.dubbo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- mine -->
<relativePath>../pom.xml</relativePath>
<!-- origin -->
<!-- <relativePath/> lookup parent from repository -->
</parent>

编译后 Jar 包无法直接运行

编译好后, 运行 demoProvider-0.0.1-SNAPSHOT.jar 发现系统提示

1
2
$ Java -jar demoProvider-0.0.1-SNAPSHOT.jar
no main manifest attribute, in demoProvider-0.0.1-SNAPSHOT.jar

于是检查了一下, demoProvider 提供的 build 配置

1
2
3
4
5
6
7
8
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

发现并没有配置 executions, 请添加 mine add 的配置, 会发现编译会生成预期的可执行 jar 包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- mine add -->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

启动应用

1
java -jar demoProvider-0.0.1-SNAPSHOT.jar

此处注意, 默认zookeeper的注册地址是 zookeeper://localhost:2181?client=curator 请自行修改为自己部署的zookeeper集群, 可参考前面的博客 dubbo-apllo-docker 做配置部署

登录 dubbo-admin 查看Provider状态
Provider

Dubbo App in Docker

为了使部署更加方便, 我打算集成使用 Docker 制作镜像来提供更加便捷的部署

集成 Maven Docker 插件

在网上找了一些相关资料之后,
添加 maven 插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>sky/${project.artifactId}</repository>
<tag>latest</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>

制作 Dockerfile

1
2
3
4
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/demoProvider-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

阅读参考

Spring Boot 2.0(四):使用 Docker 部署 Spring Boot

使用Docker容器化SpringBoot+Dubbo应用的实践

Docker部署Dubbo跨主机IP访问解决方案