Search in sources :

Example 11 with SofaBootRpcRuntimeException

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

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

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

the class ServerConfigContainerTest method testCustomServerConfig.

@Test
public void testCustomServerConfig() {
    final ServerConfig serverConfig = new ServerConfig();
    serverConfig.setPort(123);
    final String protocol = "xxx";
    serverConfigContainer.registerCustomServerConfig(protocol, serverConfig);
    ServerConfig serverConfig2 = serverConfigContainer.getServerConfig(protocol);
    Assert.assertEquals(123, serverConfig2.getPort());
    Assert.assertEquals(serverConfig.getPort(), serverConfig2.getPort());
    boolean result = false;
    serverConfigContainer.unRegisterCustomServerConfig(protocol);
    try {
        serverConfigContainer.getServerConfig(protocol);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof SofaBootRpcRuntimeException);
        result = true;
    }
    Assert.assertTrue(result);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) ActivelyDestroyTest(com.alipay.sofa.rpc.boot.test.ActivelyDestroyTest) Test(org.junit.Test)

Example 13 with SofaBootRpcRuntimeException

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

the class RpcBindingConverter method convertServiceAnnotation.

/**
 * convert props to RpcBindingParam
 *
 * @param bindingParam
 * @param sofaServiceAnnotation
 * @param sofaServiceBindingAnnotation
 * @param bindingConverterContext
 */
protected void convertServiceAnnotation(RpcBindingParam bindingParam, SofaService sofaServiceAnnotation, SofaServiceBinding sofaServiceBindingAnnotation, BindingConverterContext bindingConverterContext) {
    if (sofaServiceBindingAnnotation.timeout() != 0) {
        bindingParam.setTimeout(sofaServiceBindingAnnotation.timeout());
    }
    // TODO need a magic number
    if (sofaServiceBindingAnnotation.weight() != 0) {
        bindingParam.setWeight(sofaServiceBindingAnnotation.weight());
    }
    if (sofaServiceBindingAnnotation.warmUpTime() != 0) {
        bindingParam.setWarmUpTime(sofaServiceBindingAnnotation.warmUpTime());
    }
    if (sofaServiceBindingAnnotation.warmUpWeight() != 0) {
        bindingParam.setWarmUpWeight(sofaServiceBindingAnnotation.warmUpWeight());
    }
    if (StringUtils.hasText(sofaServiceBindingAnnotation.serializeType())) {
        bindingParam.setSerialization(sofaServiceBindingAnnotation.serializeType());
    }
    ApplicationContext applicationContext = bindingConverterContext.getApplicationContext();
    List<Filter> filters = new ArrayList<Filter>(RpcFilterContainer.getInstance().getFilters(applicationContext));
    String[] filterNames = sofaServiceBindingAnnotation.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);
    }
    String threadPool = sofaServiceBindingAnnotation.userThreadPool();
    if (StringUtils.hasText(threadPool)) {
        UserThreadPool threadPoolObj = (UserThreadPool) applicationContext.getBean(threadPool);
        String interfaceName = sofaServiceAnnotation.interfaceType().getCanonicalName();
        String uniqId = sofaServiceAnnotation.uniqueId();
        String uniqueName = interfaceName + ":1.0" + (StringUtils.isEmpty(uniqId) ? "" : ":" + uniqId);
        UserThreadPoolManager.registerUserThread(uniqueName, threadPoolObj);
    }
    String registryAlias = sofaServiceBindingAnnotation.registry();
    if (StringUtils.hasText(registryAlias)) {
        String[] registrys = registryAlias.split(",");
        bindingParam.setRegistrys(Arrays.asList(registrys));
    }
    SofaParameter[] parameters = sofaServiceBindingAnnotation.parameters();
    if (parameters.length > 0) {
        bindingParam.setParameters(parseSofaParameters(parameters));
    }
    SofaMethod[] sofaMethods = sofaServiceBindingAnnotation.methodInfos();
    if (sofaMethods.length > 0) {
        bindingParam.setMethodInfos(parseSofaMethods(sofaMethods));
    }
}
Also used : SofaMethod(com.alipay.sofa.runtime.api.annotation.SofaMethod) ArrayList(java.util.ArrayList) UserThreadPool(com.alipay.sofa.rpc.server.UserThreadPool) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) ApplicationContext(org.springframework.context.ApplicationContext) ExcludeFilter(com.alipay.sofa.rpc.filter.ExcludeFilter) Filter(com.alipay.sofa.rpc.filter.Filter) SofaParameter(com.alipay.sofa.runtime.api.annotation.SofaParameter)

Example 14 with SofaBootRpcRuntimeException

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

the class RpcBindingConverter method parseFilter.

protected void parseFilter(Element element, RpcBindingParam param, BindingConverterContext bindingConverterContext) {
    List<Filter> filters = new ArrayList<Filter>(RpcFilterContainer.getInstance().getFilters(bindingConverterContext.getApplicationContext()));
    if (element != null) {
        List<String> filterNames = new ArrayList<String>();
        String filterStrs = element.getAttribute(RpcBindingXmlConstants.TAG_FILTER);
        if (StringUtils.hasText(filterStrs)) {
            String[] subFilter = filterStrs.split(FILTER_SEPERATOR_SYMBOL);
            for (String subfilterName : subFilter) {
                if (StringUtils.hasText(subfilterName)) {
                    if (subfilterName.charAt(0) == EXCLUDE_FILTER_BEGIN_SYMBOL) {
                        String realFilterName = subfilterName.substring(1);
                        if (StringUtils.hasText(realFilterName)) {
                            filters.add(new ExcludeFilter(realFilterName));
                        }
                    } else {
                        filterNames.add(subfilterName);
                    }
                }
            }
        }
        if (!CollectionUtils.isEmpty(filterNames)) {
            for (String filterName : filterNames) {
                Object filter = bindingConverterContext.getApplicationContext().getBean(filterName);
                if (filter instanceof Filter) {
                    filters.add((Filter) filter);
                } else {
                    throw new SofaBootRpcRuntimeException("filter name[" + filterName + "] is not ref a Filter.");
                }
            }
        }
    }
    param.setFilters(filters);
}
Also used : ExcludeFilter(com.alipay.sofa.rpc.filter.ExcludeFilter) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) ExcludeFilter(com.alipay.sofa.rpc.filter.ExcludeFilter) Filter(com.alipay.sofa.rpc.filter.Filter) ArrayList(java.util.ArrayList)

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