Java Code Examples for com.alibaba.dubbo.rpc.Invoker#getUrl()

The following examples show how to use com.alibaba.dubbo.rpc.Invoker#getUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransactionLoadBalance.java    From ByteJTA with GNU Lesser General Public License v3.0 6 votes vote down vote up
public <T> Invoker<T> selectSpecificInvoker(List<Invoker<T>> invokers, URL url, Invocation invocation, RemoteNode context)
		throws RpcException {
	RemoteAddr remoteAddr = new RemoteAddr();
	remoteAddr.setServerHost(context.getServerHost());
	remoteAddr.setServerPort(context.getServerPort());
	for (int i = 0; invokers != null && i < invokers.size(); i++) {
		Invoker<T> invoker = invokers.get(i);
		URL targetUrl = invoker.getUrl();
		RemoteAddr targetAddr = new RemoteAddr();
		targetAddr.setServerHost(targetUrl.getIp());
		targetAddr.setServerPort(targetUrl.getPort());
		if (targetAddr.equals(remoteAddr)) {
			return invoker;
		} // end-if (targetAddr.equals(remoteAddr))
	}

	throw new RpcException(String.format("Invoker(%s:%s) is not found!", context.getServerHost(), context.getServerPort()));
}
 
Example 2
Source File: DubboRegistry.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
public DubboRegistry(Invoker<RegistryService> registryInvoker, RegistryService registryService) {
    super(registryInvoker.getUrl());
    this.registryInvoker = registryInvoker;
    this.registryService = registryService;
    // Start reconnection timer
    this.reconnectPeriod = registryInvoker.getUrl().getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, RECONNECT_PERIOD_DEFAULT);
    reconnectFuture = reconnectTimer.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            // Check and connect to the registry
            try {
                connect();
            } catch (Throwable t) { // Defensive fault tolerance
                logger.error("Unexpected error occur at reconnect, cause: " + t.getMessage(), t);
            }
        }
    }, reconnectPeriod, reconnectPeriod, TimeUnit.MILLISECONDS);
}
 
Example 3
Source File: ProviderConsumerRegTable.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
public static ProviderInvokerWrapper getProviderWrapper(Invoker invoker) {
    URL providerUrl = invoker.getUrl();
    if (Constants.REGISTRY_PROTOCOL.equals(providerUrl.getProtocol())) {
        providerUrl = URL.valueOf(providerUrl.getParameterAndDecoded(Constants.EXPORT_KEY));
    }
    String serviceUniqueName = providerUrl.getServiceKey();
    Set<ProviderInvokerWrapper> invokers = providerInvokers.get(serviceUniqueName);
    if (invokers == null) {
        return null;
    }

    for (ProviderInvokerWrapper providerWrapper : invokers) {
        Invoker providerInvoker = providerWrapper.getInvoker();
        if (providerInvoker == invoker) {
            return providerWrapper;
        }
    }

    return null;
}
 
Example 4
Source File: DirectLoadBalance.java    From liteflow with Apache License 2.0 6 votes vote down vote up
@Override
public  <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
    String ip = DirectIpHolder.getIp();
    if(StringUtils.isNotBlank(ip)){
        for (Invoker<T> invoker : invokers) {
            URL invokerURL = invoker.getUrl();
            if (invokerURL.getIp().equals(ip)) {
                return invoker;
            }
        }
       throw new RpcException("there is no invoker, ip:" + ip);
    }else {
        Invoker<T> selectedInvoker = randomLoadBalance.select(invokers, url, invocation);
        return selectedInvoker;
    }
}
 
Example 5
Source File: TimeoutFilter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    long start = System.currentTimeMillis();
    Result result = invoker.invoke(invocation);
    long elapsed = System.currentTimeMillis() - start;
    if (invoker.getUrl() != null
            && elapsed > invoker.getUrl().getMethodParameter(invocation.getMethodName(),
                    "timeout", Integer.MAX_VALUE)) {
        if (logger.isWarnEnabled()) {
            logger.warn("invoke time out. method: " + invocation.getMethodName()
                    + "arguments: " + Arrays.toString(invocation.getArguments()) + " , url is "
                    + invoker.getUrl() + ", invoke elapsed " + elapsed + " ms.");
        }
    }
    return result;
}
 
