Search in sources :

Example 66 with ConsumerConfig

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

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

the class RpcBindingAdapter method inBinding.

/**
 * in binding, in binding means reference service
 *
 * @param contract           binding contract
 * @param binding            binding object
 * @param sofaRuntimeContext sofa runtime context
 */
@Override
public Object inBinding(Object contract, RpcBinding binding, SofaRuntimeContext sofaRuntimeContext) {
    ApplicationContext applicationContext = sofaRuntimeContext.getSofaRuntimeManager().getRootApplicationContext();
    ConsumerConfigHelper consumerConfigHelper = applicationContext.getBean(ConsumerConfigHelper.class);
    ConsumerConfigContainer consumerConfigContainer = applicationContext.getBean(ConsumerConfigContainer.class);
    ProcessorContainer processorContainer = applicationContext.getBean(ProcessorContainer.class);
    ConsumerConfig consumerConfig = consumerConfigHelper.getConsumerConfig((Contract) contract, binding);
    processorContainer.processorConsumer(consumerConfig);
    if (MockMode.LOCAL.equalsIgnoreCase(binding.getRpcBindingParam().getMockMode())) {
        consumerConfig.setMockRef(consumerConfigHelper.getMockRef(binding, applicationContext));
    }
    consumerConfigContainer.addConsumerConfig(binding, consumerConfig);
    try {
        Object result = consumerConfig.refer();
        binding.setConsumerConfig(consumerConfig);
        return result;
    } catch (Exception e) {
        throw new ServiceRuntimeException(LogCodes.getLog(LogCodes.ERROR_PROXY_CONSUME_FAIL), e);
    }
}
Also used : ProcessorContainer(com.alipay.sofa.rpc.boot.runtime.adapter.processor.ProcessorContainer) ApplicationContext(org.springframework.context.ApplicationContext) ConsumerConfigHelper(com.alipay.sofa.rpc.boot.runtime.adapter.helper.ConsumerConfigHelper) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ConsumerConfigContainer(com.alipay.sofa.rpc.boot.container.ConsumerConfigContainer) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 68 with ConsumerConfig

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

the class ConsumerMockProcessorTest method testProcessor.

@Test
public void testProcessor() {
    ConsumerConfig consumerConfig = new ConsumerConfig();
    processor.processorConsumer(consumerConfig);
    Assert.assertEquals(MOCK_URL, consumerConfig.getParameter(ConsumerMockProcessor.MOCK_URL));
    Assert.assertEquals(MockMode.REMOTE, consumerConfig.getMockMode());
    processor.setMockUrl("");
    ConsumerConfig consumerConfig2 = new ConsumerConfig();
    processor.processorConsumer(consumerConfig2);
    Assert.assertFalse(StringUtils.hasText(consumerConfig2.getMockMode()));
    Assert.assertFalse(StringUtils.hasText(consumerConfig2.getParameter(ConsumerMockProcessor.MOCK_URL)));
}
Also used : ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Example 69 with ConsumerConfig

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

the class SofaBootRpcAllTest method testLoadBalancerAnnotation.

@Test
public void testLoadBalancerAnnotation() throws NoSuchFieldException, IllegalAccessException {
    Field consumerConfigMapField = ConsumerConfigContainer.class.getDeclaredField("consumerConfigMap");
    consumerConfigMapField.setAccessible(true);
    ConcurrentMap<Binding, ConsumerConfig> consumerConfigMap = (ConcurrentMap<Binding, ConsumerConfig>) consumerConfigMapField.get(consumerConfigContainer);
    boolean found = false;
    for (ConsumerConfig consumerConfig : consumerConfigMap.values()) {
        if ("loadbalancer".equals(consumerConfig.getUniqueId()) && AnnotationService.class.getName().equals(consumerConfig.getInterfaceId())) {
            found = true;
            Assert.assertEquals("roundRobin", consumerConfig.getLoadBalancer());
        }
    }
    Assert.assertTrue("Found roundrobin reference", found);
}
Also used : Binding(com.alipay.sofa.runtime.spi.binding.Binding) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) Field(java.lang.reflect.Field) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 70 with ConsumerConfig

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

the class ConsumerMockProcessorTest method testMockSet.

@Test
public void testMockSet() {
    ConsumerConfig consumerConfig = new ConsumerConfig();
    consumerConfig.setMockMode(MockMode.LOCAL);
    processor.processorConsumer(consumerConfig);
    Assert.assertFalse(StringUtils.hasText(consumerConfig.getParameter(ConsumerMockProcessor.MOCK_URL)));
    Assert.assertEquals(MockMode.LOCAL, consumerConfig.getMockMode());
    consumerConfig = new ConsumerConfig();
    consumerConfig.setMockMode(MockMode.REMOTE);
    consumerConfig.setParameter(ConsumerMockProcessor.MOCK_URL, "another");
    processor.processorConsumer(consumerConfig);
    Assert.assertEquals("another", consumerConfig.getParameter(ConsumerMockProcessor.MOCK_URL));
    Assert.assertEquals(MockMode.REMOTE, consumerConfig.getMockMode());
}
Also used : ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Aggregations

ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)139 Test (org.junit.Test)86 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)68 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)61 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)44 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)40 HelloService (com.alipay.sofa.rpc.test.HelloService)38 CountDownLatch (java.util.concurrent.CountDownLatch)27 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)21 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)21 ArrayList (java.util.ArrayList)19 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)18 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)14 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)12 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)12 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)10 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ConsumerBootstrap (com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap)8 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)8