Search in sources :

Example 1 with ConsumerConfig

use of com.alipay.sofa.rpc.config.ConsumerConfig in project Sentinel by alibaba.

the class AbstractSofaRpcFilterTest method testNeedToLoadProviderAndConsumer.

@Test
public void testNeedToLoadProviderAndConsumer() {
    SentinelSofaRpcProviderFilter providerFilter = new SentinelSofaRpcProviderFilter();
    ProviderConfig providerConfig = new ProviderConfig();
    providerConfig.setInterfaceId(Serializer.class.getName());
    providerConfig.setId("AAA");
    FilterInvoker providerInvoker = new FilterInvoker(null, null, providerConfig);
    assertTrue(providerFilter.needToLoad(providerInvoker));
    SentinelSofaRpcConsumerFilter consumerFilter = new SentinelSofaRpcConsumerFilter();
    ConsumerConfig consumerConfig = new ConsumerConfig();
    consumerConfig.setInterfaceId(Serializer.class.getName());
    consumerConfig.setId("BBB");
    FilterInvoker consumerInvoker = new FilterInvoker(null, null, consumerConfig);
    assertTrue(consumerFilter.needToLoad(consumerInvoker));
    providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
    assertFalse(providerFilter.needToLoad(providerInvoker));
    assertTrue(consumerFilter.needToLoad(consumerInvoker));
    providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "");
    assertTrue(providerFilter.needToLoad(providerInvoker));
    RpcConfigs.putValue(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
    assertFalse(providerFilter.needToLoad(providerInvoker));
    assertFalse(consumerFilter.needToLoad(consumerInvoker));
}
Also used : FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Serializer(com.alipay.sofa.rpc.codec.Serializer) Test(org.junit.Test)

Example 2 with ConsumerConfig

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

the class RpcConsumerConfig method consumerConfig.

private <T> ConsumerConfig<T> consumerConfig(String graph, String interfaceId) {
    String serviceId;
    if (graph != null) {
        serviceId = interfaceId + ":" + graph;
    } else {
        serviceId = interfaceId;
    }
    @SuppressWarnings("unchecked") ConsumerConfig<T> consumerConfig = (ConsumerConfig<T>) this.configs.get(serviceId);
    if (consumerConfig != null) {
        return consumerConfig;
    }
    assert consumerConfig == null;
    consumerConfig = new ConsumerConfig<>();
    HugeConfig conf = this.conf;
    String protocol = conf.get(RpcOptions.RPC_PROTOCOL);
    int timeout = conf.get(RpcOptions.RPC_CLIENT_READ_TIMEOUT) * 1000;
    int connectTimeout = conf.get(RpcOptions.RPC_CLIENT_CONNECT_TIMEOUT) * 1000;
    int reconnectPeriod = conf.get(RpcOptions.RPC_CLIENT_RECONNECT_PERIOD) * 1000;
    int retries = conf.get(RpcOptions.RPC_CLIENT_RETRIES);
    String loadBalancer = conf.get(RpcOptions.RPC_CLIENT_LOAD_BALANCER);
    if (graph != null) {
        consumerConfig.setId(serviceId).setUniqueId(graph);
        // Default is FailoverCluster, set to FanoutCluster to broadcast
        consumerConfig.setCluster("fanout");
    }
    consumerConfig.setInterfaceId(interfaceId).setProtocol(protocol).setDirectUrl(this.remoteUrls).setTimeout(timeout).setConnectTimeout(connectTimeout).setReconnectPeriod(reconnectPeriod).setRetries(retries).setLoadBalancer(loadBalancer);
    this.configs.put(serviceId, consumerConfig);
    return consumerConfig;
}
Also used : ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) HugeConfig(com.baidu.hugegraph.config.HugeConfig)

Example 3 with ConsumerConfig

use of com.alipay.sofa.rpc.config.ConsumerConfig in project incubator-shenyu by apache.

the class SofaProxyServiceTest method testGenericInvoker.

@Test
@SuppressWarnings("all")
public void testGenericInvoker() throws IllegalAccessException {
    ConsumerConfig consumerConfig = mock(ConsumerConfig.class);
    GenericService genericService = mock(GenericService.class);
    when(consumerConfig.refer()).thenReturn(genericService);
    when(consumerConfig.getInterfaceId()).thenReturn(PATH);
    when(genericService.$genericInvoke(METHOD_NAME, LEFT, RIGHT)).thenReturn(null);
    ApplicationConfigCache applicationConfigCache = ApplicationConfigCache.getInstance();
    final Field cacheField = FieldUtils.getDeclaredField(ApplicationConfigCache.class, "cache", true);
    assertNotNull(cacheField);
    final Object cache = cacheField.get(applicationConfigCache);
    assertTrue(cache instanceof LoadingCache);
    ((LoadingCache) cache).put(PATH, consumerConfig);
    SofaProxyService sofaProxyService = new SofaProxyService(new SofaParamResolveServiceImpl());
    sofaProxyService.genericInvoker("", metaData, exchange);
    RpcInvokeContext.getContext().getResponseCallback().onAppResponse("success", null, null);
}
Also used : Field(java.lang.reflect.Field) GenericService(com.alipay.sofa.rpc.api.GenericService) ApplicationConfigCache(org.apache.shenyu.plugin.sofa.cache.ApplicationConfigCache) LoadingCache(com.google.common.cache.LoadingCache) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.jupiter.api.Test)

