Search in sources :

Example 21 with URL

use of com.networknt.registry.URL in project light-4j by networknt.

the class ConsulRegistry method updateServiceCache.

/**
 * update service cache of the serviceName.
 * update local cache when service list changed,
 * if need notify, notify service
 *
 * @param serviceName
 * @param serviceUrls
 * @param needNotify
 */
private void updateServiceCache(String serviceName, ConcurrentHashMap<String, List<URL>> serviceUrls, boolean needNotify) {
    if (serviceUrls != null && !serviceUrls.isEmpty()) {
        List<URL> urls = serviceCache.get(serviceName);
        if (urls == null) {
            if (logger.isDebugEnabled()) {
                try {
                    logger.debug("serviceUrls = " + Config.getInstance().getMapper().writeValueAsString(serviceUrls));
                } catch (Exception e) {
                }
            }
            serviceCache.put(serviceName, serviceUrls.get(serviceName));
        }
        for (Map.Entry<String, List<URL>> entry : serviceUrls.entrySet()) {
            boolean change = true;
            if (urls != null) {
                List<URL> newUrls = entry.getValue();
                if (newUrls == null || newUrls.isEmpty() || ConsulUtils.isSame(newUrls, urls)) {
                    change = false;
                } else {
                    serviceCache.put(serviceName, newUrls);
                }
            }
            if (change && needNotify) {
                notifyExecutor.execute(new NotifyService(entry.getKey(), entry.getValue()));
                logger.info("light service notify-service: " + entry.getKey());
                StringBuilder sb = new StringBuilder();
                for (URL url : entry.getValue()) {
                    sb.append(url.getUri()).append(";");
                }
                logger.info("consul notify urls:" + sb.toString());
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) URL(com.networknt.registry.URL)

Example 22 with URL

use of com.networknt.registry.URL in project light-4j by networknt.

the class LocalFirstLoadBalanceTest method testSelectWithEmptyList.

@Test
public void testSelectWithEmptyList() throws Exception {
    List<URL> urls = new ArrayList<>();
    URL url = loadBalance.select(urls, null);
    Assert.assertNull(url);
}
Also used : ArrayList(java.util.ArrayList) URL(com.networknt.registry.URL) Test(org.junit.Test)

Example 23 with URL

use of com.networknt.registry.URL in project light-4j by networknt.

the class RoundRobinLoadBalanceTest method testSelect.

@Test
public void testSelect() throws Exception {
    List<URL> urls = new ArrayList<>();
    urls.add(new URLImpl("http", "127.0.0.1", 8081, "v1", new HashMap<String, String>()));
    urls.add(new URLImpl("http", "127.0.0.1", 8082, "v1", new HashMap<String, String>()));
    urls.add(new URLImpl("http", "127.0.0.1", 8083, "v1", new HashMap<String, String>()));
    urls.add(new URLImpl("http", "127.0.0.1", 8084, "v1", new HashMap<String, String>()));
    URL url = loadBalance.select(urls, null);
    Assert.assertEquals(url, URLImpl.valueOf("http://127.0.0.1:8082/v1"));
    url = loadBalance.select(urls, null);
    Assert.assertEquals(url, URLImpl.valueOf("http://127.0.0.1:8083/v1"));
    url = loadBalance.select(urls, null);
    Assert.assertEquals(url, URLImpl.valueOf("http://127.0.0.1:8084/v1"));
    url = loadBalance.select(urls, null);
    Assert.assertEquals(url, URLImpl.valueOf("http://127.0.0.1:8081/v1"));
    url = loadBalance.select(urls, null);
    Assert.assertEquals(url, URLImpl.valueOf("http://127.0.0.1:8082/v1"));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URL(com.networknt.registry.URL) URLImpl(com.networknt.registry.URLImpl) Test(org.junit.Test)

Example 24 with URL

use of com.networknt.registry.URL in project light-4j by networknt.

the class RoundRobinLoadBalanceTest method testSelectWithEmptyList.

@Test
public void testSelectWithEmptyList() throws Exception {
    List<URL> urls = new ArrayList<>();
    URL url = loadBalance.select(urls, null);
    Assert.assertNull(url);
}
Also used : ArrayList(java.util.ArrayList) URL(com.networknt.registry.URL) Test(org.junit.Test)

Example 25 with URL

use of com.networknt.registry.URL in project light-4j by networknt.

the class ZooKeeperRegistry method nodeChildsToUrls.

private List<URL> nodeChildsToUrls(String parentPath, List<String> currentChilds) {
    List<URL> urls = new ArrayList<URL>();
    if (currentChilds != null) {
        for (String node : currentChilds) {
            String nodePath = parentPath + Constants.PATH_SEPARATOR + node;
            String data = client.readData(nodePath, true);
            try {
                URL url = URLImpl.valueOf(data);
                urls.add(url);
            } catch (Exception e) {
                if (logger.isInfoEnabled())
                    logger.warn(String.format("Found malformed urls from ZooKeeperRegistry, path=%s", nodePath), e);
            }
        }
    }
    return urls;
}
Also used : ArrayList(java.util.ArrayList) URL(com.networknt.registry.URL) FrameworkException(com.networknt.exception.FrameworkException)

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