use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class ZookeeperRegistry method doRegister.
@Override
protected void doRegister(URL url) {
try {
serverLock.lock();
// 防止旧节点未正常注销
removeNode(url, ZkNodeType.AVAILABLE_SERVER);
removeNode(url, ZkNodeType.UNAVAILABLE_SERVER);
createNode(url, ZkNodeType.UNAVAILABLE_SERVER);
} catch (Throwable e) {
throw new MotanFrameworkException(String.format("Failed to register %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
} finally {
serverLock.unlock();
}
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class CompressRpcCodec method putMethodSign.
public static void putMethodSign(Provider<?> provider, List<Method> methods) {
String group = provider.getUrl().getGroup();
String interfaceName = provider.getInterface().getName();
String version = provider.getUrl().getVersion();
for (Method method : methods) {
MethodInfo temp = new MethodInfo(group, interfaceName, method.getName(), ReflectUtil.getMethodParamDesc(method), version);
String sign = temp.getSign();
MethodInfo priInfo = SIGN_METHOD_MAP.putIfAbsent(sign, temp);
if (priInfo != null && !temp.equals(priInfo)) {
// 方法签名冲突
throw new MotanFrameworkException("add method sign conflict! " + temp.toString() + " with " + priInfo.toString(), MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
} else {
LoggerUtil.info("add method sign:" + sign + ", methodinfo:" + temp.toString());
}
}
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class CompressRpcCodec method decodeRequest.
private Object decodeRequest(byte[] body, long requestId, String remoteIp, Serialization serialization) throws IOException, ClassNotFoundException {
ObjectInput input = createInput(getInputStream(body));
String interfaceName = null;
String methodName = null;
String paramtersDesc = null;
String group = null;
String version = null;
String flag = input.readUTF();
if (SIGN_FLAG.equals(flag)) {
// 方法签名方式
String sign = input.readUTF();
MethodInfo mInfo = SIGN_METHOD_MAP.get(sign);
if (mInfo == null) {
throw new MotanFrameworkException("decode error: invalid method sign: " + sign, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
interfaceName = mInfo.getInterfaceName();
methodName = mInfo.getMethodName();
paramtersDesc = mInfo.getParamtersDesc();
group = mInfo.getGroup();
version = mInfo.getVersion();
} else {
interfaceName = flag;
methodName = input.readUTF();
paramtersDesc = input.readUTF();
}
DefaultRequest rpcRequest = new DefaultRequest();
rpcRequest.setRequestId(requestId);
rpcRequest.setInterfaceName(interfaceName);
rpcRequest.setMethodName(methodName);
rpcRequest.setParamtersDesc(paramtersDesc);
rpcRequest.setArguments(decodeRequestParameter(input, paramtersDesc, serialization));
rpcRequest.setAttachments(decodeRequestAttachments(input));
rpcRequest.setRpcProtocolVersion(RpcProtocolVersion.VERSION_2.getVersion());
input.close();
Map<String, String> attachments = rpcRequest.getAttachments();
// 根据签名添加client固定参数。
putSignedAttachment(attachments, remoteIp);
if (attachments.get(URLParamType.group.name()) == null) {
// 如果attachment sign失效时,需要使用methodsign中的group信息。
attachments.put(URLParamType.group.name(), group);
attachments.put(URLParamType.version.name(), version);
}
return rpcRequest;
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class DefaultRpcCodec method decode.
/**
* decode data
*
* <pre>
* 对于client端:主要是来自server端的response or exception
* 对于server端: 主要是来自client端的request
* </pre>
*
* @param data
* @return
* @throws IOException
*/
@Override
public Object decode(Channel channel, String remoteIp, byte[] data) throws IOException {
if (data.length <= RpcProtocolVersion.VERSION_1.getHeaderLength()) {
throw new MotanFrameworkException("decode error: format problem", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
short type = ByteUtil.bytes2short(data, 0);
if (type != MAGIC) {
throw new MotanFrameworkException("decode error: magic error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
if (data[2] != RpcProtocolVersion.VERSION_1.getVersion()) {
throw new MotanFrameworkException("decode error: version error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
int bodyLength = ByteUtil.bytes2int(data, 12);
if (RpcProtocolVersion.VERSION_1.getHeaderLength() + bodyLength != data.length) {
throw new MotanFrameworkException("decode error: content length error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
byte flag = data[3];
byte dataType = (byte) (flag & MASK);
boolean isResponse = (dataType != MotanConstants.FLAG_REQUEST);
byte[] body = new byte[bodyLength];
System.arraycopy(data, RpcProtocolVersion.VERSION_1.getHeaderLength(), body, 0, bodyLength);
long requestId = ByteUtil.bytes2long(data, 4);
Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));
try {
if (isResponse) {
// response
return decodeResponse(body, dataType, requestId, serialization);
} else {
return decodeRequest(body, requestId, serialization);
}
} catch (ClassNotFoundException e) {
throw new MotanFrameworkException("decode " + (isResponse ? "response" : "request") + " error: class not found", e, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
} catch (Exception e) {
if (ExceptionUtil.isMotanException(e)) {
throw (RuntimeException) e;
} else {
throw new MotanFrameworkException("decode error: isResponse=" + isResponse, e, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
}
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class ServiceConfig method doExport.
@SuppressWarnings("unchecked")
private void doExport(ProtocolConfig protocolConfig, int port, List<URL> registryURLs) {
String protocolName = protocolConfig.getName();
if (protocolName == null || protocolName.length() == 0) {
protocolName = URLParamType.protocol.getValue();
}
String hostAddress = host;
if (StringUtils.isBlank(hostAddress) && basicServiceConfig != null) {
hostAddress = basicServiceConfig.getHost();
}
if (NetUtils.isInvalidLocalHost(hostAddress)) {
hostAddress = getLocalHostAddress(registryURLs);
}
Map<String, String> map = new HashMap<String, String>();
map.put(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_SERVICE);
map.put(URLParamType.refreshTimestamp.getName(), String.valueOf(System.currentTimeMillis()));
collectConfigParams(map, protocolConfig, basicServiceConfig, extConfig, this);
collectMethodConfigParams(map, this.getMethods());
URL serviceUrl = new URL(protocolName, hostAddress, port, interfaceClass.getName(), map);
if (serviceExists(serviceUrl)) {
LoggerUtil.warn(String.format("%s configService is malformed, for same service (%s) already exists ", interfaceClass.getName(), serviceUrl.getIdentity()));
throw new MotanFrameworkException(String.format("%s configService is malformed, for same service (%s) already exists ", interfaceClass.getName(), serviceUrl.getIdentity()), MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
List<URL> urls = new ArrayList<URL>();
// injvm 协议只支持注册到本地,其他协议可以注册到local、remote
if (MotanConstants.PROTOCOL_INJVM.equals(protocolConfig.getId())) {
URL localRegistryUrl = null;
for (URL ru : registryURLs) {
if (MotanConstants.REGISTRY_PROTOCOL_LOCAL.equals(ru.getProtocol())) {
localRegistryUrl = ru.createCopy();
break;
}
}
if (localRegistryUrl == null) {
localRegistryUrl = new URL(MotanConstants.REGISTRY_PROTOCOL_LOCAL, hostAddress, MotanConstants.DEFAULT_INT_VALUE, RegistryService.class.getName());
}
urls.add(localRegistryUrl);
} else {
for (URL ru : registryURLs) {
urls.add(ru.createCopy());
}
}
for (URL u : urls) {
u.addParameter(URLParamType.embed.getName(), StringTools.urlEncode(serviceUrl.toFullStr()));
registereUrls.add(u.createCopy());
}
ConfigHandler configHandler = ExtensionLoader.getExtensionLoader(ConfigHandler.class).getExtension(MotanConstants.DEFAULT_VALUE);
exporters.add(configHandler.export(interfaceClass, ref, urls));
initLocalAppInfo(serviceUrl);
}
Aggregations