use of com.networknt.registry.URL in project light-4j by networknt.
the class ZooKeeperRegistry method doUnavailable.
@Override
protected void doUnavailable(URL url) {
try {
serverLock.lock();
if (url == null) {
availableServices.removeAll(getRegisteredServiceUrls());
for (URL u : getRegisteredServiceUrls()) {
removeNode(u, ZkNodeType.AVAILABLE_SERVER);
removeNode(u, ZkNodeType.UNAVAILABLE_SERVER);
createNode(u, ZkNodeType.UNAVAILABLE_SERVER);
}
} else {
availableServices.remove(url);
removeNode(url, ZkNodeType.AVAILABLE_SERVER);
removeNode(url, ZkNodeType.UNAVAILABLE_SERVER);
createNode(url, ZkNodeType.UNAVAILABLE_SERVER);
}
} finally {
serverLock.unlock();
}
}
use of com.networknt.registry.URL in project light-4j by networknt.
the class ZooKeeperRegistry method doAvailable.
@Override
protected void doAvailable(URL url) {
try {
serverLock.lock();
if (url == null) {
availableServices.addAll(getRegisteredServiceUrls());
for (URL u : getRegisteredServiceUrls()) {
removeNode(u, ZkNodeType.AVAILABLE_SERVER);
removeNode(u, ZkNodeType.UNAVAILABLE_SERVER);
createNode(u, ZkNodeType.AVAILABLE_SERVER);
}
} else {
availableServices.add(url);
removeNode(url, ZkNodeType.AVAILABLE_SERVER);
removeNode(url, ZkNodeType.UNAVAILABLE_SERVER);
createNode(url, ZkNodeType.AVAILABLE_SERVER);
}
} finally {
serverLock.unlock();
}
}
use of com.networknt.registry.URL in project light-4j by networknt.
the class ZooKeeperRegistryTest method subAndUnsubService.
@Test
public void subAndUnsubService() throws Exception {
ServiceListener serviceListener = new ServiceListener() {
@Override
public void notifyService(URL refUrl, URL registryUrl, List<URL> urls) {
if (!urls.isEmpty()) {
Assert.assertTrue(urls.contains(serviceUrl));
}
}
};
registry.subscribeService(clientUrl, serviceListener);
Assert.assertTrue(containsServiceListener(clientUrl, serviceListener));
registry.doRegister(serviceUrl);
registry.doAvailable(serviceUrl);
Thread.sleep(2000);
registry.unsubscribeService(clientUrl, serviceListener);
Assert.assertFalse(containsServiceListener(clientUrl, serviceListener));
}
use of com.networknt.registry.URL in project light-4j by networknt.
the class LightCluster method serviceToUrl.
/**
* Implement serviceToUrl with client side service discovery.
*
* @param protocol String
* @param serviceName String
* @param requestKey String
* @return String
*/
@Override
public String serviceToUrl(String protocol, String serviceName, String tag, String requestKey) {
if (logger.isDebugEnabled())
logger.debug("protocol = " + protocol + " serviceName = " + serviceName);
// lookup in serviceMap first, if not there, then subscribe and discover.
List<URL> urls = serviceMap.get(serviceName);
if (logger.isDebugEnabled())
logger.debug("cached serviceName " + serviceName + " urls = " + urls);
if (urls == null) {
URL subscribeUrl = URLImpl.valueOf("light://localhost/" + serviceName);
if (tag != null) {
subscribeUrl.addParameter(Constants.TAG_ENVIRONMENT, tag);
}
if (logger.isDebugEnabled())
logger.debug("subscribeUrl = " + subscribeUrl);
// you only need to subscribe once.
if (!subscribedSet.contains(subscribeUrl)) {
registry.subscribe(subscribeUrl, new ClusterNotifyListener());
subscribedSet.add(subscribeUrl);
}
urls = registry.discover(subscribeUrl);
if (logger.isDebugEnabled())
logger.debug("discovered urls = " + urls);
}
URL url = loadBalance.select(urls, requestKey);
if (logger.isDebugEnabled())
logger.debug("final url after load balance = " + url);
// construct a url in string
return protocol + "://" + url.getHost() + ":" + url.getPort();
}
use of com.networknt.registry.URL in project light-4j by networknt.
the class LocalFirstLoadBalance method searchLocalUrls.
private List<URL> searchLocalUrls(List<URL> urls, String ip) {
List<URL> localUrls = new ArrayList<URL>();
long local = ipToLong(ip);
for (URL url : urls) {
long tmp = ipToLong(url.getHost());
if (local != 0 && local == tmp) {
localUrls.add(url);
}
}
return localUrls;
}
Aggregations