com.alibaba.dubbo.registry.RegistryService Java Examples

The following examples show how to use com.alibaba.dubbo.registry.RegistryService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DubboRegistry.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public DubboRegistry(Invoker<RegistryService> registryInvoker, RegistryService registryService) {
    super(registryInvoker.getUrl());
    this.registryInvoker = registryInvoker;
    this.registryService = registryService;
    // 启动重连定时器
    int reconnectPeriod = registryInvoker.getUrl().getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, RECONNECT_PERIOD_DEFAULT);
    reconnectFuture = scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
        public void run() {
            // 检测并连接注册中心
            try {
                connect();
            } catch (Throwable t) { // 防御性容错
                logger.error("Unexpected error occur at reconnect, cause: " + t.getMessage(), t);
            }
        }
    }, reconnectPeriod, reconnectPeriod, TimeUnit.MILLISECONDS);
}
 
Example #2
Source File: DubboRegistry.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
public DubboRegistry(Invoker<RegistryService> registryInvoker, RegistryService registryService) {
    super(registryInvoker.getUrl());
    this.registryInvoker = registryInvoker;
    this.registryService = registryService;
    // Start reconnection timer
    this.reconnectPeriod = registryInvoker.getUrl().getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, RECONNECT_PERIOD_DEFAULT);
    reconnectFuture = reconnectTimer.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            // Check and connect to the registry
            try {
                connect();
            } catch (Throwable t) { // Defensive fault tolerance
                logger.error("Unexpected error occur at reconnect, cause: " + t.getMessage(), t);
            }
        }
    }, reconnectPeriod, reconnectPeriod, TimeUnit.MILLISECONDS);
}
 
Example #3
Source File: DubboRegistryFactory.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
public Registry createRegistry(URL url) {
    url = getRegistryURL(url);
    List<URL> urls = new ArrayList<URL>();
    urls.add(url.removeParameter(Constants.BACKUP_KEY));
    String backup = url.getParameter(Constants.BACKUP_KEY);
    if (backup != null && backup.length() > 0) {
        String[] addresses = Constants.COMMA_SPLIT_PATTERN.split(backup);
        for (String address : addresses) {
            urls.add(url.setAddress(address));
        }
    }
    RegistryDirectory<RegistryService> directory = new RegistryDirectory<RegistryService>(RegistryService.class, url.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()).addParameterAndEncoded(Constants.REFER_KEY, url.toParameterString()));
    Invoker<RegistryService> registryInvoker = cluster.join(directory);
    RegistryService registryService = proxyFactory.getProxy(registryInvoker);
    DubboRegistry registry = new DubboRegistry(registryInvoker, registryService);
    directory.setRegistry(registry);
    directory.setProtocol(protocol);
    directory.notify(urls);
    directory.subscribe(new URL(Constants.CONSUMER_PROTOCOL, NetUtils.getLocalHost(), 0, RegistryService.class.getName(), url.getParameters()));
    return registry;
}
 
