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));
}
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;
}
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);
}
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);
}
}
}
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());
}
Aggregations