Search in sources :

Example 1 with SofaBootRpcRuntimeException

use of com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException in project sofa-boot by sofastack.

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 2 with SofaBootRpcRuntimeException

use of com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException in project sofa-boot by sofastack.

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 3 with SofaBootRpcRuntimeException

use of com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException in project sofa-boot by sofastack.

the class RpcBindingConverter method convertReferenceAnnotation.

/**
 * convert props to RpcBindingParam
 *
 * @param bindingParam
 * @param sofaReferenceBindingAnnotation
 * @param bindingConverterContext
 */
protected void convertReferenceAnnotation(RpcBindingParam bindingParam, SofaReferenceBinding sofaReferenceBindingAnnotation, BindingConverterContext bindingConverterContext) {
    if (sofaReferenceBindingAnnotation.addressWaitTime() != 0) {
        bindingParam.setAddressWaitTime(sofaReferenceBindingAnnotation.addressWaitTime());
    }
    if (StringUtils.hasText(sofaReferenceBindingAnnotation.directUrl())) {
        bindingParam.setTargetUrl(sofaReferenceBindingAnnotation.directUrl());
    }
    if (sofaReferenceBindingAnnotation.timeout() != 0) {
        bindingParam.setTimeout(sofaReferenceBindingAnnotation.timeout());
    }
    if (StringUtils.hasText(sofaReferenceBindingAnnotation.serializeType())) {
        bindingParam.setSerialization(sofaReferenceBindingAnnotation.serializeType());
    }
    if (StringUtils.hasText(sofaReferenceBindingAnnotation.loadBalancer())) {
        bindingParam.setLoadBalancer(sofaReferenceBindingAnnotation.loadBalancer());
    }
    if (StringUtils.hasText(sofaReferenceBindingAnnotation.mockMode())) {
        bindingParam.setMockMode(sofaReferenceBindingAnnotation.mockMode());
    }
    if (StringUtils.hasText(sofaReferenceBindingAnnotation.mockBean())) {
        bindingParam.setMockBean(sofaReferenceBindingAnnotation.mockBean());
    }
    bindingParam.setType(sofaReferenceBindingAnnotation.invokeType());
    ApplicationContext applicationContext = bindingConverterContext.getApplicationContext();
    List<Filter> filters = new ArrayList<Filter>(RpcFilterContainer.getInstance().getFilters(applicationContext));
    String[] filterNames = sofaReferenceBindingAnnotation.filters();
    if (filterNames.length > 0) {
        for (String filterName : filterNames) {
            Object filter = applicationContext.getBean(filterName);
            if (filter instanceof Filter) {
                filters.add((Filter) filter);
            } else {
                throw new SofaBootRpcRuntimeException("filter name[" + filterName + "] is not ref a Filter.");
            }
        }
    }
    if (!CollectionUtils.isEmpty(filters)) {
        bindingParam.setFilters(filters);
    }
    bindingParam.setRetries(sofaReferenceBindingAnnotation.retries());
    String callbackRef = sofaReferenceBindingAnnotation.callbackHandler();
    if (StringUtils.hasText(callbackRef)) {
        bindingParam.setCallbackHandler(applicationContext.getBean(callbackRef));
    }
    bindingParam.setLazy(sofaReferenceBindingAnnotation.lazy());
    String registryAlias = sofaReferenceBindingAnnotation.registry();
    if (StringUtils.hasText(registryAlias)) {
        String[] registrys = registryAlias.split(",");
        bindingParam.setRegistrys(Arrays.asList(registrys));
    }
    SofaParameter[] parameters = sofaReferenceBindingAnnotation.parameters();
    if (parameters.length > 0) {
        bindingParam.setParameters(parseSofaParameters(parameters));
    }
    SofaMethod[] sofaMethods = sofaReferenceBindingAnnotation.methodInfos();
    if (sofaMethods.length > 0) {
        bindingParam.setMethodInfos(parseSofaMethods(sofaMethods));
    }
}
Also used : SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) ApplicationContext(org.springframework.context.ApplicationContext) SofaMethod(com.alipay.sofa.runtime.api.annotation.SofaMethod) ExcludeFilter(com.alipay.sofa.rpc.filter.ExcludeFilter) Filter(com.alipay.sofa.rpc.filter.Filter) SofaParameter(com.alipay.sofa.runtime.api.annotation.SofaParameter) ArrayList(java.util.ArrayList)

