Search in sources :

Example 21 with MethodConfig

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

the class SofaRegistryHelperTest method convertProviderToUrls.

@Test
public void convertProviderToUrls() throws Exception {
    ServerConfig serverConfig = new ServerConfig().setHost("0.0.0.0").setPort(22000).setProtocol("bolt");
    ServerConfig serverConfig2 = new ServerConfig().setHost("127.0.0.1").setPort(12200).setProtocol("tr");
    ServerConfig serverConfig3 = new ServerConfig().setHost("192.1.1.1").setPort(8080).setProtocol("xfire");
    ProviderConfig<?> providerConfig = new ProviderConfig();
    providerConfig.setInterfaceId("com.alipay.sofa.rpc.test.TestService").setUniqueId("qqqq").setApplication(new ApplicationConfig().setAppName("xxxx")).setTimeout(4444).setWeight(250).setServer(Arrays.asList(serverConfig, serverConfig2));
    MethodConfig methodConfig = new MethodConfig().setName("echo").setTimeout(3333);
    MethodConfig methodConfig2 = new MethodConfig().setName("xx").setTimeout(2222);
    providerConfig.setMethods(Arrays.asList(methodConfig, methodConfig2));
    String s1 = SofaRegistryHelper.convertProviderToUrls(providerConfig, serverConfig);
    Assert.assertNotNull(s1);
    ProviderInfo providerInfo = SofaRegistryHelper.parseProviderInfo(s1);
    Assert.assertEquals(SystemInfo.getLocalHost(), providerInfo.getHost());
    Assert.assertEquals(serverConfig.getPort(), providerInfo.getPort());
    Assert.assertEquals(providerConfig.getAppName(), providerInfo.getAttr(ProviderInfoAttrs.ATTR_APP_NAME));
    Assert.assertEquals(providerConfig.getTimeout(), providerInfo.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT));
    String s2 = SofaRegistryHelper.convertProviderToUrls(providerConfig, serverConfig2);
    Assert.assertNotNull(s2);
    ProviderInfo providerInfo2 = SofaRegistryHelper.parseProviderInfo(s2);
    Assert.assertEquals(SystemInfo.getLocalHost(), providerInfo.getHost());
    Assert.assertEquals(serverConfig2.getPort(), providerInfo2.getPort());
    Assert.assertEquals(providerConfig.getAppName(), providerInfo2.getAttr(ProviderInfoAttrs.ATTR_APP_NAME));
    Assert.assertEquals(providerConfig.getTimeout(), providerInfo2.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT));
    String s3 = SofaRegistryHelper.convertProviderToUrls(providerConfig, serverConfig3);
    Assert.assertNotNull(s3);
    ProviderInfo providerInfo3 = SofaRegistryHelper.parseProviderInfo(s3);
    Assert.assertEquals(serverConfig3.getHost(), providerInfo3.getHost());
    Assert.assertEquals(serverConfig3.getPort(), providerInfo3.getPort());
    Assert.assertEquals(providerConfig.getAppName(), providerInfo3.getAttr(ProviderInfoAttrs.ATTR_APP_NAME));
    Assert.assertEquals(providerConfig.getTimeout(), providerInfo3.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT));
}
Also used : MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Test(org.junit.Test)

Example 22 with MethodConfig

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

the class CallbackMethodClientMain method main.

public static void main(String[] args) throws InterruptedException {
    ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("future-server");
    MethodConfig methodConfig = new MethodConfig();
    methodConfig.setName("sayHello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setOnReturn(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            LOGGER.info("Method get result: {}", appResponse);
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
            LOGGER.info("Method get app exception: {}", throwable);
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
            LOGGER.info("Method get sofa exception: {}", sofaException);
        }
    });
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setApplication(applicationConfig).setInterfaceId(HelloService.class.getName()).setTimeout(5000).setMethods(Collections.singletonList(methodConfig)).setDirectUrl("bolt://127.0.0.1:22222?appName=future-server");
    HelloService helloService = consumerConfig.refer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    try {
        for (int i = 0; i < 100; i++) {
            try {
                String s = helloService.sayHello("xxx", 22);
                LOGGER.warn("{}", s);
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            }
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
            }
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    }
    synchronized (CallbackMethodClientMain.class) {
        while (true) {
            CallbackMethodClientMain.class.wait();
        }
    }
}
Also used : SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) HelloService(com.alipay.sofa.rpc.test.HelloService) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig)

Example 23 with MethodConfig

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

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 24 with MethodConfig

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

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)

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