Docker 安装 Dubbo Admin 单机教程

大纲

前言

本文将介绍如何使用 Docker + Docker Compose 安装 Dubbo Admin 单实例,包括 ZooKeeper 与 MySQL 的安装。

官方资源

Docker 资源

准备工作

ZooKeeper 安装

这里使用 ZooKeeper 作为注册中心,Dubbo Admin 会从 ZooKeeper 中获取需要监控的 Dubbo 服务列表。

  • 创建数据目录,用于存放 ZooKeeper 的数据
1
2
3
4
5
# 数据目录
$ sudo mkdir -p /data/zookeeper/data

# 日志目录
$ sudo mkdir -p /data/zookeeper/datalog
  • 创建 Docker Compose 的配置文件,并加入以下 ZooKeeper 容器的配置内容
1
$ vi docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
version: '3.5'

services:

zookeeper:
image: zookeeper:3.8.4
container_name: zookeeper
restart: always
ports:
- 2181:2181
environment:
TZ: Asia/Shanghai
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=zookeeper:2888:3888;2181
volumes:
- /data/zookeeper/data:/data
- /data/zookeeper/datalog:/datalog
networks:
- dubbo-network

networks:
dubbo-network:
driver: bridge
核心配置配置说明
ZOO_MY_ID: 1节点 ID,单机模式下可以固定为 1,如果是集群模式,必须全局唯一。
ZOO_SERVERS: server.1=zookeeper:2888:3888;2181- 单机模式下的服务地址,如果是集群模式,可以指定多个服务地址(使用空格分隔开)。
- 这里的 zookeeper 是在 Docker Compose 中定义的 ZooKeeper 服务的名称,用作其他服务(如 Dubbo Admin)访问 ZooKeeper 的主机名,Docker Compose 会自动将这个名称解析为 ZooKeeper 容器的 IP 地址。
  • 创建并启动 ZooKeeper 容器
1
$ sudo docker compose up -d
  • 查看 ZooKeeper 容器的启动日志
1
$ sudo docker logs zookeeper

开始安装

创建配置文件

  • 在宿主机内创建 Dubbo Admin 的 application.properties 配置文件,并根据 Dubbo Admin 镜像的版本,从 这里 获取 application.properties 配置文件对应的内容,比如 0.6.0 版本
1
2
3
4
5
# 创建配置目录
$ sudo mkdir -p /data/dubbo-admin/conf

# 创建配置文件
$ sudo touch /data/dubbo-admin/conf/application.properties
  • 编辑 Dubbo Admin 的 application.properties 配置文件,并写入相应的配置内容,以下配置内容对应是 Dubbo Admin 镜像的 0.6.0 版本
1
$ sudo vi /data/dubbo-admin/conf/application.properties
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

server.port=38080
dubbo.protocol.port=30880
dubbo.application.qos-port=32222

# centers in dubbo, if you want to add parameters, please add them to the url
admin.registry.address=zookeeper://zookeeper:2181
admin.config-center=zookeeper://zookeeper:2181
admin.metadata-report.address=zookeeper://zookeeper:2181

# nacos config, add parameters to url like username=nacos&password=nacos
#admin.registry.address=nacos://zookeeper:8848?group=DEFAULT_GROUP&namespace=public
#admin.config-center=nacos://zookeeper:8848?group=dubbo
#admin.metadata-report.address=nacos://zookeeper:8848?group=dubbo

#group (Deprecated it is recommended to use URL to add parameters,will be removed in the future)
#admin.registry.group=dubbo
#admin.config-center.group=dubbo
#admin.metadata-report.group=dubbo

#namespace used by nacos. (Deprecated it is recommended to use URL to add parameters,will be removed in the future)
#admin.registry.namespace=public
#admin.config-center.namespace=public
#admin.metadata-report.namespace=public

admin.root.user.name=root
admin.root.user.password=root