Example #4
Source File: ConfigTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelayOnInitialized() throws Exception {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-on-initialized.xml");
    //ctx.start();
    try {
        List<URL> urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNotNull(urls);
        assertEquals(1, urls.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20888/com.alibaba.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter.unexport();
    }
}
 
Example #5
Source File: ConfigTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelayFixedTime() throws Exception {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-fixed-time.xml");
    ctx.start();
    try {
        List<URL> urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNull(urls);
        int i = 0;
        while ((i ++) < 60 && urls == null) {
            urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
            Thread.sleep(10);
        }
        assertNotNull(urls);
        assertEquals(1, urls.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20883/com.alibaba.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter.unexport();
    }
}
 
Example #6
Source File: ConfigTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiRegistry() {
    SimpleRegistryService registryService1 = new SimpleRegistryService();
    Exporter<RegistryService> exporter1 = SimpleRegistryExporter.export(4545, registryService1);
    SimpleRegistryService registryService2 = new SimpleRegistryService();
    Exporter<RegistryService> exporter2 = SimpleRegistryExporter.export(4546, registryService2);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-registry.xml");
    ctx.start();
    try {
        List<URL> urls1 = registryService1.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNull(urls1);
        List<URL> urls2 = registryService2.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNotNull(urls2);
        assertEquals(1, urls2.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20880/com.alibaba.dubbo.config.spring.api.DemoService", urls2.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter1.unexport();
        exporter2.unexport();
    }
}
 
Example #7
Source File: ConfigTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotation() {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    try {
        ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-provider.xml");
        providerContext.start();
        try {
            ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-consumer.xml");
            consumerContext.start();
            try {
                AnnotationAction annotationAction = (AnnotationAction) consumerContext.getBean("annotationAction");
                String hello = annotationAction.doSayName("hello");
                assertEquals("annotation:hello", hello);
            } finally {
                consumerContext.stop();
                consumerContext.close();
            }
        } finally {
            providerContext.stop();
            providerContext.close();
        }
    } finally {
        exporter.unexport();
    }
}
 
Example #8
Source File: DubboRegistryFactory.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
private static URL getRegistryURL(URL url) {
    return url.setPath(RegistryService.class.getName())
            .removeParameter(Constants.EXPORT_KEY).removeParameter(Constants.REFER_KEY)
            .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName())
            .addParameter(Constants.CLUSTER_STICKY_KEY, "true")
            .addParameter(Constants.LAZY_CONNECT_KEY, "true")
            .addParameter(Constants.RECONNECT_KEY, "false")
            .addParameterIfAbsent(Constants.TIMEOUT_KEY, "10000")
            .addParameterIfAbsent(Constants.CALLBACK_INSTANCES_LIMIT_KEY, "10000")
            .addParameterIfAbsent(Constants.CONNECT_TIMEOUT_KEY, "10000")
            .addParameter(Constants.METHODS_KEY, StringUtils.join(new HashSet<String>(Arrays.asList(Wrapper.getWrapper(RegistryService.class).getDeclaredMethodNames())), ","))
            //.addParameter(Constants.STUB_KEY, RegistryServiceStub.class.getName())
            //.addParameter(Constants.STUB_EVENT_KEY, Boolean.TRUE.toString()) //for event dispatch
            //.addParameter(Constants.ON_DISCONNECT_KEY, "disconnect")
            .addParameter("subscribe.1.callback", "true")
            .addParameter("unsubscribe.1.callback", "false");
}
 
Example #9
Source File: ConfigTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiRegistry() {
    SimpleRegistryService registryService1 = new SimpleRegistryService();
    Exporter<RegistryService> exporter1 = SimpleRegistryExporter.export(4545, registryService1);
    SimpleRegistryService registryService2 = new SimpleRegistryService();
    Exporter<RegistryService> exporter2 = SimpleRegistryExporter.export(4546, registryService2);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-registry.xml");
    ctx.start();
    try {
        List<URL> urls1 = registryService1.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNull(urls1);
        List<URL> urls2 = registryService2.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNotNull(urls2);
        assertEquals(1, urls2.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20880/com.alibaba.dubbo.config.spring.api.DemoService", urls2.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter1.unexport();
        exporter2.unexport();
    }
}
 
Example #10
Source File: ConfigTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelayFixedTime() throws Exception {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-fixed-time.xml");
    ctx.start();
    try {
        List<URL> urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNull(urls);
        int i = 0;
        while ((i ++) < 60 && urls == null) {
            urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
            Thread.sleep(10);
        }
        assertNotNull(urls);
        assertEquals(1, urls.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20883/com.alibaba.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter.unexport();
    }
}
 
Example #11
Source File: ConfigTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelayOnInitialized() throws Exception {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-on-initialized.xml");
    //ctx.start();
    try {
        List<URL> urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNotNull(urls);
        assertEquals(1, urls.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20883/com.alibaba.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter.unexport();
    }
}
 
Example #12
Source File: ConfigTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotation() {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    try {
        ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-provider.xml");
        providerContext.start();
        try {
            ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-consumer.xml");
            consumerContext.start();
            try {
                AnnotationAction annotationAction = (AnnotationAction) consumerContext.getBean("annotationAction");
                String hello = annotationAction.doSayName("hello");
                assertEquals("annotation:hello", hello);
            } finally {
                consumerContext.stop();
                consumerContext.close();
            }
        } finally {
            providerContext.stop();
            providerContext.close();
        }
    } finally {
        exporter.unexport();
    }
}
 
Example #13
Source File: SimpleRegistryService.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void subscribe(URL url, NotifyListener listener) {
    if (getUrl().getPort() == 0) {
        URL registryUrl = RpcContext.getContext().getUrl();
        if (registryUrl != null && registryUrl.getPort() > 0
        		&& RegistryService.class.getName().equals(registryUrl.getPath())) {
            super.setUrl(registryUrl);
            super.register(registryUrl);
        }
    }
    String client = RpcContext.getContext().getRemoteAddressString();
    ConcurrentMap<URL, Set<NotifyListener>> clientListeners = remoteSubscribed.get(client);
    if (clientListeners == null) {
        remoteSubscribed.putIfAbsent(client, new ConcurrentHashMap<URL, Set<NotifyListener>>());
        clientListeners = remoteSubscribed.get(client);
    }
    Set<NotifyListener> listeners = clientListeners.get(url);
    if (listeners == null) {
        clientListeners.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
        listeners = clientListeners.get(url);
    }
    listeners.add(listener);
    super.subscribe(url, listener);
    subscribed(url, listener);
}
 
Example #14
Source File: DubboRegistryFactory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Registry createRegistry(URL url) {
    url = getRegistryURL(url);
    List<URL> urls = new ArrayList<URL>();
    urls.add(url.removeParameter(Constants.BACKUP_KEY));
    String backup = url.getParameter(Constants.BACKUP_KEY);
    if (backup != null && backup.length() > 0) {
        String[] addresses = Constants.COMMA_SPLIT_PATTERN.split(backup);
        for (String address : addresses) {
            urls.add(url.setAddress(address));
        }
    }
    RegistryDirectory<RegistryService> directory = new RegistryDirectory<RegistryService>(RegistryService.class, url.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()).addParameterAndEncoded(Constants.REFER_KEY, url.toParameterString()));
    Invoker<RegistryService> registryInvoker = cluster.join(directory);
    RegistryService registryService = proxyFactory.getProxy(registryInvoker);
    DubboRegistry registry = new DubboRegistry(registryInvoker, registryService);
    directory.setRegistry(registry);
    directory.setProtocol(protocol);
    directory.notify(urls);
    directory.subscribe(new URL(Constants.CONSUMER_PROTOCOL, NetUtils.getLocalHost(), 0, RegistryService.class.getName(), url.getParameters()));
    return registry;
}
 
Example #15
Source File: DubboRegistryFactory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private static URL getRegistryURL(URL url) {
    return url.setPath(RegistryService.class.getName())
            .removeParameter(Constants.EXPORT_KEY).removeParameter(Constants.REFER_KEY)
            .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName())
            .addParameter(Constants.CLUSTER_STICKY_KEY, "true")
            .addParameter(Constants.LAZY_CONNECT_KEY, "true")
            .addParameter(Constants.RECONNECT_KEY, "false")
            .addParameterIfAbsent(Constants.TIMEOUT_KEY, "10000")
            .addParameterIfAbsent(Constants.CALLBACK_INSTANCES_LIMIT_KEY, "10000")
            .addParameterIfAbsent(Constants.CONNECT_TIMEOUT_KEY, "10000")
            .addParameter(Constants.METHODS_KEY, StringUtils.join(new HashSet<String>(Arrays.asList(Wrapper.getWrapper(RegistryService.class).getDeclaredMethodNames())), ","))
            //.addParameter(Constants.STUB_KEY, RegistryServiceStub.class.getName())
            //.addParameter(Constants.STUB_EVENT_KEY, Boolean.TRUE.toString()) //for event dispatch
            //.addParameter(Constants.ON_DISCONNECT_KEY, "disconnect")
            .addParameter("subscribe.1.callback", "true")
            .addParameter("unsubscribe.1.callback", "false");
}
 
Example #16
Source File: ConfigTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiProtocolRegister() {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4547, registryService);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol-register.xml");
    ctx.start();
    try {
        List<URL> urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNotNull(urls);
        assertEquals(1, urls.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20824/com.alibaba.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter.unexport();
    }
}
 
Example #17
Source File: AbstractRegistryFactory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Registry getRegistry(URL url) {
	url = url.setPath(RegistryService.class.getName())
			.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName())
			.removeParameters(Constants.EXPORT_KEY, Constants.REFER_KEY);
	String key = url.toServiceString();
    // 锁定注册中心获取过程,保证注册中心单一实例
    LOCK.lock();
    try {
        Registry registry = REGISTRIES.get(key);
        if (registry != null) {
            return registry;
        }
        registry = createRegistry(url);
        if (registry == null) {
            throw new IllegalStateException("Can not create registry " + url);
        }
        REGISTRIES.put(key, registry);
        return registry;
    } finally {
        // 释放锁
        LOCK.unlock();
    }
}
 
Example #18
Source File: DubboRegistry.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public DubboRegistry(Invoker<RegistryService> registryInvoker, RegistryService registryService) {
    super(registryInvoker.getUrl());
    this.registryInvoker = registryInvoker;
    this.registryService = registryService;
    // 启动重连定时器
    int reconnectPeriod = registryInvoker.getUrl().getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, RECONNECT_PERIOD_DEFAULT);
    reconnectFuture = scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
        public void run() {
            // 检测并连接注册中心
            try {
                connect();
            } catch (Throwable t) { // 防御性容错
                logger.error("Unexpected error occur at reconnect, cause: " + t.getMessage(), t);
            }
        }
    }, reconnectPeriod, reconnectPeriod, TimeUnit.MILLISECONDS);
}
 
