Search in sources :

Example 26 with Override

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);
    }
}
Also used : Override(com.alibaba.dubbo.registry.common.domain.Override) Provider(com.alibaba.dubbo.registry.common.domain.Provider)

Example 27 with Override

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;
}
Also used : Override(com.alibaba.dubbo.registry.common.domain.Override)

Example 28 with 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;
}
Also used : Override(com.alibaba.dubbo.registry.common.domain.Override)

Example 29 with 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;
}
Also used : LoadBalance(com.alibaba.dubbo.registry.common.domain.LoadBalance) ArrayList(java.util.ArrayList) Override(com.alibaba.dubbo.registry.common.domain.Override) Map(java.util.Map)

Example 30 with Override

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);
}
Also used : ArrayList(java.util.ArrayList) Override(com.alibaba.dubbo.registry.common.domain.Override)

Aggregations

Override (com.alibaba.dubbo.registry.common.domain.Override)30 ArrayList (java.util.ArrayList)10 Provider (com.alibaba.dubbo.registry.common.domain.Provider)8 HashMap (java.util.HashMap)7 Map (java.util.Map)6 Consumer (com.alibaba.dubbo.registry.common.domain.Consumer)4 Route (com.alibaba.dubbo.registry.common.domain.Route)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 LoadBalance (com.alibaba.dubbo.registry.common.domain.LoadBalance)1 Owner (com.alibaba.dubbo.registry.common.domain.Owner)1 Weight (com.alibaba.dubbo.registry.common.domain.Weight)1