Search in sources :

Example 1 with URL

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

Example 2 with URL

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

Example 3 with URL

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));
}
Also used : ServiceListener(com.networknt.registry.support.command.ServiceListener) List(java.util.List) URL(com.networknt.registry.URL)

Example 4 with URL

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

Example 5 with URL

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;
}
Also used : ArrayList(java.util.ArrayList) 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