Example #19
Source File: RegistryProtocol.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
       url = url.setProtocol(url.getParameter(Constants.REGISTRY_KEY, Constants.DEFAULT_REGISTRY)).removeParameter(Constants.REGISTRY_KEY);
       Registry registry = registryFactory.getRegistry(url);
       if (RegistryService.class.equals(type)) {
       	return proxyFactory.getInvoker((T) registry, type, url);
       }

       // group="a,b" or group="*"
       Map<String, String> qs = StringUtils.parseQueryString(url.getParameterAndDecoded(Constants.REFER_KEY));
       String group = qs.get(Constants.GROUP_KEY);
       if (group != null && group.length() > 0 ) {
           if ( ( Constants.COMMA_SPLIT_PATTERN.split( group ) ).length > 1
                   || "*".equals( group ) ) {
               return doRefer( getMergeableCluster(), registry, type, url );
           }
       }
       return doRefer(cluster, registry, type, url);
   }
 
Example #20
Source File: AbstractRegistryFactory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Registry getRegistry(URL url) {
	url = url.setPath(RegistryService.class.getName())
			.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName())
			.removeParameters(Constants.EXPORT_KEY, Constants.REFER_KEY);
	String key = url.toServiceString();
    // 锁定注册中心获取过程,保证注册中心单一实例
    LOCK.lock();
    try {
        Registry registry = REGISTRIES.get(key);
        if (registry != null) {
            return registry;
        }
        registry = createRegistry(url);
        if (registry == null) {
            throw new IllegalStateException("Can not create registry " + url);
        }
        REGISTRIES.put(key, registry);
        return registry;
    } finally {
        // 释放锁
        LOCK.unlock();
    }
}
 
