consumer端要想获取指定provider的服务列表

義往昔 19天前 ⋅ 44 阅读
package org.apache.dubbo.demo.consumer;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.integration.RegistryDirectory;
import org.apache.dubbo.registry.support.AbstractRegistryFactory;
import org.apache.dubbo.registry.zookeeper.ZookeeperRegistry;
import org.apache.dubbo.rpc.Invoker;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Application {
    /**
     * In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before launch the application
     */
    public static void main(String[] args) {
        ReferenceConfig<DemoService> reference = new ReferenceConfig<>();
        reference.setApplication(new ApplicationConfig("dubbo-demo-api-consumer"));
        reference.setRegistry(new RegistryConfig("zookeeper://localhost:2181"));
        reference.setInterface(DemoService.class);
        DemoService service = reference.get();
        String message = service.sayHello("dubbo");

        // 查找消费方接口的providers
        Collection<Registry> registries = AbstractRegistryFactory.getRegistries();
        ZookeeperRegistry registry = (ZookeeperRegistry) registries.toArray()[0];
        Map<URL, Set<NotifyListener>> listeners = registry.getSubscribed();
        Iterator<Map.Entry<URL, Set<NotifyListener>>> iterator = listeners.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<URL, Set<NotifyListener>> entry = iterator.next();
            URL url = entry.getKey();
            Set<NotifyListener> notifyListeners = entry.getValue();
            // 判断url中包含的interface、group和version是否要查询的
            {
                // ...
                // 如果是,提取NotifyListener
                if (!notifyListeners.isEmpty()) {
                    NotifyListener notifyListener = (NotifyListener) notifyListeners.toArray()[0];
                    if (notifyListener instanceof RegistryDirectory) {
                        RegistryDirectory directory = (RegistryDirectory) notifyListener;
                        try {
                            Field invokers = directory.getClass().getDeclaredField("invokers");
                            invokers.setAccessible(true);
                            // 获取到了providers
                            List<Invoker> providers = (List<Invoker>) invokers.get(directory);
                            System.out.println(providers);
                        } catch (NoSuchFieldException | IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            // 否则, continue
        }

        System.out.println(message);
    }
}


全部评论: 0

    我有话说: