use of com.alibaba.dubbo.registry.common.domain.Provider in project dubbo by alibaba.
the class Consumers method index.
public void index(Map<String, Object> context) throws Exception {
String service = (String) context.get("service");
String application = (String) context.get("application");
String address = (String) context.get("address");
List<Consumer> consumers;
List<Override> overrides;
List<Provider> providers = null;
List<Route> routes = null;
// service
if (service != null && service.length() > 0) {
consumers = consumerService.findByService(service);
overrides = overrideService.findByService(service);
providers = providerService.findByService(service);
routes = routeService.findByService(service);
} else // address
if (address != null && address.length() > 0) {
consumers = consumerService.findByAddress(address);
overrides = overrideService.findByAddress(Tool.getIP(address));
} else // application
if (application != null && application.length() > 0) {
consumers = consumerService.findByApplication(application);
overrides = overrideService.findByApplication(application);
} else // all
{
consumers = consumerService.findAll();
overrides = overrideService.findAll();
}
if (consumers != null && consumers.size() > 0) {
for (Consumer consumer : consumers) {
if (service == null || service.length() == 0) {
providers = providerService.findByService(consumer.getService());
routes = routeService.findByService(consumer.getService());
}
List<Route> routed = new ArrayList<Route>();
consumer.setProviders(RouteUtils.route(consumer.getService(), consumer.getAddress(), consumer.getParameters(), providers, overrides, routes, null, routed));
consumer.setRoutes(routed);
OverrideUtils.setConsumerOverrides(consumer, overrides);
}
}
context.put("consumers", consumers);
}
use of com.alibaba.dubbo.registry.common.domain.Provider in project dubbo by alibaba.
the class Providers method update.
public boolean update(Provider newProvider, Map<String, Object> context) {
Long id = newProvider.getId();
String parameters = newProvider.getParameters();
Provider provider = providerService.findProvider(id);
if (provider == null) {
context.put("message", getMessage("NoSuchOperationData", id));
return false;
}
String service = provider.getService();
if (!super.currentUser.hasServicePrivilege(service)) {
context.put("message", getMessage("HaveNoServicePrivilege", service));
return false;
}
Map<String, String> oldMap = StringUtils.parseQueryString(provider.getParameters());
Map<String, String> newMap = StringUtils.parseQueryString(parameters);
for (Map.Entry<String, String> entry : oldMap.entrySet()) {
if (entry.getValue().equals(newMap.get(entry.getKey()))) {
newMap.remove(entry.getKey());
}
}
if (provider.isDynamic()) {
String address = provider.getAddress();
List<Override> overrides = overrideService.findByServiceAndAddress(provider.getService(), provider.getAddress());
OverrideUtils.setProviderOverrides(provider, overrides);
Override override = provider.getOverride();
if (override != null) {
if (newMap.size() > 0) {
override.setParams(StringUtils.toQueryString(newMap));
override.setEnabled(true);
override.setOperator(operator);
override.setOperatorAddress(operatorAddress);
overrideService.updateOverride(override);
} else {
overrideService.deleteOverride(override.getId());
}
} else {
override = new Override();
override.setService(service);
override.setAddress(address);
override.setParams(StringUtils.toQueryString(newMap));
override.setEnabled(true);
override.setOperator(operator);
override.setOperatorAddress(operatorAddress);
overrideService.saveOverride(override);
}
} else {
provider.setParameters(parameters);
providerService.updateProvider(provider);
}
return true;
}
use of com.alibaba.dubbo.registry.common.domain.Provider in project dubbo by alibaba.
the class SyncUtils method url2Provider.
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;
}
use of com.alibaba.dubbo.registry.common.domain.Provider 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.Provider in project dubbo by alibaba.
the class Dump method providers.
public void providers(Map<String, Object> context) throws IOException {
PrintWriter writer = response.getWriter();
List<Provider> providers = providerDAO.findAll();
List<String> sortedProviders = new ArrayList<String>();
for (Provider provider : providers) {
sortedProviders.add(provider.getUrl() + " " + provider.getService());
}
Collections.sort(sortedProviders);
writer.println(sortedProviders.size() + " provider instance");
for (String provider : sortedProviders) {
writer.println(provider);
}
writer.flush();
response.setContentType("text/plain");
}
Aggregations