Example 6
Source File: ConsumerSubscribeListener.java    From dubbo-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Override
public void destroyed(Invoker<?> invoker) {
  Class<?> interfaceClass = invoker.getInterface();
  URL url = invoker.getUrl();
  String group = url.getParameter(DubboSpringBootStarterConstants.GROUP);
  String version = url.getParameter(DubboSpringBootStarterConstants.VERSION);
  ClassIdBean classIdBean = new ClassIdBean(interfaceClass, group, version);
  SUBSCRIBEDINTERFACES_SET.remove(classIdBean);
}
 
Example 7
Source File: ExecuteLimitFilter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    URL url = invoker.getUrl();
    String methodName = invocation.getMethodName();
    int max = url.getMethodParameter(methodName, Constants.EXECUTES_KEY, 0);
    if (max > 0) {
        RpcStatus count = RpcStatus.getStatus(url, invocation.getMethodName());
        if (count.getActive() >= max) {
            throw new RpcException("Failed to invoke method " + invocation.getMethodName() + " in provider " + url + ", cause: The service using threads greater than <dubbo:service executes=\"" + max + "\" /> limited.");
        }
    }
    long begin = System.currentTimeMillis();
    boolean isException = false;
    RpcStatus.beginCount(url, methodName);
    try {
        Result result = invoker.invoke(invocation);
        return result;
    } catch (Throwable t) {
        isException = true;
        if(t instanceof RuntimeException) {
            throw (RuntimeException) t;
        }
        else {
            throw new RpcException("unexpected exception when ExecuteLimitFilter", t);
        }
    }
    finally {
        RpcStatus.endCount(url, methodName, System.currentTimeMillis() - begin, isException);
    }
}
 
Example 8
Source File: RegistryProtocol.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * 通过invoker的url 获取 providerUrl的地址
 * @param origininvoker
 * @return
 */
private URL getProviderUrl(final Invoker<?> origininvoker){
    String export = origininvoker.getUrl().getParameterAndDecoded(Constants.EXPORT_KEY);
    if (export == null || export.length() == 0) {
        throw new IllegalArgumentException("The registry export url is null! registry: " + origininvoker.getUrl());
    }
    
    URL providerUrl = URL.valueOf(export);
    return providerUrl;
}
 
Example 9
Source File: TimeoutFilter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    long start = System.currentTimeMillis();
    Result result = invoker.invoke(invocation);
    long elapsed = System.currentTimeMillis() - start;
    if (invoker.getUrl() != null
            && elapsed > invoker.getUrl().getMethodParameter(invocation.getMethodName(),
                    "timeout", Integer.MAX_VALUE)) {
        if (logger.isWarnEnabled()) {
            logger.warn("invoke time out. method: " + invocation.getMethodName()
                    + "arguments: " + Arrays.toString(invocation.getArguments()) + " , url is "
                    + invoker.getUrl() + ", invoke elapsed " + elapsed + " ms.");
        }
    }
    return result;
}
 
Example 10
Source File: RegistryProtocol.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * 通过invoker的url 获取 providerUrl的地址
 * @param origininvoker
 * @return
 */
private URL getProviderUrl(final Invoker<?> origininvoker){
    String export = origininvoker.getUrl().getParameterAndDecoded(Constants.EXPORT_KEY);
    if (export == null || export.length() == 0) {
        throw new IllegalArgumentException("The registry export url is null! registry: " + origininvoker.getUrl());
    }
    
    URL providerUrl = URL.valueOf(export);
    return providerUrl;
}
 
Example 11
Source File: DubboProtocol.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
    URL url = invoker.getUrl();
    
    // export service.
    String key = serviceKey(url);
    DubboExporter<T> exporter = new DubboExporter<T>(invoker, key, exporterMap);
    exporterMap.put(key, exporter);
    
    //export an stub service for dispaching event
    Boolean isStubSupportEvent = url.getParameter(Constants.STUB_EVENT_KEY,Constants.DEFAULT_STUB_EVENT);
    Boolean isCallbackservice = url.getParameter(Constants.IS_CALLBACK_SERVICE, false);
    if (isStubSupportEvent && !isCallbackservice){
        String stubServiceMethods = url.getParameter(Constants.STUB_EVENT_METHODS_KEY);
        if (stubServiceMethods == null || stubServiceMethods.length() == 0 ){
            if (logger.isWarnEnabled()){
                logger.warn(new IllegalStateException("consumer [" +url.getParameter(Constants.INTERFACE_KEY) +
                        "], has set stubproxy support event ,but no stub methods founded."));
            }
        } else {
            stubServiceMethodsMap.put(url.getServiceKey(), stubServiceMethods);
        }
    }

    openServer(url);

    // modified by lishen
    optimizeSerialization(url);

    return exporter;
}
 