Example #21
Source File: DubboRegistryFactory.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
private static URL getRegistryURL(URL url) {
    return url.setPath(RegistryService.class.getName())
            .removeParameter(Constants.EXPORT_KEY).removeParameter(Constants.REFER_KEY)
            .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName())
            .addParameter(Constants.CLUSTER_STICKY_KEY, "true")
            .addParameter(Constants.LAZY_CONNECT_KEY, "true")
            .addParameter(Constants.RECONNECT_KEY, "false")
            .addParameterIfAbsent(Constants.TIMEOUT_KEY, "10000")
            .addParameterIfAbsent(Constants.CALLBACK_INSTANCES_LIMIT_KEY, "10000")
            .addParameterIfAbsent(Constants.CONNECT_TIMEOUT_KEY, "10000")
            .addParameter(Constants.METHODS_KEY, StringUtils.join(new HashSet<String>(Arrays.asList(Wrapper.getWrapper(RegistryService.class).getDeclaredMethodNames())), ","))
            //.addParameter(Constants.STUB_KEY, RegistryServiceStub.class.getName())
            //.addParameter(Constants.STUB_EVENT_KEY, Boolean.TRUE.toString()) //for event dispatch
            //.addParameter(Constants.ON_DISCONNECT_KEY, "disconnect")
            .addParameter("subscribe.1.callback", "true")
            .addParameter("unsubscribe.1.callback", "false");
}
 
