use of com.networknt.registry.URL in project light-4j by networknt.
the class CommandFailbackRegistry method doUnsubscribe.
@Override
protected void doUnsubscribe(URL url, NotifyListener listener) {
if (logger.isInfoEnabled())
logger.info("CommandFailbackRegistry unsubscribe. url: " + url.toSimpleString());
URL urlCopy = url.createCopy();
CommandServiceManager manager = commandManagerMap.get(urlCopy);
manager.removeNotifyListener(listener);
unsubscribeService(urlCopy, manager);
// unsubscribeCommand(urlCopy, manager);
}
use of com.networknt.registry.URL in project light-4j by networknt.
the class CommandFailbackRegistry method doSubscribe.
@Override
protected void doSubscribe(URL url, final NotifyListener listener) {
if (logger.isInfoEnabled())
logger.info("CommandFailbackRegistry subscribe. url: " + url.toSimpleString());
URL urlCopy = url.createCopy();
CommandServiceManager manager = getCommandServiceManager(urlCopy);
manager.addNotifyListener(listener);
subscribeService(urlCopy, manager);
// subscribeCommand(urlCopy, manager);
List<URL> urls = doDiscover(urlCopy);
if (urls != null && urls.size() > 0) {
this.notify(urlCopy, listener, urls);
}
}
use of com.networknt.registry.URL in project light-4j by networknt.
the class CommandServiceManager method mergeResult.
private List<URL> mergeResult(URL url, Map<String, Integer> weights) {
List<URL> finalResult = new ArrayList<URL>();
if (weights.size() > 1) {
// construct a rule url with all groups and added as first
URL ruleUrl = new URLImpl("rule", url.getHost(), url.getPort(), url.getPath());
StringBuilder weightsBuilder = new StringBuilder(64);
for (Map.Entry<String, Integer> entry : weights.entrySet()) {
weightsBuilder.append(entry.getKey()).append(':').append(entry.getValue()).append(',');
}
ruleUrl.addParameter(URLParamType.weights.getName(), weightsBuilder.deleteCharAt(weightsBuilder.length() - 1).toString());
finalResult.add(ruleUrl);
}
for (String key : weights.keySet()) {
if (groupServiceCache.containsKey(key)) {
finalResult.addAll(groupServiceCache.get(key));
} else {
URL urlTemp = url.createCopy();
urlTemp.addParameter(URLParamType.group.getName(), key);
finalResult.addAll(discoverOneGroup(urlTemp));
registry.subscribeService(urlTemp, this);
}
}
return finalResult;
}
use of com.networknt.registry.URL in project light-4j by networknt.
the class CommandServiceManager method notifyService.
@Override
public void notifyService(URL serviceUrl, URL registryUrl, List<URL> urls) {
if (registry == null) {
throw new FrameworkException(new Status(REGISTRY_IS_NULL));
}
URL urlCopy = serviceUrl.createCopy();
String groupName = urlCopy.getParameter(URLParamType.group.getName(), URLParamType.group.getValue());
groupServiceCache.put(groupName, urls);
List<URL> finalResult = new ArrayList<URL>();
if (logger.isInfoEnabled())
logger.info("command cache is null. service:" + serviceUrl.toSimpleString());
// if no command cache, return group
finalResult.addAll(discoverOneGroup(refUrl));
for (NotifyListener notifyListener : notifySet) {
notifyListener.notify(registry.getUrl(), finalResult);
}
}
use of com.networknt.registry.URL in project light-4j by networknt.
the class ConsulRegistry method lookupServiceUpdate.
private ConcurrentHashMap<String, List<URL>> lookupServiceUpdate(String serviceName, String tag) {
Long lastConsulIndexId = lookupServices.get(serviceName) == null ? 0L : lookupServices.get(serviceName);
if (logger.isDebugEnabled())
logger.debug("serviceName = " + serviceName + " tag = " + tag + " lastConsulIndexId = " + lastConsulIndexId);
ConsulResponse<List<ConsulService>> response = lookupConsulService(serviceName, tag, lastConsulIndexId);
if (logger.isDebugEnabled()) {
try {
logger.debug("response = " + Config.getInstance().getMapper().writeValueAsString(response));
} catch (Exception e) {
}
}
if (response != null) {
List<ConsulService> services = response.getValue();
if (logger.isDebugEnabled())
try {
logger.debug("services = " + Config.getInstance().getMapper().writeValueAsString(services));
} catch (Exception e) {
}
if (services != null && !services.isEmpty() && response.getConsulIndex() > lastConsulIndexId) {
ConcurrentHashMap<String, List<URL>> serviceUrls = new ConcurrentHashMap<String, List<URL>>();
for (ConsulService service : services) {
try {
URL url = ConsulUtils.buildUrl(service);
List<URL> urlList = serviceUrls.get(serviceName);
if (urlList == null) {
urlList = new ArrayList<>();
serviceUrls.put(serviceName, urlList);
}
if (logger.isDebugEnabled())
logger.debug("lookupServiceUpdate url = " + url);
urlList.add(url);
} catch (Exception e) {
logger.error("convert consul service to url fail! service:" + service, e);
}
}
lookupServices.put(serviceName, response.getConsulIndex());
return serviceUrls;
} else {
logger.info(serviceName + " no need update, lastIndex:" + lastConsulIndexId);
}
}
return null;
}
Aggregations