#session timeout, default is one hour
admin.check.sessionTimeoutMilli=3600000


# apollo config
# admin.config-center = apollo://localhost:8070?token=e16e5cd903fd0c97a116c873b448544b9d086de9&app.id=test&env=dev&cluster=default&namespace=dubbo

# (Deprecated it is recommended to use URL to add parameters,will be removed in the future)
#admin.apollo.token=e16e5cd903fd0c97a116c873b448544b9d086de9
#admin.apollo.appId=test
#admin.apollo.env=dev
#admin.apollo.cluster=default
#admin.apollo.namespace=dubbo

#compress
server.compression.enabled=true
server.compression.mime-types=text/css,text/javascript,application/javascript
server.compression.min-response-size=10240

#token timeout, default is one hour
admin.check.tokenTimeoutMilli=3600000
#Jwt signingKey
admin.check.signSecret=86295dd0c4ef69a1036b0b0c15158d77

#dubbo config
dubbo.application.name=dubbo-admin
dubbo.registry.address=${admin.registry.address}

# mysql
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/dubbo-admin?characterEncoding=utf8&connectTimeout=1000&socketTimeout=10000&autoReconnect=true
#spring.datasource.username=root
#spring.datasource.password=mysql

# h2
spring.datasource.url=jdbc:h2:mem:~/dubbo-admin;MODE=MYSQL;
spring.datasource.username=sa
spring.datasource.password=

# id generate type
mybatis-plus.global-config.db-config.id-type=none

dubbo.application.logger=slf4j
核心配置配置说明
server.port=38080Dubbo Admin 的 Web 服务端口
dubbo.protocol.port=30880Dubbo 协议的端口
dubbo.application.qos-port=32222Dubbo QOS 服务的端口
admin.root.user.name=rootDubbo Admin 中 root 用户的名称,用于登录 Dubbo Admin 控制台
admin.root.user.password=rootDubbo Admin 中 root 用户的密码,用于登录 Dubbo Admin 控制台
admin.registry.address注册中心地址,当使用 ZooKeeper 作为注册中心,则必须指定为 zookeeper://zookeeper:2181。如果连接的是 ZooKeeper 集群,那么 zookeeper:// 后面使用逗号分隔开多个集群节点的地址。
admin.config-center配置中心地址,当使用 ZooKeeper 作为配置中心,则必须指定为 zookeeper://zookeeper:2181。如果连接的是 ZooKeeper 集群,那么 zookeeper:// 后面使用逗号分隔开多个集群节点的地址。
admin.metadata-report.address元数据中心地址,当使用 ZooKeeper 作为元数据中心,则必须指定为 zookeeper://zookeeper:2181。如果连接的是 ZooKeeper 集群,那么 zookeeper:// 后面使用逗号分隔开多个集群节点的地址。

Dubbo Admin 官方文档

创建 Docker 容器

  • 编辑 Docker Compose 的配置文件,并加入以下 Dubbo Admin 容器的配置内容
1
$ vi docker-compose.yml
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
version: '3.5'

services:

zookeeper:
image: zookeeper:3.8.4
container_name: zookeeper
restart: always
ports:
- 2181:2181
environment:
TZ: Asia/Shanghai
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=zookeeper:2888:3888;2181
volumes:
- /data/zookeeper/data:/data
- /data/zookeeper/datalog:/datalog
networks:
- dubbo-network

dubbo-admin:
image: apache/dubbo-admin:0.6.0
container_name: dubbo-admin
restart: always
ports:
- 38080:38080
- 30880:30880
- 32222:32222
environment:
- TZ=Asia/Shanghai
volumes:
- /data/dubbo-admin/conf:/config
networks:
- dubbo-network
depends_on:
- zookeeper

networks:
dubbo-network:
driver: bridge
  • 创建并启动 Dubbo Admin 容器
1
$ sudo docker compose up -d

