Search in sources :

Example 6 with MethodConfig

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

the class DubboProviderBootstrap method copyMethods.

private void copyMethods(ProviderConfig<T> providerConfig, ServiceConfig<T> serviceConfig) {
    Map<String, MethodConfig> methodConfigs = providerConfig.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());
            dubboMethodConfigs.add(dubboMethodConfig);
        }
        serviceConfig.setMethods(dubboMethodConfigs);
    }
}
Also used : MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 7 with MethodConfig

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

the class DubooServerTest method testOneWay.

@Test
public // 单向调用
void testOneWay() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2);
    // 发布一个服务,每个请求要执行1秒
    ApplicationConfig serverApplacation = new ApplicationConfig();
    serverApplacation.setAppName("server");
    providerConfig = new ProviderConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setRef(new DemoServiceImpl()).setServer(serverConfig).setBootstrap("dubbo").setRegister(false).setApplication(serverApplacation);
    providerConfig.export();
    ApplicationConfig clientApplication = new ApplicationConfig();
    clientApplication.setAppName("client");
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    MethodConfig methodConfig = new MethodConfig();
    methodConfig.setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY);
    methodConfig.setName("sayHello");
    methodConfigs.add(methodConfig);
    consumerConfig = new ConsumerConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setDirectUrl("dubbo://127.0.0.1:20880").setTimeout(30000).setRegister(false).setProtocol("dubbo").setBootstrap("dubbo").setApplication(clientApplication).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setMethods(methodConfigs);
    final DemoService demoService = consumerConfig.refer();
    String tmp = demoService.sayHello("xxx");
    Assert.assertEquals(null, tmp);
}
Also used : MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ArrayList(java.util.ArrayList) DemoService(com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) DemoServiceImpl(com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl) Test(org.junit.Test)

Example 8 with MethodConfig

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

the class SofaRegistryHelper method convertProviderToUrls.

/**
 * Convert provider to url.
 *
 * @param providerConfig the ProviderConfig
 * @return the url list
 */
public static String convertProviderToUrls(ProviderConfig providerConfig, ServerConfig server) {
    StringBuilder sb = new StringBuilder(200);
    String appName = providerConfig.getAppName();
    // 虚拟ip
    String host = server.getVirtualHost();
    if (host == null) {
        host = server.getHost();
        if (NetUtils.isLocalHost(host) || NetUtils.isAnyHost(host)) {
            host = SystemInfo.getLocalHost();
        }
    } else {
        if (LOGGER.isWarnEnabled(appName)) {
            LOGGER.warnWithApp(appName, "Virtual host is specified, host will be change from {} to {} when register", server.getHost(), host);
        }
    }
    // 虚拟port
    Integer port = server.getVirtualPort();
    if (port == null) {
        port = server.getPort();
    } else {
        if (LOGGER.isWarnEnabled(appName)) {
            LOGGER.warnWithApp(appName, "Virtual port is specified, host will be change from {} to {} when register", server.getPort(), port);
        }
    }
    String protocol = server.getProtocol();
    sb.append(host).append(":").append(port).append(server.getContextPath());
    // .append(providerConfig.getInterfaceId())
    sb.append("?").append(ATTR_RPC_VERSION).append("=").append(Version.RPC_VERSION);
    sb.append(getKeyPairs(ATTR_SERIALIZATION, providerConfig.getSerialization()));
    sb.append(getKeyPairs(ATTR_WEIGHT, providerConfig.getWeight()));
    if (providerConfig.getTimeout() > 0) {
        sb.append(getKeyPairs(ATTR_TIMEOUT, providerConfig.getTimeout()));
    }
    sb.append(getKeyPairs(ATTR_APP_NAME, appName));
    // 兼容老系统,代码是否剥离?
    if (PROTOCOL_TYPE_BOLT.equals(protocol)) {
        // p=1
        sb.append(getKeyPairs(RPC_REMOTING_PROTOCOL, RemotingConstants.PROTOCOL_BOLT));
    } else if (PROTOCOL_TYPE_TR.equals(protocol)) {
        // p=13
        sb.append(getKeyPairs(RPC_REMOTING_PROTOCOL, RemotingConstants.PROTOCOL_TR));
    }
    // v=4.0
    sb.append(getKeyPairs(RPC_SERVICE_VERSION, SOFA4_RPC_SERVICE_VERSION));
    // _SERIALIZETYPE=xx
    sb.append(getKeyPairs(SERIALIZE_TYPE_KEY, providerConfig.getSerialization()));
    // _WEIGHT=100
    sb.append(getKeyPairs(WEIGHT_KEY, providerConfig.getWeight()));
    if (providerConfig.getTimeout() > 0) {
        // _TIMEOUT=3000
        sb.append(getKeyPairs(TIMEOUT, providerConfig.getTimeout()));
    }
    sb.append(getKeyPairs(APP_NAME, appName));
    if (StringUtils.isNotBlank(SystemInfo.getHostMachine())) {
        sb.append(getKeyPairs(HOST_MACHINE_KEY, SystemInfo.getHostMachine()));
    }
    Map<String, MethodConfig> methodConfigs = providerConfig.getMethods();
    if (CommonUtils.isNotEmpty(methodConfigs)) {
        for (Map.Entry<String, MethodConfig> entry : methodConfigs.entrySet()) {
            String methodName = entry.getKey();
            MethodConfig methodConfig = entry.getValue();
            sb.append(getKeyPairs("." + methodName + "." + ATTR_TIMEOUT, methodConfig.getTimeout()));
            // 方法级配置,只能放timeout
            String key = "[" + methodName + "]";
            String value = "[" + KEY_TIMEOUT + "#" + methodConfig.getTimeout() + "]";
            sb.append(getKeyPairs(key, value));
        }
    }
    sb.append(convertMap2Pair(providerConfig.getParameters()));
    addCommonAttrs(sb);
    return sb.toString();
}
Also used : MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with MethodConfig

use of com.alipay.sofa.rpc.config.MethodConfig 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 10 with MethodConfig

use of com.alipay.sofa.rpc.config.MethodConfig 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)

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