use of com.alibaba.dubbo.registry.common.domain.Override in project dubbo by alibaba.
the class ProviderServiceImpl method setWeight.
public void setWeight(Long id, float factor) {
if (id == null) {
throw new IllegalStateException("no provider id");
}
Provider oldProvider = findProvider(id);
if (oldProvider == null) {
throw new IllegalStateException("Provider was changed!");
}
Map<String, String> map = StringUtils.parseQueryString(oldProvider.getParameters());
String weight = map.get(Constants.WEIGHT_KEY);
if (oldProvider.isDynamic()) {
//保证disable的override唯一
List<Override> overrides = overrideService.findByServiceAndAddress(oldProvider.getService(), oldProvider.getAddress());
if (overrides == null || overrides.size() == 0) {
int value = getWeight(weight, factor);
if (value != Constants.DEFAULT_WEIGHT) {
Override override = new Override();
override.setAddress(oldProvider.getAddress());
override.setService(oldProvider.getService());
override.setEnabled(true);
override.setParams(Constants.WEIGHT_KEY + "=" + String.valueOf(value));
overrideService.saveOverride(override);
}
} else {
for (Override override : overrides) {
Map<String, String> params = StringUtils.parseQueryString(override.getParams());
String overrideWeight = params.get(Constants.WEIGHT_KEY);
if (overrideWeight == null || overrideWeight.length() == 0) {
overrideWeight = weight;
}
int value = getWeight(overrideWeight, factor);
if (value == getWeight(weight, 1)) {
params.remove(Constants.WEIGHT_KEY);
} else {
params.put(Constants.WEIGHT_KEY, String.valueOf(value));
}
if (params.size() > 0) {
override.setParams(StringUtils.toQueryString(params));
overrideService.updateOverride(override);
} else {
overrideService.deleteOverride(override.getId());
}
}
}
} else {
int value = getWeight(weight, factor);
if (value == Constants.DEFAULT_WEIGHT) {
map.remove(Constants.WEIGHT_KEY);
} else {
map.put(Constants.WEIGHT_KEY, String.valueOf(value));
}
oldProvider.setParameters(StringUtils.toQueryString(map));
updateProvider(oldProvider);
}
}
use of com.alibaba.dubbo.registry.common.domain.Override in project dubbo by alibaba.
the class OverrideUtils method weightToOverride.
public static Override weightToOverride(Weight weight) {
Override override = new Override();
override.setId(weight.getId());
override.setAddress(weight.getAddress());
override.setEnabled(true);
override.setParams("weight=" + weight.getWeight());
override.setService(weight.getService());
return override;
}
use of com.alibaba.dubbo.registry.common.domain.Override in project dubbo by alibaba.
the class OverrideUtils method loadBalanceToOverride.
public static Override loadBalanceToOverride(LoadBalance loadBalance) {
Override override = new Override();
override.setId(loadBalance.getId());
override.setService(loadBalance.getService());
override.setEnabled(true);
String method = loadBalance.getMethod();
String strategy = loadBalance.getStrategy();
if (StringUtils.isEmpty(method) || method.equals("*")) {
override.setParams("loadbalance=" + strategy);
} else {
override.setParams(method + ".loadbalance=" + strategy);
}
return override;
}
use of com.alibaba.dubbo.registry.common.domain.Override in project dubbo by alibaba.
the class OverrideUtils method overridesToLoadBalances.
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;
}
use of com.alibaba.dubbo.registry.common.domain.Override in project dubbo by alibaba.
the class OverrideUtils method setConsumerOverrides.
public static void setConsumerOverrides(Consumer consumer, List<Override> overrides) {
if (consumer == null || overrides == null) {
return;
}
List<Override> result = new ArrayList<Override>(overrides.size());
for (Override override : overrides) {
if (!override.isEnabled()) {
continue;
}
if (override.isMatch(consumer)) {
result.add(override);
}
if (override.isUniqueMatch(consumer)) {
consumer.setOverride(override);
}
}
Collections.sort(result, OverrideUtils.OVERRIDE_COMPARATOR);
consumer.setOverrides(result);
}
Aggregations