Example #22
Source File: DubboRegistryFactory.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public Registry createRegistry(URL url) {
    url = getRegistryURL(url);
    List<URL> urls = new ArrayList<URL>();
    urls.add(url.removeParameter(Constants.BACKUP_KEY));
    String backup = url.getParameter(Constants.BACKUP_KEY);
    if (backup != null && backup.length() > 0) {
        String[] addresses = Constants.COMMA_SPLIT_PATTERN.split(backup);
        for (String address : addresses) {
            urls.add(url.setAddress(address));
        }
    }
    RegistryDirectory<RegistryService> directory = new RegistryDirectory<RegistryService>(RegistryService.class, url.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()).addParameterAndEncoded(Constants.REFER_KEY, url.toParameterString()));
    Invoker<RegistryService> registryInvoker = cluster.join(directory);
    RegistryService registryService = proxyFactory.getProxy(registryInvoker);
    DubboRegistry registry = new DubboRegistry(registryInvoker, registryService);
    directory.setRegistry(registry);
    directory.setProtocol(protocol);
    directory.notify(urls);
    directory.subscribe(new URL(Constants.CONSUMER_PROTOCOL, NetUtils.getLocalHost(), 0, RegistryService.class.getName(), url.getParameters()));
    return registry;
}
 
Example #23
Source File: RegistryProtocol.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
       url = url.setProtocol(url.getParameter(Constants.REGISTRY_KEY, Constants.DEFAULT_REGISTRY)).removeParameter(Constants.REGISTRY_KEY);
       Registry registry = registryFactory.getRegistry(url);
       if (RegistryService.class.equals(type)) {
       	return proxyFactory.getInvoker((T) registry, type, url);
       }

       // group="a,b" or group="*"
       Map<String, String> qs = StringUtils.parseQueryString(url.getParameterAndDecoded(Constants.REFER_KEY));
       String group = qs.get(Constants.GROUP_KEY);
       if (group != null && group.length() > 0 ) {
           if ( ( Constants.COMMA_SPLIT_PATTERN.split( group ) ).length > 1
                   || "*".equals( group ) ) {
               return doRefer( getMergeableCluster(), registry, type, url );
           }
       }
       return doRefer(cluster, registry, type, url);
   }
 
