開(kāi)屏
繼Dubbo Admin實(shí)現(xiàn)原理文章之后,我們要具體看下Dubbo Admin各類數(shù)據(jù)的實(shí)際獲取的例子这嚣。包含提供者鸥昏、消費(fèi)、路由規(guī)則姐帚、動(dòng)態(tài)配置數(shù)據(jù)吏垮、訪問(wèn)控制、權(quán)重調(diào)節(jié)、負(fù)載均衡等惫皱。
文章是基于dubbo-2.6.0的版本進(jìn)行分析像樊。
-
Dubbo Admin的服務(wù)大致分為下圖,基本上每類角色一個(gè)對(duì)應(yīng)的服務(wù)實(shí)現(xiàn)旅敷。
Dubbo Admin服務(wù)類圖 registryCache保存了zookeeper上Dubbo服務(wù)節(jié)點(diǎn)上的所有信息生棍,按照
ConcurrentMap<category, ConcurrentMap<servicename, Map<Long, URL>>>的數(shù)據(jù)結(jié)構(gòu)進(jìn)行保存,其中category包含providers,consumers,routers,configurators媳谁。
提供者數(shù)據(jù)
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服務(wù)節(jié)點(diǎn)上的所有信息。
findAllProviderUrl()根據(jù)包含category=provider的filter獲取Map<Long, URL>的返回值晴音,其中key是Dubbo Admin的內(nèi)部維護(hù)的唯一表示ID柔纵,value為對(duì)應(yīng)的服務(wù)的URL對(duì)象。
SyncUtils.url2ProviderList()方法負(fù)責(zé)將provider的URL轉(zhuǎn)為provider對(duì)象锤躁。
消費(fèi)者數(shù)據(jù)
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服務(wù)節(jié)點(diǎn)上的所有信息。
findAllConsumerUrl()根據(jù)包含category=consumer的filter獲取Map<Long, URL>的返回值系羞,其中key是Dubbo Admin的內(nèi)部維護(hù)的唯一表示ID郭计,value為對(duì)應(yīng)的服務(wù)的URL對(duì)象。
SyncUtils.url2ProviderList()方法負(fù)責(zé)將Consumer的URL轉(zhuǎn)為Consumer對(duì)象椒振。
路由規(guī)則數(shù)據(jù)
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服務(wù)節(jié)點(diǎn)上的所有信息。
findAllUrl()根據(jù)包含category=routers的filter獲取Map<Long, URL>的返回值澎迎,其中key是Dubbo Admin的內(nèi)部維護(hù)的唯一表示ID庐杨,value為對(duì)應(yīng)的服務(wù)的URL對(duì)象。
SyncUtils.url2RouteList()方法負(fù)責(zé)將Router的URL轉(zhuǎn)為Router對(duì)象夹供。
動(dòng)態(tài)配置數(shù)據(jù)
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服務(wù)節(jié)點(diǎn)上的所有信息。
findAllUrl()根據(jù)包含category=configurators的filter獲取Map<Long, URL>的返回值哮洽,其中key是Dubbo Admin的內(nèi)部維護(hù)的唯一表示ID填渠,value為對(duì)應(yīng)的服務(wù)的URL對(duì)象。
SyncUtils.url2Override()方法負(fù)責(zé)將configurators的URL轉(zhuǎn)為Override對(duì)象袁铐。
訪問(wèn)控制數(shù)據(jù)
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服務(wù)節(jié)點(diǎn)上的所有信息横浑。
findAllUrl()根據(jù)包含category=routers的filter獲取Map<Long, URL>的返回值剔桨,其中key是Dubbo Admin的內(nèi)部維護(hù)的唯一表示ID,value為對(duì)應(yīng)的服務(wù)的URL對(duì)象徙融。
SyncUtils.url2Override()方法負(fù)責(zé)將routers的URL轉(zhuǎn)為router對(duì)象洒缀。
權(quán)重調(diào)節(jié)數(shù)據(jù)
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服務(wù)節(jié)點(diǎn)上的所有信息。
findAllUrl()根據(jù)包含category=configurators的filter獲取Map<Long, URL>的返回值树绩,其中key是Dubbo Admin的內(nèi)部維護(hù)的唯一表示ID萨脑,value為對(duì)應(yīng)的服務(wù)的URL對(duì)象。
SyncUtils.url2Override()方法負(fù)責(zé)將configurators的URL轉(zhuǎn)為Override對(duì)象饺饭。
overridesToWeights負(fù)責(zé)將Override對(duì)象轉(zhuǎn)為Weight對(duì)象渤早。
負(fù)載均衡數(shù)據(jù)
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服務(wù)節(jié)點(diǎn)上的所有信息瘫俊。
findAllUrl()根據(jù)包含category=configurators的filter獲取Map<Long, URL>的返回值鹊杖,其中key是Dubbo Admin的內(nèi)部維護(hù)的唯一表示ID,value為對(duì)應(yīng)的服務(wù)的URL對(duì)象扛芽。
SyncUtils.url2Override()方法負(fù)責(zé)將configurators的URL轉(zhuǎn)為Override對(duì)象骂蓖。
overridesToLoadBalances負(fù)責(zé)將Override對(duì)象轉(zhuǎn)為L(zhǎng)oadBalance對(duì)象。