Example 12
Source File: TimeoutFilter.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    long start = System.currentTimeMillis();
    Result result = invoker.invoke(invocation);
    long elapsed = System.currentTimeMillis() - start;
    if (invoker.getUrl() != null
            && elapsed > invoker.getUrl().getMethodParameter(invocation.getMethodName(),
                    "timeout", Integer.MAX_VALUE)) {
        if (logger.isWarnEnabled()) {
            logger.warn("invoke time out. method: " + invocation.getMethodName()
                    + "arguments: " + Arrays.toString(invocation.getArguments()) + " , url is "
                    + invoker.getUrl() + ", invoke elapsed " + elapsed + " ms.");
        }
    }
    return result;
}
 
Example 13
Source File: DubboProtocol.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
    URL url = invoker.getUrl();
    
    // export service.
    String key = serviceKey(url);
    DubboExporter<T> exporter = new DubboExporter<T>(invoker, key, exporterMap);
    exporterMap.put(key, exporter);
    
    //export an stub service for dispaching event
    Boolean isStubSupportEvent = url.getParameter(Constants.STUB_EVENT_KEY,Constants.DEFAULT_STUB_EVENT);
    Boolean isCallbackservice = url.getParameter(Constants.IS_CALLBACK_SERVICE, false);
    if (isStubSupportEvent && !isCallbackservice){
        String stubServiceMethods = url.getParameter(Constants.STUB_EVENT_METHODS_KEY);
        if (stubServiceMethods == null || stubServiceMethods.length() == 0 ){
            if (logger.isWarnEnabled()){
                logger.warn(new IllegalStateException("consumer [" +url.getParameter(Constants.INTERFACE_KEY) +
                        "], has set stubproxy support event ,but no stub methods founded."));
            }
        } else {
            stubServiceMethodsMap.put(url.getServiceKey(), stubServiceMethods);
        }
    }

    openServer(url);

    // modified by lishen
    optimizeSerialization(url);

    return exporter;
}
 
Example 14
Source File: FailfastClusterInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    checkInvokers(invokers, invocation);
    Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
    try {
        return invoker.invoke(invocation);
    } catch (Throwable e) {
        if (e instanceof RpcException && ((RpcException)e).isBiz()) { // biz exception.
            throw (RpcException) e;
        }
        throw new RpcException(e instanceof RpcException ? ((RpcException)e).getCode() : 0, "Failfast invoke providers " + invoker.getUrl() + " " + loadbalance.getClass().getSimpleName() + " select from all providers " + invokers + " for service " + getInterface().getName() + " method " + invocation.getMethodName() + " on consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);
    }
}
 
Example 15
Source File: ProtocolFilterWrapper.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private static <T> Invoker<T> buildInvokerChain(final Invoker<T> invoker, String key, String group) {
    Invoker<T> last = invoker;
    List<Filter> filters = ExtensionLoader.getExtensionLoader(Filter.class).getActivateExtension(invoker.getUrl(), key, group);
    if (filters.size() > 0) {
        for (int i = filters.size() - 1; i >= 0; i --) {
            final Filter filter = filters.get(i);
            final Invoker<T> next = last;
            last = new Invoker<T>() {

                public Class<T> getInterface() {
                    return invoker.getInterface();
                }

                public URL getUrl() {
                    return invoker.getUrl();
                }

                public boolean isAvailable() {
                    return invoker.isAvailable();
                }

                public Result invoke(Invocation invocation) throws RpcException {
                    return filter.invoke(next, invocation);
                }

                public void destroy() {
                    invoker.destroy();
                }

                @Override
                public String toString() {
                    return invoker.toString();
                }
            };
        }
    }
    return last;
}
 
Example 16
Source File: FutureFilter.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
private void fireThrowCallback(final Invoker<?> invoker, final Invocation invocation, final Throwable exception) {
    final Method onthrowMethod = (Method)StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_METHOD_KEY));
    final Object onthrowInst = StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_INSTANCE_KEY));

    //没有设置onthrow callback.
    if (onthrowMethod == null  &&  onthrowInst == null ){
        return ;
    }
    if (onthrowMethod == null  ||  onthrowInst == null ){
        throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() +" has a onthrow callback config , but no such "+(onthrowMethod == null ? "method" : "instance")+" found. url:"+invoker.getUrl());
    }
    if (! onthrowMethod.isAccessible()) {
        onthrowMethod.setAccessible(true);
    }
    Class<?>[] rParaTypes = onthrowMethod.getParameterTypes() ;
    if (rParaTypes[0].isAssignableFrom(exception.getClass())){
        try {
            Object[] args = invocation.getArguments();
            Object[] params;
            
            if (rParaTypes.length >1 ) {
                if (rParaTypes.length == 2 && rParaTypes[1].isAssignableFrom(Object[].class)){
                    params = new Object[2];
                    params[0] = exception;
                    params[1] = args ;
                }else {
                    params = new Object[args.length + 1];
                    params[0] = exception;
                    System.arraycopy(args, 0, params, 1, args.length);
                }
            } else {
                params = new Object[] { exception };
            }
            onthrowMethod.invoke(onthrowInst,params);
        } catch (Throwable e) {
            logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), e);
        } 
    } else {
        logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), exception);
    }
}
 
