Search in sources :

Example 51 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class ClusterSupport method init.

public void init() {
    long start = System.currentTimeMillis();
    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 = UrlUtils.stringToURLs(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");
        }
        LoggerUtil.info("cluster init cost " + (System.currentTimeMillis() - start) + ", refer size:" + (cluster.getReferers() == null ? 0 : cluster.getReferers().size()) + ", cluster:" + cluster.getUrl().toSimpleString());
        StatsUtil.registryStatisticCallback(this);
        return;
    }
    throw new MotanFrameworkException(String.format("ClusterSupport No service urls for the refer:%s, registries:%s", this.url.getIdentity(), registryUrls), MotanErrorMsgConstant.SERVICE_UNFOUND);
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Registry(com.weibo.api.motan.registry.Registry) URL(com.weibo.api.motan.rpc.URL)

Example 52 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class FailoverHaStrategy method call.

@Override
public Response call(Request request, LoadBalance<T> loadBalance) {
    List<Referer<T>> referers = selectReferers(request, loadBalance);
    if (referers.isEmpty()) {
        throw new MotanServiceException(String.format("FailoverHaStrategy No referers for request:%s, loadbalance:%s", request, loadBalance));
    }
    URL refUrl = referers.get(0).getUrl();
    // 先使用method的配置
    int tryCount = refUrl.getMethodParameter(request.getMethodName(), request.getParamtersDesc(), URLParamType.retries.getName(), URLParamType.retries.getIntValue());
    // 如果有问题,则设置为不重试
    if (tryCount < 0) {
        tryCount = 0;
    }
    for (int i = 0; i <= tryCount; i++) {
        Referer<T> refer = referers.get(i % referers.size());
        try {
            request.setRetries(i);
            return refer.call(request);
        } catch (RuntimeException e) {
            // 对于业务异常,直接抛出
            if (ExceptionUtil.isBizException(e)) {
                throw e;
            } else if (i >= tryCount) {
                throw e;
            }
            LoggerUtil.warn(String.format("FailoverHaStrategy Call false for request:%s error=%s", request, e.getMessage()));
        }
    }
    throw new MotanFrameworkException("FailoverHaStrategy.call should not come here!");
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Referer(com.weibo.api.motan.rpc.Referer) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) URL(com.weibo.api.motan.rpc.URL)

Example 53 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class DefaultRpcProtocolTest method testProtocol.

@Test
public void testProtocol() {
    url = new URL("motan", "localhost", 18080, "com.weibo.api.motan.procotol.example.IHello");
    url.getParameters().put(URLParamType.endpointFactory.getName(), "mockEndpoint");
    try {
        defaultRpcProtocol.export(null, null);
    } catch (Exception e) {
        if (e instanceof MotanFrameworkException) {
            Assert.assertTrue(e.getMessage().contains("url is null"));
        } else {
            Assert.assertTrue(false);
        }
    }
    try {
        defaultRpcProtocol.export(null, url);
    } catch (Exception e) {
        if (e instanceof MotanFrameworkException) {
            Assert.assertTrue(e.getMessage().contains("provider is null"));
        } else {
            Assert.assertTrue(false);
        }
    }
    defaultRpcProtocol.export(new Provider<IHello>() {

        private IHello hello = new Hello();

        @Override
        public Response call(Request request) {
            hello.hello();
            return new DefaultResponse("hello");
        }

        @Override
        public void init() {
        }

        @Override
        public void destroy() {
        }

        @Override
        public boolean isAvailable() {
            return false;
        }

        @Override
        public String desc() {
            return null;
        }

        @Override
        public URL getUrl() {
            return new URL("motan", "localhost", 18080, "com.weibo.api.motan.procotol.example.IHello");
        }

        @Override
        public Class<IHello> getInterface() {
            return IHello.class;
        }

        @Override
        public Method lookupMethod(String methodName, String methodDesc) {
            return null;
        }

        @Override
        public IHello getImpl() {
            return hello;
        }
    }, url);
    Referer<IHello> referer = defaultRpcProtocol.refer(IHello.class, url);
    DefaultRequest request = new DefaultRequest();
    request.setMethodName("hello");
    request.setInterfaceName(IHello.class.getName());
    Response response = referer.call(request);
    System.out.println("client: " + response.getValue());
    defaultRpcProtocol.destroy();
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Method(java.lang.reflect.Method) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IHello(com.weibo.api.motan.protocol.example.IHello) Hello(com.weibo.api.motan.protocol.example.Hello) IHello(com.weibo.api.motan.protocol.example.IHello) Test(org.junit.Test)

Example 54 with MotanFrameworkException

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);
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 55 with MotanFrameworkException

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);
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Aggregations

MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)57 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)11 URL (com.weibo.api.motan.rpc.URL)10 IOException (java.io.IOException)9 Method (java.lang.reflect.Method)8 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)6 Response (com.weibo.api.motan.rpc.Response)5 TransportException (com.weibo.api.motan.transport.TransportException)4 ArrayList (java.util.ArrayList)4 Serialization (com.weibo.api.motan.codec.Serialization)3 ConfigHandler (com.weibo.api.motan.config.handler.ConfigHandler)3 Registry (com.weibo.api.motan.registry.Registry)3 HeartbeatFactory (com.weibo.api.motan.transport.HeartbeatFactory)3 HashMap (java.util.HashMap)3 CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)2 ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)2 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)2 Request (com.weibo.api.motan.rpc.Request)2 DeserializableObject (com.weibo.api.motan.serialize.DeserializableObject)2 ChannelFuture (io.netty.channel.ChannelFuture)2