Apache Dubbo 常用 CLI 命令与工具详解
汇总 Apache Dubbo 运维命令与工具:Maven 依赖树、编译打包与运行,QoS 控制台(22222)telnet 接入、ls/help/ps/online/offline/invoke 命令回显,并附 Java API 速查。
Apache Dubbo 常用 CLI 命令与工具详解
Dubbo 是纯 Java 库,没有独立的命令行工具,但自带一个功能完备的运维控制台 QoS(端口默认 22222),可通过 telnet 或 TCP 直连进行服务查询、上下线与在线调用。本文以 dubbo 3.2.20 为准,先给出围绕 Dubbo 的 Maven 命令,再详解 QoS 运维命令与核心 Java API,所有回显均为真实执行结果。
一、Maven 常用命令
1.1 查看依赖树
cd /root/dubbo-demo && mvn -B dependency:tree
[INFO] \- org.apache.dubbo:dubbo:jar:3.2.20:compile
[INFO] +- io.netty:netty-all:jar:4.1.113.Final:compile
[INFO] +- org.javassist:javassist:jar:3.30.2-GA:compile
[INFO] +- com.alibaba:hessian-lite:jar:3.2.13:compile
[INFO] \- com.alibaba.fastjson2:fastjson2:jar:2.0.52:compile
1.2 编译、打包与测试
mvn -q -B compile # 仅编译
mvn -B package # 编译并打包为 jar
mvn -B test # 执行单元测试
1.3 生成类路径(便于脱离 Maven 直接运行)
mvn -q -B dependency:build-classpath -Dmdep.outputFile=cp.txt
java -Xms256m -Xmx512m -cp "target/classes:$(cat cp.txt)" com.example.dubbo.ConsumerApp
1.4 通过 exec 插件运行入口类
mvn -B exec:java -Dexec.mainClass=com.example.dubbo.ProviderApp
mvn -B exec:java -Dexec.mainClass=com.example.dubbo.ConsumerApp
1.5 常用参数说明
-q:安静模式,仅输出错误;-B:批处理模式,适合脚本;-o:离线模式,仅使用本地仓库;-Dexec.mainClass=...:exec 插件指定主类;MAVEN_OPTS="-Xms256m -Xmx512m":约束构建/运行进程堆大小。
二、QoS 运维控制台(端口 22222)
2.1 接入方式
QoS 底层是 TCP 服务,支持 telnet 或 bash 的 /dev/tcp 直连。telnet 接入实测:
(printf "version\nquit\n"; sleep 1) | telnet 127.0.0.1 22222
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
dubbo version "3.2.20"
dubbo>BYE!
Connection closed by foreign host.
/dev/tcp 直连(无需安装 telnet):
exec 3<>/dev/tcp/127.0.0.1/22222; printf "ls\nquit\n" >&3; timeout 4 cat <&3
2.2 退出方式
会话退出命令为 quit(回显 BYE!);exit 会提示 no such command。
三、QoS 常用命令详解
3.1 help:查看全部命令
exec 3<>/dev/tcp/127.0.0.1/22222; printf "help\nquit\n" >&3; timeout 5 cat <&3
| cd | Change default service. |
| count | Count the service. |
| getConfig | Get current running config. |
| getAddress | Get service available address |
| gracefulShutdown | Gracefully shutdown servers |
| help | help command |
| invoke | Invoke the service method. |
| live | Judge if service is alive? |
| ls | ls service |
| metrics | reuse qos report |
| offline | offline dubbo |
| online | online app addresses |
| ps | Print server ports and connections. |
| quit | quit telnet console |
| ready | Judge if service is ready to work? |
| shutdown | Shutdown Dubbo Application. |
| startup | Judge if service has started? |
| version | version command(show dubbo version) |
3.2 ls:列出已暴露/已引用服务
dubbo>ls
As Provider side:
+-----------------------------------------------------------------------------------+---+
| Provider Service Name |PUB|
+-----------------------------------------------------------------------------------+---+
| com.example.dubbo.GreetingService | |
+-----------------------------------------------------------------------------------+---+
|DubboInternal - dubbo-demo-provider/org.apache.dubbo.metadata.MetadataService:1.0.0| |
+-----------------------------------------------------------------------------------+---+
As Consumer side:
+---------------------+---+
|Consumer Service Name|NUM|
+---------------------+---+
PUB 列标记服务是否已下线(空表示在线),ls -l 可查看更详细的方法与地址信息。
3.3 ps:查看服务端口与连接
dubbo>ps
20880
回显列出了 provider 暴露的协议端口。
3.4 version / live / ready / startup:状态判断
dubbo>version
dubbo version "3.2.20"
dubbo>live
true
dubbo>ready
true
dubbo>startup
true
3.5 online / offline:服务上下线
exec 3<>/dev/tcp/127.0.0.1/22222; \
printf "offline com.example.dubbo.GreetingService\nonline com.example.dubbo.GreetingService\nquit\n" >&3; timeout 5 cat <&3
dubbo>OK
dubbo>OK
dubbo>BYE!
offline <服务名> 在不重启进程的前提下摘除服务,online <服务名> 重新上线,均回显 OK。
3.6 cd / pwd / count:服务上下文与计数
exec 3<>/dev/tcp/127.0.0.1/22222; \
printf "cd com.example.dubbo.GreetingService\npwd\ncount\nquit\n" >&3; timeout 6 cat <&3
Used the com.example.dubbo.GreetingService as default.
You can cancel default service by command: cd /
dubbo>com.example.dubbo.GreetingService
dubbo>Please input service name, eg:
count XxxService
count XxxService xxxMethod
count XxxService xxxMethod 10
or "cd XxxService" firstly.
cd 设定默认服务(cd / 取消),pwd 打印当前默认服务,count 统计服务/方法的调用次数。
3.7 invoke:在线调用服务方法
exec 3<>/dev/tcp/127.0.0.1/22222; \
printf "cd com.example.dubbo.GreetingService\ninvoke sayHello(\"dubbo\")\ninvoke add(3,4)\nquit\n" >&3; timeout 6 cat <&3
dubbo>result: "Hello, dubbo from Dubbo provider!"
elapsed: 0 ms.
dubbo>result: 7
elapsed: 0 ms.
invoke参数必须为 JSON 语法:字符串写"dubbo",整数直接写4;否则会报Invalid json argument。
3.8 getConfig:查看运行期配置
exec 3<>/dev/tcp/127.0.0.1/22222; printf "getConfig\nquit\n" >&3; timeout 6 cat <&3
ApplicationConfig: dubbo-demo-provider
<dubbo:application enableFileCache="true" executorManagementMode="isolation" parameters="{}" name="dubbo-demo-provider" protocol="dubbo" />
ProtocolConfig: dubbo
<dubbo:protocol preferSerialization="fastjson2,hessian2" name="dubbo" />
<dubbo:provider deprecated="false" parameters="{}" dynamic="true" />
<dubbo:consumer sticky="false" parameters="{}" />
3.9 调用链观测:trace 说明
Dubbo 3.2.20 的 QoS 已移除 trace 命令,实测返回:
dubbo>trace com.example.dubbo.GreetingService sayHello :no such command
调用链与路由观测改由 getRecentRouterSnapshot、enableRouterSnapshot 及 enableSimpleProfiler / enableDetailProfiler 等命令承担。
四、QoS 命令速查表
| 命令 | 用途 | 示例 |
|---|---|---|
help | 列出全部命令 | help |
ls | 列出 provider/consumer 服务 | ls / ls -l |
ps | 查看暴露端口 | ps |
version | 查看 Dubbo 版本 | version |
live / ready / startup | 健康与就绪判断 | ready |
online / offline | 服务上/下线 | offline com.example.XxxService |
invoke | 在线调用方法 | invoke sayHello("dubbo") |
cd / pwd | 设定/查看默认服务 | cd com.example.XxxService |
count | 调用计数 | count com.example.XxxService sayHello |
getConfig | 打印运行配置 | getConfig |
metrics_default | 打印指标 | metrics_default |
switchLogLevel | 动态调整日志级别 | switchLogLevel DEBUG |
gracefulShutdown | 优雅停机 | gracefulShutdown |
quit | 退出会话 | quit |
五、核心 Java API 速查
5.1 ServiceConfig(服务端暴露)
| 方法 | 说明 |
|---|---|
new ServiceConfig<T>() | 创建服务配置 |
setApplication(new ApplicationConfig("name")) | 设置应用名 |
setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)) | 直连模式,不用注册中心 |
setProtocol(new ProtocolConfig("dubbo", 20880)) | 设置协议与端口 |
setInterface(Class) / setRef(Object) | 设置接口与实现对象 |
export() | 暴露服务 |
getExportedUrls() | 获取已暴露地址 |
setTimeout(int) / setRetries(int) | 设置超时/重试 |
5.2 ReferenceConfig(客户端引用)
| 方法 | 说明 |
|---|---|
new ReferenceConfig<T>() | 创建引用配置 |
setInterface(Class) | 设置接口 |
setUrl("dubbo://127.0.0.1:20880") | 直连地址 |
get() | 获取代理对象并发起调用 |
destroy() | 销毁引用,释放连接 |
setCheck(false) | 启动时不校验服务可用 |
5.3 注解方式(Spring Boot)
| 注解 | 用途 |
|---|---|
@EnableDubbo | 开启 Dubbo 注解扫描 |
@DubboService | 标注 service 实现,自动暴露 |
@DubboReference | 注入远程服务代理 |
@DubboService(timeout=3000, retries=2) | 服务级参数 |
示例:
@DubboService
public class GreetingServiceImpl implements GreetingService { /* ... */ }
@DubboReference(url = "dubbo://127.0.0.1:20880")
private GreetingService greetingService;