美文网首页dubbo
Dubbo Admin 数据获取原理

Dubbo Admin 数据获取原理

作者: 晴天哥_王志 | 来源:发表于2020-01-03 00:02 被阅读0次

    开屏

    • Dubbo Admin实现原理文章之后,我们要具体看下Dubbo Admin各类数据的实际获取的例子。包含提供者、消费、路由规则、动态配置数据、访问控制、权重调节、负载均衡等。

    • 文章是基于dubbo-2.6.0的版本进行分析。

    • Dubbo Admin的服务大致分为下图,基本上每类角色一个对应的服务实现。


      Dubbo Admin服务类图
    • registryCache保存了zookeeper上Dubbo服务节点上的所有信息,按照
      ConcurrentMap<category, ConcurrentMap<servicename, Map<Long, URL>>>的数据结构进行保存,其中category包含providers,consumers,routers,configurators。

    提供者数据

    提供者
    public class ProviderServiceImpl extends AbstractService implements ProviderService {
    
        public List<Provider> findAll() {
            return SyncUtils.url2ProviderList(findAllProviderUrl());
        }
    
        private Map<Long, URL> findAllProviderUrl() {
            Map<String, String> filter = new HashMap<String, String>();
            filter.put(Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY);
            return SyncUtils.filterFromCategory(getRegistryCache(), filter);
        }
    }
    
    
    public class SyncUtils {
    
        public static Provider url2Provider(Pair<Long, URL> pair) {
            if (pair == null) {
                return null;
            }
    
            Long id = pair.getKey();
            URL url = pair.getValue();
    
            if (url == null)
                return null;
    
            Provider p = new Provider();
            p.setId(id);
            p.setService(url.getServiceKey());
            p.setAddress(url.getAddress());
            p.setApplication(url.getParameter(Constants.APPLICATION_KEY));
            p.setUrl(url.toIdentityString());
            p.setParameters(url.toParameterString());
    
            p.setDynamic(url.getParameter("dynamic", true));
            p.setEnabled(url.getParameter(Constants.ENABLED_KEY, true));
            p.setWeight(url.getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT));
            p.setUsername(url.getParameter("owner"));
    
            return p;
        }
    
        public static List<Provider> url2ProviderList(Map<Long, URL> ps) {
            List<Provider> ret = new ArrayList<Provider>();
            for (Map.Entry<Long, URL> entry : ps.entrySet()) {
                ret.add(url2Provider(new Pair<Long, URL>(entry.getKey(), entry.getValue())));
            }
            return ret;
        }
    }
    
    • getRegistryCache()返回的是registryCache,保存了zookeeper上Dubbo服务节点上的所有信息。

    • findAllProviderUrl()根据包含category=provider的filter获取Map<Long, URL>的返回值,其中key是Dubbo Admin的内部维护的唯一表示ID,value为对应的服务的URL对象。

    • SyncUtils.url2ProviderList()方法负责将provider的URL转为provider对象。

    消费者数据

    消费者
    public class ConsumerServiceImpl extends AbstractService implements ConsumerService {
    
        public List<Consumer> findAll() {
            return SyncUtils.url2ConsumerList(findAllConsumerUrl());
        }
    
        private Map<Long, URL> findAllConsumerUrl() {
            Map<String, String> filter = new HashMap<String, String>();
            filter.put(Constants.CATEGORY_KEY, Constants.CONSUMERS_CATEGORY);
            return SyncUtils.filterFromCategory(getRegistryCache(), filter);
        }
    }
    
    
    public class SyncUtils {
    
        public static List<Consumer> url2ConsumerList(Map<Long, URL> cs) {
            List<Consumer> list = new ArrayList<Consumer>();
            if (cs == null) return list;
            for (Map.Entry<Long, URL> entry : cs.entrySet()) {
                list.add(url2Consumer(new Pair<Long, URL>(entry.getKey(), entry.getValue())));
            }
            return list;
        }
    
        public static Consumer url2Consumer(Pair<Long, URL> pair) {
            if (pair == null) {
                return null;
            }
    
            Long id = pair.getKey();
            URL url = pair.getValue();
    
            if (null == url)
                return null;
    
            Consumer c = new Consumer();
            c.setId(id);
            c.setService(url.getServiceKey());
            c.setAddress(url.getHost());
            c.setApplication(url.getParameter(Constants.APPLICATION_KEY));
            c.setParameters(url.toParameterString());
    
            return c;
        }
    }
    
    • getRegistryCache()返回的是registryCache,保存了zookeeper上Dubbo服务节点上的所有信息。

    • findAllConsumerUrl()根据包含category=consumer的filter获取Map<Long, URL>的返回值,其中key是Dubbo Admin的内部维护的唯一表示ID,value为对应的服务的URL对象。

    • SyncUtils.url2ProviderList()方法负责将Consumer的URL转为Consumer对象。

    路由规则数据

    路由规则
    public class RouteServiceImpl extends AbstractService implements RouteService {
    
        public List<Route> findAll() {
            return SyncUtils.url2RouteList(findAllUrl());
        }
    
        private Map<Long, URL> findAllUrl() {
            Map<String, String> filter = new HashMap<String, String>();
            filter.put(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY);
    
            return SyncUtils.filterFromCategory(getRegistryCache(), filter);
        }
    }
    
    
    public class SyncUtils {
    
        public static List<Route> url2RouteList(Map<Long, URL> cs) {
            List<Route> list = new ArrayList<Route>();
            if (cs == null) return list;
            for (Map.Entry<Long, URL> entry : cs.entrySet()) {
                list.add(url2Route(new Pair<Long, URL>(entry.getKey(), entry.getValue())));
            }
            return list;
        }
    
        public static Route url2Route(Pair<Long, URL> pair) {
            if (pair == null) {
                return null;
            }
    
            Long id = pair.getKey();
            URL url = pair.getValue();
    
            if (null == url)
                return null;
    
            Route r = new Route();
            r.setId(id);
            r.setName(url.getParameter("name"));
            r.setService(url.getServiceKey());
            r.setPriority(url.getParameter(Constants.PRIORITY_KEY, 0));
            r.setEnabled(url.getParameter(Constants.ENABLED_KEY, true));
            r.setForce(url.getParameter(Constants.FORCE_KEY, false));
            r.setRule(url.getParameterAndDecoded(Constants.RULE_KEY));
            return r;
        }
    }
    
    • getRegistryCache()返回的是registryCache,保存了zookeeper上Dubbo服务节点上的所有信息。

    • findAllUrl()根据包含category=routers的filter获取Map<Long, URL>的返回值,其中key是Dubbo Admin的内部维护的唯一表示ID,value为对应的服务的URL对象。

    • SyncUtils.url2RouteList()方法负责将Router的URL转为Router对象。

    动态配置数据

    动态配置
    public class OverrideServiceImpl extends AbstractService implements OverrideService {
    
        public List<Override> findAll() {
            return SyncUtils.url2OverrideList(findOverrideUrl(null, null, null));
        }
    
        private Map<Long, URL> findOverrideUrl(String service, String address, String application) {
            Map<String, String> filter = new HashMap<String, String>();
            filter.put(Constants.CATEGORY_KEY, Constants.CONFIGURATORS_CATEGORY);
            if (service != null && service.length() > 0) {
                filter.put(SyncUtils.SERVICE_FILTER_KEY, service);
            }
            if (address != null && address.length() > 0) {
                filter.put(SyncUtils.ADDRESS_FILTER_KEY, address);
            }
            if (application != null && application.length() > 0) {
                filter.put(Constants.APPLICATION_KEY, application);
            }
            return SyncUtils.filterFromCategory(getRegistryCache(), filter);
        }
    }
    
    
    public class SyncUtils {
    
        public static List<com.alibaba.dubbo.registry.common.domain.Override> url2OverrideList(Map<Long, URL> cs) {
            List<com.alibaba.dubbo.registry.common.domain.Override> list = new ArrayList<com.alibaba.dubbo.registry.common.domain.Override>();
            if (cs == null) return list;
            for (Map.Entry<Long, URL> entry : cs.entrySet()) {
                list.add(url2Override(new Pair<Long, URL>(entry.getKey(), entry.getValue())));
            }
            return list;
        }
    
        public static com.alibaba.dubbo.registry.common.domain.Override url2Override(Pair<Long, URL> pair) {
            if (pair == null) {
                return null;
            }
    
            Long id = pair.getKey();
            URL url = pair.getValue();
    
            if (null == url)
                return null;
    
            com.alibaba.dubbo.registry.common.domain.Override o = new com.alibaba.dubbo.registry.common.domain.Override();
            o.setId(id);
    
            Map<String, String> parameters = new HashMap<String, String>(url.getParameters());
    
            o.setService(url.getServiceKey());
            parameters.remove(Constants.INTERFACE_KEY);
            parameters.remove(Constants.GROUP_KEY);
            parameters.remove(Constants.VERSION_KEY);
            parameters.remove(Constants.APPLICATION_KEY);
            parameters.remove(Constants.CATEGORY_KEY);
            parameters.remove(Constants.DYNAMIC_KEY);
            parameters.remove(Constants.ENABLED_KEY);
    
            o.setEnabled(url.getParameter(Constants.ENABLED_KEY, true));
    
            String host = url.getHost();
            boolean anyhost = url.getParameter(Constants.ANYHOST_VALUE, false);
            if (!anyhost || !"0.0.0.0".equals(host)) {
                o.setAddress(url.getAddress());
            }
    
            o.setApplication(url.getParameter(Constants.APPLICATION_KEY, url.getUsername()));
            parameters.remove(Constants.VERSION_KEY);
    
            o.setParams(StringUtils.toQueryString(parameters));
    
            return o;
        }
    }
    
    • getRegistryCache()返回的是registryCache,保存了zookeeper上Dubbo服务节点上的所有信息。

    • findAllUrl()根据包含category=configurators的filter获取Map<Long, URL>的返回值,其中key是Dubbo Admin的内部维护的唯一表示ID,value为对应的服务的URL对象。

    • SyncUtils.url2Override()方法负责将configurators的URL转为Override对象。

    访问控制数据

    访问控制
    public class RouteServiceImpl extends AbstractService implements RouteService {
    
        public List<Route> findAllForceRoute() {
            return SyncUtils.url2RouteList(findRouteUrl(null, null, true));
        }
    
        private Map<Long, URL> findRouteUrl(String service, String address, boolean force) {
            Map<String, String> filter = new HashMap<String, String>();
            filter.put(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY);
            if (service != null && service.length() > 0) {
                filter.put(SyncUtils.SERVICE_FILTER_KEY, service);
            }
            if (address != null && address.length() > 0) {
                filter.put(SyncUtils.ADDRESS_FILTER_KEY, address);
            }
            if (force) {
                filter.put("force", "true");
            }
            return SyncUtils.filterFromCategory(getRegistryCache(), filter);
        }
    }
    
    
    public class SyncUtils {
    
        public static List<Route> url2RouteList(Map<Long, URL> cs) {
            List<Route> list = new ArrayList<Route>();
            if (cs == null) return list;
            for (Map.Entry<Long, URL> entry : cs.entrySet()) {
                list.add(url2Route(new Pair<Long, URL>(entry.getKey(), entry.getValue())));
            }
            return list;
        }
    
        public static Route url2Route(Pair<Long, URL> pair) {
            if (pair == null) {
                return null;
            }
    
            Long id = pair.getKey();
            URL url = pair.getValue();
    
            if (null == url)
                return null;
    
            Route r = new Route();
            r.setId(id);
            r.setName(url.getParameter("name"));
            r.setService(url.getServiceKey());
            r.setPriority(url.getParameter(Constants.PRIORITY_KEY, 0));
            r.setEnabled(url.getParameter(Constants.ENABLED_KEY, true));
            r.setForce(url.getParameter(Constants.FORCE_KEY, false));
            r.setRule(url.getParameterAndDecoded(Constants.RULE_KEY));
            return r;
        }
    }
    
    • getRegistryCache()返回的是registryCache,保存了zookeeper上Dubbo服务节点上的所有信息。

    • findAllUrl()根据包含category=routers的filter获取Map<Long, URL>的返回值,其中key是Dubbo Admin的内部维护的唯一表示ID,value为对应的服务的URL对象。

    • SyncUtils.url2Override()方法负责将routers的URL转为router对象。

    权重调节数据

    权重调节
    public class OverrideServiceImpl extends AbstractService implements OverrideService {
    
        public List<Override> findAll() {
            return SyncUtils.url2OverrideList(findOverrideUrl(null, null, null));
        }
    
        private Map<Long, URL> findOverrideUrl(String service, String address, String application) {
            Map<String, String> filter = new HashMap<String, String>();
            filter.put(Constants.CATEGORY_KEY, Constants.CONFIGURATORS_CATEGORY);
            if (service != null && service.length() > 0) {
                filter.put(SyncUtils.SERVICE_FILTER_KEY, service);
            }
            if (address != null && address.length() > 0) {
                filter.put(SyncUtils.ADDRESS_FILTER_KEY, address);
            }
            if (application != null && application.length() > 0) {
                filter.put(Constants.APPLICATION_KEY, application);
            }
            return SyncUtils.filterFromCategory(getRegistryCache(), filter);
        }
    }
    
    
    public class SyncUtils {
    
        public static List<com.alibaba.dubbo.registry.common.domain.Override> url2OverrideList(Map<Long, URL> cs) {
            List<com.alibaba.dubbo.registry.common.domain.Override> list = new ArrayList<com.alibaba.dubbo.registry.common.domain.Override>();
            if (cs == null) return list;
            for (Map.Entry<Long, URL> entry : cs.entrySet()) {
                list.add(url2Override(new Pair<Long, URL>(entry.getKey(), entry.getValue())));
            }
            return list;
        }
    
        public static com.alibaba.dubbo.registry.common.domain.Override url2Override(Pair<Long, URL> pair) {
            if (pair == null) {
                return null;
            }
    
            Long id = pair.getKey();
            URL url = pair.getValue();
    
            if (null == url)
                return null;
    
            com.alibaba.dubbo.registry.common.domain.Override o = new com.alibaba.dubbo.registry.common.domain.Override();
            o.setId(id);
    
            Map<String, String> parameters = new HashMap<String, String>(url.getParameters());
    
            o.setService(url.getServiceKey());
            parameters.remove(Constants.INTERFACE_KEY);
            parameters.remove(Constants.GROUP_KEY);
            parameters.remove(Constants.VERSION_KEY);
            parameters.remove(Constants.APPLICATION_KEY);
            parameters.remove(Constants.CATEGORY_KEY);
            parameters.remove(Constants.DYNAMIC_KEY);
            parameters.remove(Constants.ENABLED_KEY);
    
            o.setEnabled(url.getParameter(Constants.ENABLED_KEY, true));
    
            String host = url.getHost();
            boolean anyhost = url.getParameter(Constants.ANYHOST_VALUE, false);
            if (!anyhost || !"0.0.0.0".equals(host)) {
                o.setAddress(url.getAddress());
            }
    
            o.setApplication(url.getParameter(Constants.APPLICATION_KEY, url.getUsername()));
            parameters.remove(Constants.VERSION_KEY);
    
            o.setParams(StringUtils.toQueryString(parameters));
    
            return o;
        }
    }
    
    
    public class OverrideUtils {
    
        public static List<Weight> overridesToWeights(List<Override> overrides) {
            List<Weight> weights = new ArrayList<Weight>();
            if (overrides == null) {
                return weights;
            }
            for (Override o : overrides) {
                if (StringUtils.isEmpty(o.getParams())) {
                    continue;
                } else {
                    Map<String, String> params = StringUtils.parseQueryString(o.getParams());
                    for (Map.Entry<String, String> entry : params.entrySet()) {
                        if (entry.getKey().equals("weight")) {
                            Weight weight = new Weight();
                            weight.setAddress(o.getAddress());
                            weight.setId(o.getId());
                            weight.setService(o.getService());
                            weight.setWeight(Integer.valueOf(entry.getValue()));
                            weights.add(weight);
                        }
                    }
                }
            }
            return weights;
        }
    }
    
    • getRegistryCache()返回的是registryCache,保存了zookeeper上Dubbo服务节点上的所有信息。

    • findAllUrl()根据包含category=configurators的filter获取Map<Long, URL>的返回值,其中key是Dubbo Admin的内部维护的唯一表示ID,value为对应的服务的URL对象。

    • SyncUtils.url2Override()方法负责将configurators的URL转为Override对象。

    • overridesToWeights负责将Override对象转为Weight对象。

    负载均衡数据

    负载均衡
    public class OverrideServiceImpl extends AbstractService implements OverrideService {
    
        public List<Override> findAll() {
            return SyncUtils.url2OverrideList(findOverrideUrl(null, null, null));
        }
    
        private Map<Long, URL> findOverrideUrl(String service, String address, String application) {
            Map<String, String> filter = new HashMap<String, String>();
            filter.put(Constants.CATEGORY_KEY, Constants.CONFIGURATORS_CATEGORY);
            if (service != null && service.length() > 0) {
                filter.put(SyncUtils.SERVICE_FILTER_KEY, service);
            }
            if (address != null && address.length() > 0) {
                filter.put(SyncUtils.ADDRESS_FILTER_KEY, address);
            }
            if (application != null && application.length() > 0) {
                filter.put(Constants.APPLICATION_KEY, application);
            }
            return SyncUtils.filterFromCategory(getRegistryCache(), filter);
        }
    }
    
    
    public class SyncUtils {
    
        public static List<com.alibaba.dubbo.registry.common.domain.Override> url2OverrideList(Map<Long, URL> cs) {
            List<com.alibaba.dubbo.registry.common.domain.Override> list = new ArrayList<com.alibaba.dubbo.registry.common.domain.Override>();
            if (cs == null) return list;
            for (Map.Entry<Long, URL> entry : cs.entrySet()) {
                list.add(url2Override(new Pair<Long, URL>(entry.getKey(), entry.getValue())));
            }
            return list;
        }
    
        public static com.alibaba.dubbo.registry.common.domain.Override url2Override(Pair<Long, URL> pair) {
            if (pair == null) {
                return null;
            }
    
            Long id = pair.getKey();
            URL url = pair.getValue();
    
            if (null == url)
                return null;
    
            com.alibaba.dubbo.registry.common.domain.Override o = new com.alibaba.dubbo.registry.common.domain.Override();
            o.setId(id);
    
            Map<String, String> parameters = new HashMap<String, String>(url.getParameters());
    
            o.setService(url.getServiceKey());
            parameters.remove(Constants.INTERFACE_KEY);
            parameters.remove(Constants.GROUP_KEY);
            parameters.remove(Constants.VERSION_KEY);
            parameters.remove(Constants.APPLICATION_KEY);
            parameters.remove(Constants.CATEGORY_KEY);
            parameters.remove(Constants.DYNAMIC_KEY);
            parameters.remove(Constants.ENABLED_KEY);
    
            o.setEnabled(url.getParameter(Constants.ENABLED_KEY, true));
    
            String host = url.getHost();
            boolean anyhost = url.getParameter(Constants.ANYHOST_VALUE, false);
            if (!anyhost || !"0.0.0.0".equals(host)) {
                o.setAddress(url.getAddress());
            }
    
            o.setApplication(url.getParameter(Constants.APPLICATION_KEY, url.getUsername()));
            parameters.remove(Constants.VERSION_KEY);
    
            o.setParams(StringUtils.toQueryString(parameters));
    
            return o;
        }
    }
    
    
    public class OverrideUtils {
        public static List<LoadBalance> overridesToLoadBalances(List<Override> overrides) {
            List<LoadBalance> loadBalances = new ArrayList<LoadBalance>();
            if (overrides == null) {
                return loadBalances;
            }
            for (Override o : overrides) {
                if (StringUtils.isEmpty(o.getParams())) {
                    continue;
                } else {
                    Map<String, String> params = StringUtils.parseQueryString(o.getParams());
                    for (Map.Entry<String, String> entry : params.entrySet()) {
                        if (entry.getKey().endsWith("loadbalance")) {
                            LoadBalance loadBalance = new LoadBalance();
                            String method = null;
                            if (entry.getKey().endsWith(".loadbalance")) {
                                method = entry.getKey().split(".loadbalance")[0];
                            } else {
                                method = "*";
                            }
    
                            loadBalance.setMethod(method);
                            loadBalance.setId(o.getId());
                            loadBalance.setService(o.getService());
                            loadBalance.setStrategy(entry.getValue());
                            loadBalances.add(loadBalance);
    
                        }
                    }
                }
            }
            return loadBalances;
        }
    }
    
    • getRegistryCache()返回的是registryCache,保存了zookeeper上Dubbo服务节点上的所有信息。

    • findAllUrl()根据包含category=configurators的filter获取Map<Long, URL>的返回值,其中key是Dubbo Admin的内部维护的唯一表示ID,value为对应的服务的URL对象。

    • SyncUtils.url2Override()方法负责将configurators的URL转为Override对象。

    • overridesToLoadBalances负责将Override对象转为LoadBalance对象。

    相关文章

      网友评论

        本文标题:Dubbo Admin 数据获取原理

        本文链接:https://www.haomeiwen.com/subject/eeraoctx.html