use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class FailbackRegistry method unregister.
@Override
public void unregister(URL url) {
failedRegistered.remove(url);
failedUnregistered.remove(url);
try {
super.unregister(url);
} catch (Exception e) {
if (isCheckingUrls(getUrl(), url)) {
throw new MotanFrameworkException(String.format("[%s] false to unregistery %s to %s", registryClassName, url, getUrl()), e);
}
failedUnregistered.add(url);
}
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class FailbackRegistry method register.
@Override
public void register(URL url) {
failedRegistered.remove(url);
failedUnregistered.remove(url);
try {
super.register(url);
} catch (Exception e) {
if (isCheckingUrls(getUrl(), url)) {
throw new MotanFrameworkException(String.format("[%s] false to registery %s to %s", registryClassName, url, getUrl()), e);
}
failedRegistered.add(url);
}
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class ClusterSupport method init.
public void init() {
prepareCluster();
URL subUrl = toSubscribeUrl(url);
for (URL ru : registryUrls) {
String directUrlStr = ru.getParameter(URLParamType.directUrl.getName());
// 如果有directUrl,直接使用这些directUrls进行初始化,不用到注册中心discover
if (StringUtils.isNotBlank(directUrlStr)) {
List<URL> directUrls = parseDirectUrls(directUrlStr);
if (!directUrls.isEmpty()) {
notify(ru, directUrls);
LoggerUtil.info("Use direct urls, refUrl={}, directUrls={}", url, directUrls);
continue;
}
}
// client 注册自己,同时订阅service列表
Registry registry = getRegistry(ru);
registry.subscribe(subUrl, this);
}
boolean check = Boolean.parseBoolean(url.getParameter(URLParamType.check.getName(), URLParamType.check.getValue()));
if (!CollectionUtil.isEmpty(cluster.getReferers()) || !check) {
cluster.init();
if (CollectionUtil.isEmpty(cluster.getReferers()) && !check) {
LoggerUtil.warn(String.format("refer:%s", this.url.getPath() + "/" + this.url.getVersion()), "No services");
}
return;
}
throw new MotanFrameworkException(String.format("ClusterSupport No service urls for the refer:%s, registries:%s", this.url.getIdentity(), registryUrls), MotanErrorMsgConstant.SERVICE_UNFOUND);
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class RefererConfig method initRef.
@SuppressWarnings({ "unchecked", "rawtypes" })
public synchronized void initRef() {
if (initialized.get()) {
return;
}
try {
interfaceClass = (Class) Class.forName(interfaceClass.getName(), true, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw new MotanFrameworkException("ReferereConfig initRef Error: Class not found " + interfaceClass.getName(), e, MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
if (CollectionUtil.isEmpty(protocols)) {
throw new MotanFrameworkException(String.format("%s RefererConfig is malformed, for protocol not set correctly!", interfaceClass.getName()));
}
checkInterfaceAndMethods(interfaceClass, methods);
clusterSupports = new ArrayList<ClusterSupport<T>>(protocols.size());
List<Cluster<T>> clusters = new ArrayList<Cluster<T>>(protocols.size());
String proxy = null;
ConfigHandler configHandler = ExtensionLoader.getExtensionLoader(ConfigHandler.class).getExtension(MotanConstants.DEFAULT_VALUE);
List<URL> registryUrls = loadRegistryUrls();
String localIp = getLocalHostAddress(registryUrls);
for (ProtocolConfig protocol : protocols) {
Map<String, String> params = new HashMap<String, String>();
params.put(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_REFERER);
params.put(URLParamType.version.getName(), URLParamType.version.getValue());
params.put(URLParamType.refreshTimestamp.getName(), String.valueOf(System.currentTimeMillis()));
collectConfigParams(params, protocol, basicReferer, extConfig, this);
collectMethodConfigParams(params, this.getMethods());
URL refUrl = new URL(protocol.getName(), localIp, MotanConstants.DEFAULT_INT_VALUE, interfaceClass.getName(), params);
ClusterSupport<T> clusterSupport = createClusterSupport(refUrl, configHandler, registryUrls);
clusterSupports.add(clusterSupport);
clusters.add(clusterSupport.getCluster());
proxy = (proxy == null) ? refUrl.getParameter(URLParamType.proxy.getName(), URLParamType.proxy.getValue()) : proxy;
}
ref = configHandler.refer(interfaceClass, clusters, proxy);
initialized.set(true);
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class CommandServiceManager method discoverServiceWithCommand.
public List<URL> discoverServiceWithCommand(URL serviceUrl, Map<String, Integer> weights, RpcCommand rpcCommand, String localIP) {
if (rpcCommand == null || CollectionUtil.isEmpty(rpcCommand.getClientCommandList())) {
return discoverOneGroup(serviceUrl);
}
List<URL> mergedResult = new LinkedList<URL>();
String path = serviceUrl.getPath();
List<RpcCommand.ClientCommand> clientCommandList = rpcCommand.getClientCommandList();
boolean hit = false;
for (RpcCommand.ClientCommand command : clientCommandList) {
mergedResult = new LinkedList<URL>();
// 判断当前url是否符合过滤条件
boolean match = RpcCommandUtil.match(command.getPattern(), path);
if (match) {
hit = true;
if (!CollectionUtil.isEmpty(command.getMergeGroups())) {
// 计算出所有要合并的分组及权重
try {
buildWeightsMap(weights, command);
} catch (MotanFrameworkException e) {
LoggerUtil.warn("build weights map fail!" + e.getMessage());
continue;
}
// 根据计算结果,分别发现各个group的service,合并结果
mergedResult.addAll(mergeResult(serviceUrl, weights));
} else {
mergedResult.addAll(discoverOneGroup(serviceUrl));
}
LoggerUtil.info("mergedResult: size-" + mergedResult.size() + " --- " + mergedResult.toString());
if (!CollectionUtil.isEmpty(command.getRouteRules())) {
LoggerUtil.info("router: " + command.getRouteRules().toString());
for (String routeRule : command.getRouteRules()) {
String[] fromTo = routeRule.replaceAll("\\s+", "").split("to");
if (fromTo.length != 2) {
routeRuleConfigError();
continue;
}
String from = fromTo[0];
String to = fromTo[1];
if (from.length() < 1 || to.length() < 1 || !IP_PATTERN.matcher(from).find() || !IP_PATTERN.matcher(to).find()) {
routeRuleConfigError();
continue;
}
boolean oppositeFrom = from.startsWith("!");
boolean oppositeTo = to.startsWith("!");
if (oppositeFrom) {
from = from.substring(1);
}
if (oppositeTo) {
to = to.substring(1);
}
int idx = from.indexOf('*');
boolean matchFrom;
if (idx != -1) {
matchFrom = localIP.startsWith(from.substring(0, idx));
} else {
matchFrom = localIP.equals(from);
}
// 开头有!,取反
if (oppositeFrom) {
matchFrom = !matchFrom;
}
LoggerUtil.info("matchFrom: " + matchFrom + ", localip:" + localIP + ", from:" + from);
if (matchFrom) {
boolean matchTo;
Iterator<URL> iterator = mergedResult.iterator();
while (iterator.hasNext()) {
URL url = iterator.next();
if (url.getProtocol().equalsIgnoreCase("rule")) {
continue;
}
idx = to.indexOf('*');
if (idx != -1) {
matchTo = url.getHost().startsWith(to.substring(0, idx));
} else {
matchTo = url.getHost().equals(to);
}
if (oppositeTo) {
matchTo = !matchTo;
}
if (!matchTo) {
iterator.remove();
LoggerUtil.info("router To not match. url remove : " + url.toSimpleString());
}
}
}
}
}
// 只取第一个匹配的 TODO 考虑是否能满足绝大多数场景需求
break;
}
}
List<URL> finalResult = new ArrayList<URL>();
if (!hit) {
finalResult = discoverOneGroup(serviceUrl);
} else {
finalResult.addAll(mergedResult);
}
return finalResult;
}
Aggregations