Search in sources :

Example 1 with ServiceStateListener

use of com.bonree.brfs.common.service.ServiceStateListener in project BRFS by zhangnianli.

the class TestService method main.

public static void main(String[] args) throws Exception {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.101.86:2181", 3000, 3000, retryPolicy);
    client.start();
    client.blockUntilConnected();
    ServiceManager sm = new DefaultServiceManager(client.usingNamespace("test"));
    sm.start();
    Service s = new Service();
    s.setServiceGroup("group");
    s.setServiceId("ser_" + ID);
    s.setHost("local");
    sm.registerService(s);
    Service tmp = sm.getServiceById(s.getServiceGroup(), s.getServiceId());
    System.out.println(tmp);
    sm.addServiceStateListener("group", new ServiceStateListener() {

        @Override
        public void serviceRemoved(Service service) {
            System.out.println("remove--" + service.getServiceId());
        }

        @Override
        public void serviceAdded(Service service) {
            System.out.println("add--" + service.getServiceId());
        }
    });
    System.out.println(sm.getServiceById(s.getServiceGroup(), s.getServiceId()));
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) DefaultServiceManager(com.bonree.brfs.common.service.impl.DefaultServiceManager) ServiceStateListener(com.bonree.brfs.common.service.ServiceStateListener) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) DefaultServiceManager(com.bonree.brfs.common.service.impl.DefaultServiceManager) ServiceManager(com.bonree.brfs.common.service.ServiceManager) Service(com.bonree.brfs.common.service.Service) RetryPolicy(org.apache.curator.RetryPolicy)

Example 2 with ServiceStateListener

use of com.bonree.brfs.common.service.ServiceStateListener in project BRFS by zhangnianli.

the class ServiceSelectorManager method useDiskSelector.

/**
 * 概述:选择相应的selector缓存
 * @param snIndex
 * @return
 * @throws Exception
 * @user <a href=mailto:weizheng@bonree.com>魏征</a>
 */
public ReaderServiceSelector useDiskSelector(int snIndex) throws Exception {
    ReaderServiceSelector readServerSelector = diskServiceSelectorCachaMap.get(snIndex);
    if (readServerSelector != null) {
        return readServerSelector;
    }
    DiskServiceMetaCache diskServiceMetaCache = new DiskServiceMetaCache(zkClient, zkServerIDPath, snIndex, diskServiceGroup);
    serviceManager.addServiceStateListener(diskServiceGroup, new ServiceStateListener() {

        @Override
        public void serviceRemoved(Service service) {
            diskServiceMetaCache.removeService(service);
        }

        @Override
        public void serviceAdded(Service service) {
            diskServiceMetaCache.addService(service);
        }
    });
    diskServiceMetaCache.loadMetaCachae(serviceManager);
    RouteRoleCache routeCache = new RouteRoleCache(zkClient, snIndex, baseRoutePath);
    RouteParser routeParser = new RouteParser(routeCache);
    // 兼容余鹏的client读取
    readServerSelector = new ReaderServiceSelector(diskServiceMetaCache, routeParser);
    RouteCacheListener cacheListener = new RouteCacheListener(routeCache);
    treeCache.getListenable().addListener(cacheListener);
    diskServiceSelectorCachaMap.put(snIndex, readServerSelector);
    return readServerSelector;
}
Also used : ReaderServiceSelector(com.bonree.brfs.client.route.impl.ReaderServiceSelector) ServiceStateListener(com.bonree.brfs.common.service.ServiceStateListener) DiskServiceMetaCache(com.bonree.brfs.client.meta.impl.DiskServiceMetaCache) Service(com.bonree.brfs.common.service.Service) RouteCacheListener(com.bonree.brfs.client.route.listener.RouteCacheListener)

Aggregations

Service (com.bonree.brfs.common.service.Service)2 ServiceStateListener (com.bonree.brfs.common.service.ServiceStateListener)2 DiskServiceMetaCache (com.bonree.brfs.client.meta.impl.DiskServiceMetaCache)1 ReaderServiceSelector (com.bonree.brfs.client.route.impl.ReaderServiceSelector)1 RouteCacheListener (com.bonree.brfs.client.route.listener.RouteCacheListener)1 ServiceManager (com.bonree.brfs.common.service.ServiceManager)1 DefaultServiceManager (com.bonree.brfs.common.service.impl.DefaultServiceManager)1 RetryPolicy (org.apache.curator.RetryPolicy)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)1