Skip to content

Commit

Permalink
[ISSUE-#7331] Check service is start success (#7331)
Browse files Browse the repository at this point in the history
  • Loading branch information
蒋小川 committed Mar 14, 2021
1 parent 7cf6884 commit 1abc9ba
Showing 1 changed file with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

0 comments on commit 1abc9ba

Please sign in to comment.