Search in sources :

Example 1 with DynamicConfigManager

use of com.alipay.sofa.rpc.dynamic.DynamicConfigManager in project sofa-rpc by sofastack.

the class AbstractCluster method resolveTimeout.

/**
 * 决定超时时间
 *
 * @param request        请求
 * @param consumerConfig 客户端配置
 * @param providerInfo   服务提供者信息
 * @return 调用超时
 */
private int resolveTimeout(SofaRequest request, ConsumerConfig consumerConfig, ProviderInfo providerInfo) {
    // 动态配置优先
    final String dynamicAlias = consumerConfig.getParameter(DynamicConfigKeys.DYNAMIC_ALIAS);
    if (StringUtils.isNotBlank(dynamicAlias)) {
        String dynamicTimeout = null;
        DynamicConfigManager dynamicConfigManager = DynamicConfigManagerFactory.getDynamicManager(consumerConfig.getAppName(), dynamicAlias);
        if (dynamicConfigManager != null) {
            dynamicTimeout = dynamicConfigManager.getConsumerMethodProperty(request.getInterfaceName(), request.getMethodName(), "timeout");
        }
        if (DynamicHelper.isNotDefault(dynamicTimeout) && StringUtils.isNotBlank(dynamicTimeout)) {
            return Integer.parseInt(dynamicTimeout);
        }
    }
    // 先去调用级别配置
    Integer timeout = request.getTimeout();
    if (timeout == null || timeout <= 0) {
        // 取客户端配置(先方法级别再接口级别)
        timeout = consumerConfig.getMethodTimeout(request.getMethodName());
        if (timeout == null || timeout <= 0) {
            // 再取服务端配置
            timeout = StringUtils.parseInteger(providerInfo.getAttr(ATTR_TIMEOUT));
            if (timeout == null || timeout <= 0) {
                // 取框架默认值
                timeout = getIntValue(CONSUMER_INVOKE_TIMEOUT);
            }
        }
    }
    return timeout;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DynamicConfigManager(com.alipay.sofa.rpc.dynamic.DynamicConfigManager)

Example 2 with DynamicConfigManager

use of com.alipay.sofa.rpc.dynamic.DynamicConfigManager in project sofa-rpc by sofastack.

the class DefaultConsumerBootstrap method refer.

@Override
public T refer() {
    if (proxyIns != null) {
        return proxyIns;
    }
    synchronized (this) {
        if (proxyIns != null) {
            return proxyIns;
        }
        String key = consumerConfig.buildKey();
        String appName = consumerConfig.getAppName();
        // 检查参数
        checkParameters();
        // 提前检查接口类
        if (LOGGER.isInfoEnabled(appName)) {
            LOGGER.infoWithApp(appName, "Refer consumer config : {} with bean id {}", key, consumerConfig.getId());
        }
        // 注意同一interface,同一tags,同一protocol情况
        // 计数器
        AtomicInteger cnt = REFERRED_KEYS.get(key);
        if (cnt == null) {
            // 没有发布过
            cnt = CommonUtils.putToConcurrentMap(REFERRED_KEYS, key, new AtomicInteger(0));
        }
        int c = cnt.incrementAndGet();
        int maxProxyCount = consumerConfig.getRepeatedReferLimit();
        if (maxProxyCount > 0) {
            if (c > maxProxyCount) {
                cnt.decrementAndGet();
                // 超过最大数量,直接抛出异常
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_DUPLICATE_CONSUMER_CONFIG, key, maxProxyCount));
            } else if (c > 1) {
                if (LOGGER.isInfoEnabled(appName)) {
                    LOGGER.infoWithApp(appName, "Duplicate consumer config with key {} has been referred!" + " Maybe it's wrong config, please check it." + " Ignore this if you did that on purpose!", key);
                }
            }
        }
        try {
            // build cluster
            cluster = ClusterFactory.getCluster(this);
            // build listeners
            consumerConfig.setConfigListener(buildConfigListener(this));
            consumerConfig.setProviderInfoListener(buildProviderInfoListener(this));
            // init cluster
            cluster.init();
            // 构造Invoker对象(执行链)
            proxyInvoker = buildClientProxyInvoker(this);
            // 创建代理类
            proxyIns = (T) ProxyFactory.buildProxy(consumerConfig.getProxy(), consumerConfig.getProxyClass(), proxyInvoker);
            // 动态配置
            final String dynamicAlias = consumerConfig.getParameter(DynamicConfigKeys.DYNAMIC_ALIAS);
            if (StringUtils.isNotBlank(dynamicAlias)) {
                final DynamicConfigManager dynamicManager = DynamicConfigManagerFactory.getDynamicManager(consumerConfig.getAppName(), dynamicAlias);
                dynamicManager.initServiceConfiguration(consumerConfig.getInterfaceId());
            }
        } catch (Exception e) {
            if (cluster != null) {
                cluster.destroy();
                cluster = null;
            }
            consumerConfig.setConfigListener(null);
            consumerConfig.setProviderInfoListener(null);
            // 发布失败不计数
            cnt.decrementAndGet();
            if (e instanceof SofaRpcRuntimeException) {
                throw (SofaRpcRuntimeException) e;
            } else {
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_BUILD_CONSUMER_PROXY), e);
            }
        }
        if (consumerConfig.getOnAvailable() != null && cluster != null) {
            // 状态变化通知监听器
            cluster.checkStateChange(false);
        }
        RpcRuntimeContext.cacheConsumerConfig(this);
        return proxyIns;
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) DynamicConfigManager(com.alipay.sofa.rpc.dynamic.DynamicConfigManager) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 3 with DynamicConfigManager

