Search in sources :

Example 11 with MethodConfig

use of com.alipay.sofa.rpc.config.MethodConfig in project sofa-boot by alipay.

the class ConsumerConfigHelper method getConsumerConfig.

/**
 * 获取 ConsumerConfig
 *
 * @param contract the Contract
 * @param binding  the RpcBinding
 * @return the ConsumerConfig
 */
public ConsumerConfig getConsumerConfig(Contract contract, RpcBinding binding) {
    RpcBindingParam param = binding.getRpcBindingParam();
    String id = binding.getBeanId();
    String interfaceId = contract.getInterfaceType().getName();
    String uniqueId = contract.getUniqueId();
    Integer timeout = param.getTimeout();
    Integer retries = param.getRetries();
    String type = param.getType();
    Integer addressWaitTime = param.getAddressWaitTime();
    Object callbackHandler = param.getCallbackHandler();
    String genericInterface = param.getGenericInterface();
    String loadBalancer = param.getLoadBalancer();
    Boolean lazy = param.getLazy();
    Boolean check = param.getCheck();
    String mockMode = param.getMockMode();
    String serialization = param.getSerialization();
    List<Filter> filters = param.getFilters();
    List<MethodConfig> methodConfigs = convertToMethodConfig(param.getMethodInfos());
    String targetUrl = param.getTargetUrl();
    String referenceLimit = sofaBootRpcProperties.getConsumerRepeatedReferenceLimit();
    ConsumerConfig consumerConfig = new ConsumerConfig();
    if (StringUtils.hasText(appName)) {
        consumerConfig.setApplication(new ApplicationConfig().setAppName(appName));
    }
    if (StringUtils.hasText(id)) {
        consumerConfig.setId(id);
    }
    if (StringUtils.hasText(genericInterface)) {
        consumerConfig.setGeneric(true);
        consumerConfig.setInterfaceId(genericInterface);
    } else if (StringUtils.hasText(interfaceId)) {
        consumerConfig.setInterfaceId(interfaceId);
    }
    if (StringUtils.hasText(uniqueId)) {
        consumerConfig.setUniqueId(uniqueId);
    }
    if (timeout != null) {
        consumerConfig.setTimeout(timeout);
    }
    if (retries != null) {
        consumerConfig.setRetries(retries);
    }
    if (StringUtils.hasText(type)) {
        consumerConfig.setInvokeType(type);
    }
    if (addressWaitTime != null) {
        consumerConfig.setAddressWait(addressWaitTime);
    }
    if (StringUtils.hasText(loadBalancer)) {
        consumerConfig.setLoadBalancer(loadBalancer);
    }
    if (lazy != null) {
        consumerConfig.setLazy(lazy);
    }
    if (check != null) {
        consumerConfig.setCheck(check);
    }
    if (mockMode != null) {
        consumerConfig.setMockMode(mockMode);
    }
    if (callbackHandler != null) {
        if (callbackHandler instanceof SofaResponseCallback) {
            consumerConfig.setOnReturn((SofaResponseCallback) callbackHandler);
        } else {
            throw new SofaBootRpcRuntimeException("callback handler must implement SofaResponseCallback [" + callbackHandler + "]");
        }
    }
    if (!CollectionUtils.isEmpty(filters)) {
        consumerConfig.setFilterRef(filters);
    }
    if (!CollectionUtils.isEmpty(methodConfigs)) {
        consumerConfig.setMethods(methodConfigs);
    }
    if (StringUtils.hasText(targetUrl)) {
        consumerConfig.setDirectUrl(targetUrl);
        consumerConfig.setLazy(true);
        consumerConfig.setSubscribe(false);
        consumerConfig.setRegister(false);
    }
    if (StringUtils.hasText(referenceLimit)) {
        consumerConfig.setRepeatedReferLimit(Integer.valueOf(referenceLimit));
    }
    String protocol = binding.getBindingType().getType();
    consumerConfig.setBootstrap(protocol);
    if (protocol.equals(SofaBootRpcConfigConstants.RPC_PROTOCOL_DUBBO)) {
        consumerConfig.setInJVM(false);
    }
    if (param.getRegistrys() != null && param.getRegistrys().size() > 0) {
        List<String> registrys = param.getRegistrys();
        for (String registryAlias : registrys) {
            RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig(registryAlias);
            consumerConfig.setRegistry(registryConfig);
        }
    } else if (registryConfigContainer.isMeshEnabled(protocol)) {
        RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH);
        consumerConfig.setRegistry(registryConfig);
    } else {
        RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig();
        consumerConfig.setRegistry(registryConfig);
    }
    if (StringUtils.hasText(serialization)) {
        consumerConfig.setSerialization(serialization);
    }
    if (Boolean.TRUE.toString().equals(sofaBootRpcProperties.getHystrixEnable())) {
        consumerConfig.setParameter(HystrixConstants.SOFA_HYSTRIX_ENABLED, Boolean.TRUE.toString());
    }
    // after sofaBootRpcProperties#getHystrixEnable for override global config
    if (param.getParameters() != null) {
        consumerConfig.setParameters(param.getParameters());
    }
    return consumerConfig.setProtocol(protocol);
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) RpcBindingParam(com.alipay.sofa.rpc.boot.runtime.param.RpcBindingParam) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) Filter(com.alipay.sofa.rpc.filter.Filter) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 12 with MethodConfig

