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());
}
}
}
}
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);
}
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"));
}
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);
}
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;
}
Aggregations