Example #24
Source File: SimpleRegistryService.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public void subscribe(URL url, NotifyListener listener) {
    if (getUrl().getPort() == 0) {
        URL registryUrl = RpcContext.getContext().getUrl();
        if (registryUrl != null && registryUrl.getPort() > 0
        		&& RegistryService.class.getName().equals(registryUrl.getPath())) {
            super.setUrl(registryUrl);
            super.register(registryUrl);
        }
    }
    String client = RpcContext.getContext().getRemoteAddressString();
    ConcurrentMap<URL, Set<NotifyListener>> clientListeners = remoteSubscribed.get(client);
    if (clientListeners == null) {
        remoteSubscribed.putIfAbsent(client, new ConcurrentHashMap<URL, Set<NotifyListener>>());
        clientListeners = remoteSubscribed.get(client);
    }
    Set<NotifyListener> listeners = clientListeners.get(url);
    if (listeners == null) {
        clientListeners.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
        listeners = clientListeners.get(url);
    }
    listeners.add(listener);
    super.subscribe(url, listener);
    subscribed(url, listener);
}
 
Example #25
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotation() {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    try {
        ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-provider.xml");
        providerContext.start();
        try {
            ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-consumer.xml");
            consumerContext.start();
            try {
                AnnotationAction annotationAction = (AnnotationAction) consumerContext.getBean("annotationAction");
                String hello = annotationAction.doSayName("hello");
                assertEquals("annotation:hello", hello);
            } finally {
                consumerContext.stop();
                consumerContext.close();
            }
        } finally {
            providerContext.stop();
            providerContext.close();
        }
    } finally {
        exporter.unexport();
    }
}
 
Example #26
Source File: AbstractRegistryFactory.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public Registry getRegistry(URL url) {
	url = url.setPath(RegistryService.class.getName())
			.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName())
			.removeParameters(Constants.EXPORT_KEY, Constants.REFER_KEY);
	String key = url.toServiceString();
    // 锁定注册中心获取过程,保证注册中心单一实例
    LOCK.lock();
    try {
        Registry registry = REGISTRIES.get(key);
        if (registry != null) {
            return registry;
        }
        registry = createRegistry(url);
        if (registry == null) {
            throw new IllegalStateException("Can not create registry " + url);
        }
        REGISTRIES.put(key, registry);
        return registry;
    } finally {
        // 释放锁
        LOCK.unlock();
    }
}
 
Example #27
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelayOnInitialized() throws Exception {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-on-initialized.xml");
    //ctx.start();
    try {
        List<URL> urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNotNull(urls);
        assertEquals(1, urls.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20883/com.alibaba.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter.unexport();
    }
}
 
Example #28
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiProtocolRegister() {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4547, registryService);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol-register.xml");
    ctx.start();
    try {
        List<URL> urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNotNull(urls);
        assertEquals(1, urls.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20824/com.alibaba.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter.unexport();
    }
}
 
Example #29
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiRegistry() {
    SimpleRegistryService registryService1 = new SimpleRegistryService();
    Exporter<RegistryService> exporter1 = SimpleRegistryExporter.export(4545, registryService1);
    SimpleRegistryService registryService2 = new SimpleRegistryService();
    Exporter<RegistryService> exporter2 = SimpleRegistryExporter.export(4546, registryService2);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-registry.xml");
    ctx.start();
    try {
        List<URL> urls1 = registryService1.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNull(urls1);
        List<URL> urls2 = registryService2.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNotNull(urls2);
        assertEquals(1, urls2.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20880/com.alibaba.dubbo.config.spring.api.DemoService", urls2.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter1.unexport();
        exporter2.unexport();
    }
}
 
Example #30
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelayFixedTime() throws Exception {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-fixed-time.xml");
    ctx.start();
    try {
        List<URL> urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNull(urls);
        int i = 0;
        while ((i ++) < 60 && urls == null) {
            urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
            Thread.sleep(10);
        }
        assertNotNull(urls);
        assertEquals(1, urls.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20883/com.alibaba.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter.unexport();
    }
}