use of com.alipay.sofa.rpc.config.MethodConfig in project sofa-boot by alipay.

the class ConsumerConfigHelper method convertToMethodConfig.

private List<MethodConfig> convertToMethodConfig(List<RpcBindingMethodInfo> methodInfos) {
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    if (!CollectionUtils.isEmpty(methodInfos)) {
        for (RpcBindingMethodInfo info : methodInfos) {
            String name = info.getName();
            Integer timeout = info.getTimeout();
            Integer retries = info.getRetries();
            String type = info.getType();
            Object callbackHandler = info.getCallbackHandler();
            MethodConfig methodConfig = new MethodConfig();
            methodConfig.setName(name);
            if (timeout != null) {
                methodConfig.setTimeout(timeout);
            }
            if (retries != null) {
                methodConfig.setRetries(retries);
            }
            if (StringUtils.hasText(type)) {
                methodConfig.setInvokeType(type);
            }
            if (callbackHandler != null) {
                if (callbackHandler instanceof SofaResponseCallback) {
                    methodConfig.setOnReturn((SofaResponseCallback) callbackHandler);
                } else {
                    throw new SofaBootRpcRuntimeException("callback handler must implement SofaResponseCallback [" + callbackHandler + "]");
                }
            }
            methodConfigs.add(methodConfig);
        }
    }
    return methodConfigs;
}
Also used : MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) ArrayList(java.util.ArrayList) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) RpcBindingMethodInfo(com.alipay.sofa.rpc.boot.runtime.binding.RpcBindingMethodInfo)

Example 13 with MethodConfig

use of com.alipay.sofa.rpc.config.MethodConfig in project sofa-boot by alipay.

the class ProviderConfigHelper method getProviderConfig.

/**
 * 获取 ProviderConfig
 *
 * @param contract the Contract
 * @param binding  the RpcBinding
 * @param target   服务实例
 * @return the ProviderConfig
 * @throws SofaBootRpcRuntimeException
 */
public ProviderConfig getProviderConfig(Contract contract, RpcBinding binding, Object target) throws SofaBootRpcRuntimeException {
    RpcBindingParam param = binding.getRpcBindingParam();
    String id = binding.getBeanId();
    String interfaceId = contract.getInterfaceType().getName();
    Object ref = target;
    String uniqueId = contract.getUniqueId();
    Integer timeout = param.getTimeout();
    Integer weight = param.getWeight();
    Integer warmupTime = param.getWarmUpTime();
    Integer warmupWeight = param.getWarmUpWeight();
    UserThreadPool threadPool = param.getUserThreadPool();
    String serialization = param.getSerialization();
    List<Filter> filters = param.getFilters();
    List<MethodConfig> methodConfigs = convertToMethodConfig(param.getMethodInfos());
    ServerConfig serverConfig = serverConfigContainer.getServerConfig(binding.getBindingType().getType());
    ProviderConfig providerConfig = new ProviderConfig();
    if (StringUtils.hasText(appName)) {
        providerConfig.setApplication(new ApplicationConfig().setAppName(appName));
    }
    if (StringUtils.hasText(id)) {
        providerConfig.setId(id);
    }
    if (StringUtils.hasText(interfaceId)) {
        providerConfig.setInterfaceId(interfaceId);
    }
    if (contract.getInterfaceType() != null) {
        providerConfig.setProxyClass(contract.getInterfaceType());
    }
    if (ref != null) {
        providerConfig.setRef(ref);
    }
    if (StringUtils.hasText(uniqueId)) {
        providerConfig.setUniqueId(uniqueId);
    }
    if (timeout != null) {
        providerConfig.setTimeout(timeout);
    }
    if (weight != null) {
        providerConfig.setWeight(weight);
    }
    if (warmupTime != null) {
        providerConfig.setParameter(ProviderInfoAttrs.ATTR_WARMUP_TIME, String.valueOf(warmupTime));
    }
    if (warmupWeight != null) {
        providerConfig.setParameter(ProviderInfoAttrs.ATTR_WARMUP_WEIGHT, String.valueOf(warmupWeight));
    }
    if (!CollectionUtils.isEmpty(filters)) {
        providerConfig.setFilterRef(filters);
    }
    if (!CollectionUtils.isEmpty(methodConfigs)) {
        providerConfig.setMethods(methodConfigs);
    }
    if (threadPool != null) {
        UserThreadPoolManager.registerUserThread(ConfigUniqueNameGenerator.getUniqueName(providerConfig), threadPool);
    }
    providerConfig.setServer(serverConfig);
    String protocol = binding.getBindingType().getType();
    // http protocol use default protocol
    if (!SofaBootRpcConfigConstants.RPC_PROTOCOL_HTTP.equals(protocol)) {
        providerConfig.setBootstrap(protocol);
    }
    if (StringUtils.hasText(serialization)) {
        providerConfig.setSerialization(serialization);
    }
    if (param.getParameters() != null) {
        providerConfig.setParameters(param.getParameters());
    }
    if (param.getRegistrys() != null && param.getRegistrys().size() > 0) {
        List<String> registrys = param.getRegistrys();
        for (String registryAlias : registrys) {
            RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig(registryAlias);
            providerConfig.setRegistry(registryConfig);
        }
    } else if (registryConfigContainer.isMeshEnabled(protocol)) {
        RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH);
        providerConfig.setRegistry(registryConfig);
    } else {
        RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig();
        providerConfig.setRegistry(registryConfig);
    }
    providerConfig.setRegister(false);
    return providerConfig;
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) RpcBindingParam(com.alipay.sofa.rpc.boot.runtime.param.RpcBindingParam) UserThreadPool(com.alipay.sofa.rpc.server.UserThreadPool) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig)