Example 4 with ConsumerConfig

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

the class FaultToleranceSubscriber method onEvent.

@Override
public void onEvent(Event originEvent) {
    Class eventClass = originEvent.getClass();
    if (eventClass == ClientSyncReceiveEvent.class) {
        if (!FaultToleranceConfigManager.isEnable()) {
            return;
        }
        // 同步结果
        ClientSyncReceiveEvent event = (ClientSyncReceiveEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        ProviderInfo providerInfo = event.getProviderInfo();
        InvocationStat result = InvocationStatFactory.getInvocationStat(consumerConfig, providerInfo);
        if (result != null) {
            result.invoke();
            Throwable t = event.getThrowable();
            if (t != null) {
                result.catchException(t);
            }
        }
    } else if (eventClass == ClientAsyncReceiveEvent.class) {
        if (!FaultToleranceConfigManager.isEnable()) {
            return;
        }
        // 异步结果
        ClientAsyncReceiveEvent event = (ClientAsyncReceiveEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        ProviderInfo providerInfo = event.getProviderInfo();
        InvocationStat result = InvocationStatFactory.getInvocationStat(consumerConfig, providerInfo);
        if (result != null) {
            result.invoke();
            Throwable t = event.getThrowable();
            if (t != null) {
                result.catchException(t);
            }
        }
    } else if (eventClass == ProviderInfoRemoveEvent.class) {
        ProviderInfoRemoveEvent event = (ProviderInfoRemoveEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        ProviderGroup providerGroup = event.getProviderGroup();
        if (!ProviderHelper.isEmpty(providerGroup)) {
            for (ProviderInfo providerInfo : providerGroup.getProviderInfos()) {
                InvocationStatFactory.removeInvocationStat(consumerConfig, providerInfo);
            }
        }
    } else if (eventClass == ProviderInfoUpdateEvent.class) {
        ProviderInfoUpdateEvent event = (ProviderInfoUpdateEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        List<ProviderInfo> add = new ArrayList<ProviderInfo>();
        List<ProviderInfo> remove = new ArrayList<ProviderInfo>();
        ProviderHelper.compareGroup(event.getOldProviderGroup(), event.getNewProviderGroup(), add, remove);
        for (ProviderInfo providerInfo : remove) {
            InvocationStatFactory.removeInvocationStat(consumerConfig, providerInfo);
        }
    } else if (eventClass == ProviderInfoUpdateAllEvent.class) {
        ProviderInfoUpdateAllEvent event = (ProviderInfoUpdateAllEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        List<ProviderInfo> add = new ArrayList<ProviderInfo>();
        List<ProviderInfo> remove = new ArrayList<ProviderInfo>();
        ProviderHelper.compareGroups(event.getOldProviderGroups(), event.getNewProviderGroups(), add, remove);
        for (ProviderInfo providerInfo : remove) {
            InvocationStatFactory.removeInvocationStat(consumerConfig, providerInfo);
        }
    }
}
Also used : InvocationStat(com.alipay.sofa.rpc.client.aft.InvocationStat) ArrayList(java.util.ArrayList) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) List(java.util.List) ArrayList(java.util.ArrayList)

Example 5 with ConsumerConfig

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

the class ConsumerSubTest method testSubLookout.

@Test
public void testSubLookout() {
    Registry registry = new DefaultRegistry();
    if (Lookout.registry() == NoopRegistry.INSTANCE) {
        Lookout.setRegistry(registry);
    }
    LookoutModule lookoutModule = new LookoutModule();
    Assert.assertEquals(true, lookoutModule.needLoad());
    lookoutModule.install();
    ConsumerConfig consumerConfig = new ConsumerConfig();
    consumerConfig.setInterfaceId("a");
    EventBus.post(new ConsumerSubEvent(consumerConfig));
    try {
        TimeUnit.SECONDS.sleep(5);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    RpcLookoutId rpcLookoutId = new RpcLookoutId();
    InfoWrapper result = Lookout.registry().get(rpcLookoutId.fetchConsumerSubId());
    final Object value = result.value();
    Assert.assertTrue(value instanceof ConsumerConfig);
    consumerConfig = (ConsumerConfig) value;
    Assert.assertEquals("a", consumerConfig.getInterfaceId());
}
Also used : ConsumerSubEvent(com.alipay.sofa.rpc.event.ConsumerSubEvent) DefaultRegistry(com.alipay.lookout.core.DefaultRegistry) RpcLookoutId(com.alipay.sofa.rpc.metrics.lookout.RpcLookoutId) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) NoopRegistry(com.alipay.lookout.api.NoopRegistry) Registry(com.alipay.lookout.api.Registry) DefaultRegistry(com.alipay.lookout.core.DefaultRegistry) InfoWrapper(com.alipay.lookout.core.InfoWrapper) 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