use of com.alipay.sofa.rpc.dynamic.DynamicConfigManager in project sofa-rpc by sofastack.

the class AutoLoadBalancer method doSelect.

@Override
protected ProviderInfo doSelect(SofaRequest request, List<ProviderInfo> providerInfos) {
    // 动态配置优先
    final String dynamicAlias = consumerConfig.getParameter(DynamicConfigKeys.DYNAMIC_ALIAS);
    if (StringUtils.isNotBlank(dynamicAlias)) {
        String dynamicLoadBalancer = null;
        DynamicConfigManager dynamicConfigManager = DynamicConfigManagerFactory.getDynamicManager(consumerConfig.getAppName(), dynamicAlias);
        if (dynamicConfigManager != null) {
            dynamicLoadBalancer = dynamicConfigManager.getConsumerServiceProperty(request.getTargetServiceUniqueName(), LOAD_BALANCER_KEY);
            if (DynamicHelper.isNotDefault(dynamicLoadBalancer) && StringUtils.isNotBlank(dynamicLoadBalancer)) {
                LoadBalancer loadBalancer = LoadBalancerFactory.getLoadBalancer(consumerBootstrap, dynamicLoadBalancer);
                return loadBalancer.select(request, providerInfos);
            }
        }
    }
    LoadBalancer loadBalancer = LoadBalancerFactory.getLoadBalancer(consumerBootstrap, DEFAULT_LOAD_BALANCER);
    return loadBalancer.select(request, providerInfos);
}
Also used : DynamicConfigManager(com.alipay.sofa.rpc.dynamic.DynamicConfigManager) AbstractLoadBalancer(com.alipay.sofa.rpc.client.AbstractLoadBalancer) LoadBalancer(com.alipay.sofa.rpc.client.LoadBalancer)

Aggregations

DynamicConfigManager (com.alipay.sofa.rpc.dynamic.DynamicConfigManager)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AbstractLoadBalancer (com.alipay.sofa.rpc.client.AbstractLoadBalancer)1 LoadBalancer (com.alipay.sofa.rpc.client.LoadBalancer)1 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)1