Example 14 with MethodConfig

use of com.alipay.sofa.rpc.config.MethodConfig in project sofa-boot by alipay.

the class ProviderConfigHelper method convertToMethodConfig.

private List<MethodConfig> convertToMethodConfig(List<RpcBindingMethodInfo> methodInfos) {
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    if (!CollectionUtils.isEmpty(methodInfos)) {
        for (RpcBindingMethodInfo info : methodInfos) {
            String name = info.getName();
            Integer timeout = info.getTimeout();
            MethodConfig methodConfig = new MethodConfig();
            methodConfig.setName(name);
            if (timeout != null) {
                methodConfig.setTimeout(timeout);
            }
            methodConfigs.add(methodConfig);
        }
    }
    return methodConfigs;
}
Also used : MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ArrayList(java.util.ArrayList) RpcBindingMethodInfo(com.alipay.sofa.rpc.boot.runtime.binding.RpcBindingMethodInfo)

Example 15 with MethodConfig

use of com.alipay.sofa.rpc.config.MethodConfig in project sofa-rpc by sofastack.

the class DubboConsumerBootstrap method copyMethods.

private void copyMethods(ConsumerConfig<T> consumerConfig, ReferenceConfig<T> referenceConfig) {
    Map<String, MethodConfig> methodConfigs = consumerConfig.getMethods();
    if (CommonUtils.isNotEmpty(methodConfigs)) {
        List<com.alibaba.dubbo.config.MethodConfig> dubboMethodConfigs = new ArrayList<com.alibaba.dubbo.config.MethodConfig>();
        for (Map.Entry<String, MethodConfig> entry : methodConfigs.entrySet()) {
            MethodConfig methodConfig = entry.getValue();
            com.alibaba.dubbo.config.MethodConfig dubboMethodConfig = new com.alibaba.dubbo.config.MethodConfig();
            dubboMethodConfig.setName(methodConfig.getName());
            dubboMethodConfig.setParameters(methodConfig.getParameters());
            dubboMethodConfig.setTimeout(methodConfig.getTimeout());
            dubboMethodConfig.setRetries(methodConfig.getRetries());
            String invokeType = methodConfig.getInvokeType();
            if (invokeType != null) {
                if (RpcConstants.INVOKER_TYPE_ONEWAY.equals(invokeType)) {
                    dubboMethodConfig.setReturn(false);
                }
                if (RpcConstants.INVOKER_TYPE_CALLBACK.equals(invokeType) || RpcConstants.INVOKER_TYPE_FUTURE.equals(invokeType)) {
                    dubboMethodConfig.setAsync(true);
                }
            }
            dubboMethodConfigs.add(dubboMethodConfig);
        }
        referenceConfig.setMethods(dubboMethodConfigs);
    }
}
Also used : MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ArrayList(java.util.ArrayList) Map(java.util.Map)

Aggregations

MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)24 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)14 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)12 ArrayList (java.util.ArrayList)11 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)10 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)7 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)7 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)5 Map (java.util.Map)5 Test (org.junit.Test)5 SofaBootRpcRuntimeException (com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException)4 RpcBindingMethodInfo (com.alipay.sofa.rpc.boot.runtime.binding.RpcBindingMethodInfo)4 RpcBindingParam (com.alipay.sofa.rpc.boot.runtime.param.RpcBindingParam)4 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)4 Filter (com.alipay.sofa.rpc.filter.Filter)4 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)3 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)3 DemoService (com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoService)2 DemoServiceImpl (com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl)2 UserThreadPool (com.alipay.sofa.rpc.server.UserThreadPool)2