若不希望指定 Dubbo Admin 的 application.properties 配置文件,而是想让 Docker Compose 通过环境变量来配置 Dubbo Admin,可以参考 这里 的 YML 配置内容,配置示例如下:

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
27
28
29
30
31
version: '3.5'

service:

......

dubbo-admin:
image: apache/dubbo-admin:0.6.0
container_name: dubbo-admin
restart: always
ports:
- 38080:38080
- 30880:30880
- 32222:32222
environment:
- TZ=Asia/Shanghai
- admin.registry.address=zookeeper://zookeeper:2181
- admin.config-center=zookeeper://zookeeper:2181
- admin.metadata-report.address=zookeeper://zookeeper:2181
- dubbo.application.name=dubbo-admin
- dubbo.registry.address=zookeeper://zookeeper:2181
- spring.datasource.url=jdbc:h2:mem:~/dubbo-admin;MODE=MYSQL;
- spring.datasource.username=sa
networks:
- dubbo-network
depends_on:
- zookeeper

networks:
dubbo-network:
driver: bridge

验证 Docker 容器

浏览器通过 http://192.168.1.235:38080 访问 Dubbo Admin 的控制台页面,请自行将 IP 地址 192.168.1.235 更改为 Dubbo Admin 容器的真实 IP 地址,默认的登录用户名和密码是 root /root

数据持久化

在默认情况下,Dubbo Admin 使用的是 H2 嵌入式数据库(内存模式),应用重启后会丢失监控数据,建议使用 MySQL 来存储 Dubbo Admin 的监控数据。

MySQL 安装

  • 创建数据目录,用于存放 MySQL 的数据
1
$ sudo mkdir -p /data/mysql
  • 创建 Jar 包目录,并下载 MySQL 的驱动包
1
2
3
4
5
6
7
8
# 创建 Jar 包目录
$ sudo mkdir -p /data/dubbo-admin/opt-libs

# 下载 MySQL 驱动包
$ sudo wget -P /data/dubbo-admin/opt-libs https://repo1.maven.org/maven2/com/mysql/mysql-connector-j/8.2.0/mysql-connector-j-8.2.0.jar

# 文件授权访问
$ sudo chmod -R 777 /data/dubbo-admin/opt-libs
  • 编辑 Docker Compose 的配置文件,并加入以下 MySQL 容器的配置内容
1
$ vi docker-compose.yml
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
version: '3.5'

services:

mysql:
image: mysql:8.4.2
container_name: mysql
command: ["mysqld", "--mysql-native-password=ON"]
restart: always
ports:
- 3306:3306
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_HOST: '%'
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: dubbo-admin
volumes:
- /data/mysql:/var/lib/mysql
networks:
- dubbo-network

zookeeper:
image: zookeeper:3.8.4
container_name: zookeeper
restart: always
ports:
- 2181:2181
environment:
TZ: Asia/Shanghai
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=zookeeper:2888:3888;2181
volumes:
- /data/zookeeper/data:/data
- /data/zookeeper/datalog:/datalog
networks:
- dubbo-network

dubbo-admin:
image: apache/dubbo-admin:0.6.0
container_name: dubbo-admin
restart: always
ports:
- 38080:38080
- 30880:30880
- 32222:32222
environment:
- TZ=Asia/Shanghai
volumes:
- /data/dubbo-admin/conf:/config
- /data/dubbo-admin/opt-libs:/opt-libs
networks:
- dubbo-network
depends_on:
- mysql
- zookeeper

networks:
dubbo-network:
driver: bridge
核心配置配置说明
MYSQL_ROOT_HOST: '%'允许 root 用户远程访问
MYSQL_ROOT_PASSWORD: root设置 root 用户的密码
MYSQL_DATABASE: dubbo-admin容器启动时自动创建指定的数据库
command: ["mysqld", "--mysql-native-password=ON"]启用 MySQL 传统的身份认证插件
  • 编辑 Dubbo Admin 的 application.properties 配置文件,添加 MySQL 数据库的配置,并注释掉 H2 数据库的配置。特别注意,MySQL 8 的驱动类是 com.mysql.cj.jdbc.Driver,而不是 com.mysql.jdbc.Driver
