You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/03/17 07:14:07 UTC
[dubbo] branch master updated: [ISSUE-#7331] Check service is start
success (#7370)
This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 12aad84 [ISSUE-#7331] Check service is start success (#7370)
12aad84 is described below
commit 12aad8458029101f097c3bc3763815148ad990e1
Author: jiangxch <61...@users.noreply.github.com>
AuthorDate: Wed Mar 17 15:13:42 2021 +0800
[ISSUE-#7331] Check service is start success (#7370)
Co-authored-by: 蒋小川 <ji...@fxiaoke.com>
---
.../org/apache/dubbo/qos/command/impl/Ready.java | 67 +++++++++++++++++++++-
1 file changed, 65 insertions(+), 2 deletions(-)
diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ready.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ready.java
index 7f7dd1a..2c6b422 100644
--- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ready.java
+++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ready.java
@@ -16,17 +16,80 @@
*/
package org.apache.dubbo.qos.command.impl;
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+import org.apache.dubbo.config.utils.ConfigValidationUtils;
import org.apache.dubbo.qos.command.BaseCommand;
import org.apache.dubbo.qos.command.CommandContext;
import org.apache.dubbo.qos.command.annotation.Cmd;
+import org.apache.dubbo.qos.textui.TTable;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.ProviderModel;
-@Cmd(name = "ready",summary = "Judge if service has started? ")
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+@Cmd(name = "ready",summary = "Judge if application or service has started? ")
public class Ready implements BaseCommand {
@Override
public String execute(CommandContext commandContext, String[] args) {
- return DubboBootstrap.getInstance().isReady() ? "true" : "false";
+ String serviceName = args.length > 0 ? args[0] : null;
+ if (StringUtils.isEmpty(serviceName)) {
+ // judge application has started
+ return DubboBootstrap.getInstance().isReady() ? "true" : "false";
+ } else {
+ // judge service has started
+ Map<String, Boolean> serviceReadyMap = isServiceReady(serviceName);
+ if (serviceReadyMap == null || serviceReadyMap.size() <= 0) {
+ return "can't match service=" + serviceName;
+ }
+ return buildUiText(serviceReadyMap);
+ }
+ }
+
+ private String buildUiText(Map<String, Boolean> serviceReadyMap) {
+ TTable tTable = new TTable(new TTable.ColumnDefine[]{
+ new TTable.ColumnDefine(TTable.Align.MIDDLE),
+ new TTable.ColumnDefine(TTable.Align.MIDDLE)
+ });
+
+ //Header
+ tTable.addRow("Provider Service Name", "STATUS");
+ for (Map.Entry<String, Boolean> entry : serviceReadyMap.entrySet()) {
+ String status = Boolean.TRUE.equals(entry.getValue()) ? "TRUE" : "FALSE";
+ tTable.addRow(entry.getKey(),status);
+ }
+ return tTable.rendering();
+ }
+
+ /**
+ * judge service provider is started
+ * @param serviceName service name,eg: org.apache.dubbo.demo.DemoService
+ * @return Map[serviceKey,isStarted] eg:[org.apache.dubbo.demo.DemoService,true] or [group1/org.apache.dubbo.demo.DemoService,false]
+ */
+ private Map<String,Boolean> isServiceReady(String serviceName) {
+ Map<String,Boolean> res = new HashMap<>();
+ for (ProviderModel providerModel : ApplicationModel.allProviderModels()) {
+ String serviceKey = providerModel.getServiceKey();
+ String interfaceName = providerModel.getServiceConfig().getInterface();
+ if (interfaceName.equals(serviceName)) {
+ List<URL> needRegistryURLs = ConfigValidationUtils.loadRegistries(providerModel.getServiceConfig(), true);
+ List<URL> registeredRegistryURLs = providerModel.getStatedUrl().stream()
+ .filter(x -> Boolean.TRUE.equals(x.isRegistered()))
+ .map(ProviderModel.RegisterStatedURL::getRegistryUrl)
+ .collect(Collectors.toList());
+ if (needRegistryURLs.size() == registeredRegistryURLs.size()) {
+ res.put(serviceKey,true);
+ } else {
+ res.put(serviceKey,false);
+ }
+ }
+ }
+ return res;
}
}