Example 4 with SofaBootRpcRuntimeException

use of com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException in project sofa-boot by alipay.

the class RegistryConfigContainer method getRegistryConfig.

/**
 * @param registryAlias
 * @return
 * @throws SofaBootRpcRuntimeException
 */
public RegistryConfig getRegistryConfig(String registryAlias) throws SofaBootRpcRuntimeException {
    RegistryConfig registryConfig;
    String registryProtocol;
    String registryAddress;
    String currentDefaultAlias;
    if (StringUtils.isNotBlank(customDefaultRegistry)) {
        currentDefaultAlias = customDefaultRegistry;
    } else {
        currentDefaultAlias = DEFAULT_REGISTRY;
    }
    if (StringUtils.isEmpty(registryAlias)) {
        registryAlias = currentDefaultAlias;
    }
    if (registryConfigs.get(registryAlias) != null) {
        return registryConfigs.get(registryAlias);
    }
    // just for new address
    if (DEFAULT_REGISTRY.equalsIgnoreCase(registryAlias)) {
        registryAddress = sofaBootRpcProperties.getRegistryAddress();
    } else if (registryAlias.equals(customDefaultRegistry)) {
        registryAddress = customDefaultRegistryAddress;
    } else {
        registryAddress = sofaBootRpcProperties.getRegistries().get(registryAlias);
    }
    // for worst condition.
    if (StringUtils.isBlank(registryAddress)) {
        registryProtocol = SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_LOCAL;
    } else {
        final int endIndex = registryAddress.indexOf("://");
        if (endIndex != -1) {
            registryProtocol = registryAddress.substring(0, endIndex);
        } else {
            registryProtocol = registryAlias;
        }
    }
    if (registryConfigMap.get(registryProtocol) != null) {
        RegistryConfigureProcessor registryConfigureProcessor = registryConfigMap.get(registryProtocol);
        registryConfig = registryConfigureProcessor.buildFromAddress(registryAddress);
        registryConfigs.put(registryAlias, registryConfig);
        // 不再处理以.分隔的.
        final Environment environment = sofaBootRpcProperties.getEnvironment();
        if (environment.containsProperty(SofaOptions.CONFIG_RPC_REGISTER_REGISTRY_IGNORE)) {
            if (Boolean.TRUE.toString().equalsIgnoreCase(environment.getProperty(SofaOptions.CONFIG_RPC_REGISTER_REGISTRY_IGNORE))) {
                registryConfig.setRegister(false);
            }
        }
        return registryConfig;
    } else {
        throw new SofaBootRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_REGISTRY_NOT_SUPPORT, registryAddress));
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) RegistryConfigureProcessor(com.alipay.sofa.rpc.boot.config.RegistryConfigureProcessor) Environment(org.springframework.core.env.Environment)

Example 5 with SofaBootRpcRuntimeException

use of com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException 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)

Aggregations

SofaBootRpcRuntimeException (com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException)14 Filter (com.alipay.sofa.rpc.filter.Filter)8 ArrayList (java.util.ArrayList)8 ExcludeFilter (com.alipay.sofa.rpc.filter.ExcludeFilter)6 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)4 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)4 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)4 SofaMethod (com.alipay.sofa.runtime.api.annotation.SofaMethod)4 SofaParameter (com.alipay.sofa.runtime.api.annotation.SofaParameter)4 ApplicationContext (org.springframework.context.ApplicationContext)4 RegistryConfigureProcessor (com.alipay.sofa.rpc.boot.config.RegistryConfigureProcessor)2 RpcBindingMethodInfo (com.alipay.sofa.rpc.boot.runtime.binding.RpcBindingMethodInfo)2 RpcBindingParam (com.alipay.sofa.rpc.boot.runtime.param.RpcBindingParam)2 ActivelyDestroyTest (com.alipay.sofa.rpc.boot.test.ActivelyDestroyTest)2 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)2 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)2 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)2 UserThreadPool (com.alipay.sofa.rpc.server.UserThreadPool)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Test (org.junit.Test)2