1
2
3
4
5
6
7
8
9
10
# mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://mysql:3306/dubbo-admin?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root

# h2
# spring.datasource.url=jdbc:h2:mem:~/dubbo-admin;MODE=MYSQL;
# spring.datasource.username=sa
# spring.datasource.password=

若不希望指定 Dubbo Admin 的 application.properties 配置文件,而是想让 Docker Compose 通过环境变量来配置 Dubbo Admin,可以参考 这里 的 YML 配置内容,配置示例如下:

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
27
28
29
30
31
32
33
34
35
36
version: '3.5'

service:

......

dubbo-admin:
image: apache/dubbo-admin:0.6.0
container_name: dubbo-admin
restart: always
ports:
- 38080:38080
- 30880:30880
- 32222:32222
environment:
- TZ=Asia/Shanghai
- admin.registry.address=zookeeper://zookeeper:2181
- admin.config-center=zookeeper://zookeeper:2181
- admin.metadata-report.address=zookeeper://zookeeper:2181
- dubbo.application.name=dubbo-admin
- dubbo.registry.address=zookeeper://zookeeper:2181
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- spring.datasource.url=jdbc:mysql://mysql:3306/dubbo-admin?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
- spring.datasource.username=root
- spring.datasource.password=root
volumes:
- /data/dubbo-admin/opt-libs:/opt-libs
networks:
- dubbo-network
depends_on:
- mysql
- zookeeper

networks:
dubbo-network:
driver: bridge
  • 创建并启动 MySQL 容器
1
$ sudo docker compose up -d
  • 查看 MySQL 容器的启动日志
1
$ sudo docker logs mysql
  • 查看 Dubbo Admin 容器的启动日志
1
$ sudo docker logs dubbo-admin

常见问题

连接 ZooKeeer 集群

若 Dubbo Admin 连接的是 ZooKeeper 集群,那么可以参考以下的 Docker Compose 配置内容:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
version: '3.5'

services:
zookeeper01:
image: zookeeper:3.8.4
container_name: zookeeper01
restart: always
hostname: zookeeper01
ports:
- 2181:2181
environment:
TZ: Asia/Shanghai
ZOO_MY_ID: 1
ZOO_PORT: 2181
ZOO_4LW_COMMANDS_WHITELIST: ruok
ZOO_SERVERS: server.1=zookeeper01:2888:3888;2181 server.2=zookeeper02:2888:3888;2181 server.3=zookeeper03:2888:3888;2181
healthcheck:
test: ["CMD", "sh", "-c", "echo ruok | nc localhost 2181 | grep imok"]
interval: 30s
timeout: 10s
retries: 5
start_period: 20s
volumes:
- /data/zookeeper/zookeeper01/data:/data
- /data/zookeeper/zookeeper01/datalog:/datalog
networks:
- dubbo-network

zookeeper02:
image: zookeeper:3.8.4
container_name: zookeeper02
restart: always
hostname: zookeeper02
ports:
- 2182:2181
environment:
TZ: Asia/Shanghai
ZOO_MY_ID: 2
ZOO_PORT: 2181
ZOO_4LW_COMMANDS_WHITELIST: ruok
ZOO_SERVERS: server.1=zookeeper01:2888:3888;2181 server.2=zookeeper02:2888:3888;2181 server.3=zookeeper03:2888:3888;2181
healthcheck:
test: ["CMD", "sh", "-c", "echo ruok | nc localhost 2181 | grep imok"]
interval: 30s
timeout: 10s
retries: 5
start_period: 20s
volumes:
- /data/zookeeper/zookeeper02/data:/data
- /data/zookeeper/zookeeper02/datalog:/datalog
networks:
- dubbo-network