Example 17
Source File: RedisProtocol.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
@Override
public <T> Exporter<T> export(final Invoker<T> invoker) throws RpcException {
    throw new UnsupportedOperationException("Unsupported export redis service. url: " + invoker.getUrl());
}
 
Example 18
Source File: FutureFilter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
private void fireThrowCallback(final Invoker<?> invoker, final Invocation invocation, final Throwable exception) {
    final Method onthrowMethod = (Method)StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_METHOD_KEY));
    final Object onthrowInst = StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_INSTANCE_KEY));

    //没有设置onthrow callback.
    if (onthrowMethod == null  &&  onthrowInst == null ){
        return ;
    }
    if (onthrowMethod == null  ||  onthrowInst == null ){
        throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() +" has a onthrow callback config , but no such "+(onthrowMethod == null ? "method" : "instance")+" found. url:"+invoker.getUrl());
    }
    if (onthrowMethod != null && ! onthrowMethod.isAccessible()) {
        onthrowMethod.setAccessible(true);
    }
    Class<?>[] rParaTypes = onthrowMethod.getParameterTypes() ;
    if (rParaTypes[0].isAssignableFrom(exception.getClass())){
        try {
            Object[] args = invocation.getArguments();
            Object[] params;
            
            if (rParaTypes.length >1 ) {
                if (rParaTypes.length == 2 && rParaTypes[1].isAssignableFrom(Object[].class)){
                    params = new Object[2];
                    params[0] = exception;
                    params[1] = args ;
                }else {
                    params = new Object[args.length + 1];
                    params[0] = exception;
                    System.arraycopy(args, 0, params, 1, args.length);
                }
            } else {
                params = new Object[] { exception };
            }
            onthrowMethod.invoke(onthrowInst,params);
        } catch (Throwable e) {
            logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), e);
        } 
    } else {
        logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), exception);
    }
}
 
Example 19
Source File: MemcachedProtocol.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public <T> Exporter<T> export(final Invoker<T> invoker) throws RpcException {
    throw new UnsupportedOperationException("Unsupported export memcached service. url: " + invoker.getUrl());
}
 
Example 20
Source File: FutureFilter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
private void fireThrowCallback(final Invoker<?> invoker, final Invocation invocation, final Throwable exception) {
    final Method onthrowMethod = (Method)StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_METHOD_KEY));
    final Object onthrowInst = StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_INSTANCE_KEY));

    //没有设置onthrow callback.
    if (onthrowMethod == null  &&  onthrowInst == null ){
        return ;
    }
    if (onthrowMethod == null  ||  onthrowInst == null ){
        throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() +" has a onthrow callback config , but no such "+(onthrowMethod == null ? "method" : "instance")+" found. url:"+invoker.getUrl());
    }
    if (onthrowMethod != null && ! onthrowMethod.isAccessible()) {
        onthrowMethod.setAccessible(true);
    }
    Class<?>[] rParaTypes = onthrowMethod.getParameterTypes() ;
    if (rParaTypes[0].isAssignableFrom(exception.getClass())){
        try {
            Object[] args = invocation.getArguments();
            Object[] params;
            
            if (rParaTypes.length >1 ) {
                if (rParaTypes.length == 2 && rParaTypes[1].isAssignableFrom(Object[].class)){
                    params = new Object[2];
                    params[0] = exception;
                    params[1] = args ;
                }else {
                    params = new Object[args.length + 1];
                    params[0] = exception;
                    System.arraycopy(args, 0, params, 1, args.length);
                }
            } else {
                params = new Object[] { exception };
            }
            onthrowMethod.invoke(onthrowInst,params);
        } catch (Throwable e) {
            logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), e);
        } 
    } else {
        logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), exception);
    }
}