Search in sources :

Example 16 with URL

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);
}
Also used : URL(com.networknt.registry.URL)

Example 17 with URL

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);
    }
}
Also used : URL(com.networknt.registry.URL)

Example 18 with URL

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;
}
Also used : ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) URL(com.networknt.registry.URL) URLImpl(com.networknt.registry.URLImpl)

Example 19 with URL

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);
    }
}
Also used : Status(com.networknt.status.Status) FrameworkException(com.networknt.exception.FrameworkException) ArrayList(java.util.ArrayList) URL(com.networknt.registry.URL) NotifyListener(com.networknt.registry.NotifyListener)

Example 20 with URL

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;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(com.networknt.registry.URL)

Aggregations

URL (com.networknt.registry.URL)25 ArrayList (java.util.ArrayList)11 URLImpl (com.networknt.registry.URLImpl)7 HashMap (java.util.HashMap)7 Test (org.junit.Test)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 FrameworkException (com.networknt.exception.FrameworkException)3 List (java.util.List)3 Map (java.util.Map)3 NotifyListener (com.networknt.registry.NotifyListener)2 Registry (com.networknt.registry.Registry)1 ServiceListener (com.networknt.registry.support.command.ServiceListener)1 Status (com.networknt.status.Status)1 ConcurrentHashSet (com.networknt.utility.ConcurrentHashSet)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1