zookeeper03:
image: zookeeper:3.8.4
container_name: zookeeper03
restart: always
hostname: zookeeper03
ports:
- 2183:2181
environment:
TZ: Asia/Shanghai
ZOO_MY_ID: 3
ZOO_PORT: 2181
ZOO_4LW_COMMANDS_WHITELIST: ruok
ZOO_SERVERS: server.1=zookeeper01:2888:3888;2181 server.2=zookeeper02:2888:3888;2181 server.3=zookeeper03:2888:3888;2181
healthcheck:
test: ["CMD", "sh", "-c", "echo ruok | nc localhost 2181 | grep imok"]
interval: 30s
timeout: 10s
retries: 5
start_period: 20s
volumes:
- /data/zookeeper/zookeeper03/data:/data
- /data/zookeeper/zookeeper03/datalog:/datalog
networks:
- dubbo-network

dubbo-admin:
image: apache/dubbo-admin:0.6.0
container_name: dubbo-admin
restart: always
ports:
- 38080:38080
- 30880:30880
- 32222:32222
environment:
- TZ=Asia/Shanghai
- admin.registry.address=zookeeper://zookeeper01:2181,zookeeper02:2181,zookeeper03:2181
- admin.config-center=zookeeper://zookeeper01:2181,zookeeper02:2181,zookeeper03:2181
- admin.metadata-report.address=zookeeper://zookeeper01:2181,zookeeper02:2181,zookeeper03:2181
- dubbo.application.name=dubbo-admin
- dubbo.registry.address=zookeeper://zookeeper01:2181,zookeeper02:2181,zookeeper03:2181
- spring.datasource.url=jdbc:h2:mem:~/dubbo-admin;MODE=MYSQL;
- spring.datasource.username=sa
networks:
- dubbo-network
depends_on:
zookeeper01:
condition: service_healthy
zookeeper02:
condition: service_healthy
zookeeper03:
condition: service_healthy

networks:
dubbo-network:
driver: bridge

无法加载 MySQL 驱动类

Dubbo Admin 容器在启动时,抛出 Cannot load driver class: com.mysql.cj.jdbc.Driver 异常。解决方法如下:

  • (1) 在宿主机中下载 MySQL 的驱动包,并将其挂载到 Dubbo Admin 容器内的 /opt-libs 目录下
  • (2) 如果还是加载不到 MySQL 的驱动类,只能更改 Dubbo Admin 项目源码中的 dubbo-admin-server 模块的 Maven 配置文件,加入 MySQL 驱动的 Maven 依赖,然后通过 Dubbo Admin 项目的源码手动构建 Docker 镜像(构建步骤可以参考这里)。

无法查看服务的元数据信息

在 Dubbo Admin 控制台中,无法查看服务提供者的元数据信息,如下图所示:

  • (1) 首先确保 Dubbo Admin 配置了以下几个属性:
1
2
3
admin.registry.address=zookeeper://zookeeper:2181
admin.config-center=zookeeper://zookeeper:2181
admin.metadata-report.address=zookeeper://zookeeper:2181
  • (2) 在服务提供者的代码中,添加元数据中心的配置(<dubbo:metadata-report/>),完整的配置示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- 配置服务应用名 -->
<dubbo:application name="dubbo-provider-application">
<dubbo:parameter key="qos.enable" value="true"/>
<dubbo:parameter key="qos.port" value="22222"/>
<dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
</dubbo:application>

<!-- 注册中心配置 -->
<dubbo:registry address="zookeeper://192.168.2.235:2181" timeout="5000"/>

<!-- 元数据中心配置 -->
<dubbo:metadata-report address="zookeeper://192.168.2.235:2181"/>

<!-- 配置服务协议 -->
<dubbo:protocol name="dubbo" port="20880"/>

<!-- 开启 Dubbo 的注解扫描 -->
<dubbo:annotation package="com.clay.dubbo.producer"/>

参考资料