use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.
the class MetricsFilterTest method testInvokeMetricsMethodService.
public void testInvokeMetricsMethodService() {
IMetricManager metricManager = MetricManager.getIMetricManager();
metricManager.clear();
MetricsFilter metricsFilter = new MetricsFilter();
Invocation sayNameInvocation = new RpcInvocation("sayName", DemoService.class.getName(), "", new Class<?>[0], new Object[0]);
Invocation echoInvocation = new RpcInvocation("echo", DemoService.class.getName(), "", new Class<?>[] { Integer.class }, new Integer[] { 1 });
RpcContext.getContext().setRemoteAddress(host, url.getPort()).setLocalAddress(host, NetUtils.getAvailablePort());
RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER_SIDE).addParameter(TIMEOUT_KEY, 300));
AppResponse response = AppResponseBuilder.create().build();
onInvokeReturns(response);
for (int i = 0; i < 50; i++) {
metricsFilter.invoke(serviceInvoker, sayNameInvocation);
metricsFilter.invoke(serviceInvoker, echoInvocation);
try {
metricsFilter.invoke(timeoutInvoker, sayNameInvocation);
} catch (RpcException e) {
// ignore
}
try {
metricsFilter.invoke(timeoutInvoker, echoInvocation);
} catch (RpcException e) {
// ignore
}
}
Protocol protocol = DubboProtocol.getDubboProtocol();
URL metricUrl = URL.valueOf("dubbo://" + url.getHost() + ":" + url.getPort() + "/" + MetricsService.class.getName() + "?" + METRICS_PORT + "=" + port);
Invoker<MetricsService> invoker = protocol.refer(MetricsService.class, metricUrl);
Invocation invocation = new RpcInvocation("getMetricsByGroup", DemoService.class.getName(), "", new Class<?>[] { String.class }, new Object[] { DUBBO_GROUP });
try {
Thread.sleep(15000);
} catch (Exception e) {
// ignore
}
String resStr = invoker.invoke(invocation).getValue().toString();
List<MetricObject> metricObjectList = new Gson().fromJson(resStr, new TypeToken<List<MetricObject>>() {
}.getType());
Map<String, Map<String, Object>> methodMetricMap = new HashMap<>();
for (int i = 0; i < metricObjectList.size(); i++) {
MetricObject object = metricObjectList.get(i);
String service = object.getTags().get("service");
String method = service + "." + object.getTags().get("method");
String metric = object.getMetric().substring(object.getMetric().lastIndexOf(".") + 1);
Map map = methodMetricMap.get(method);
if (map == null) {
map = new HashMap();
methodMetricMap.put(method, map);
}
map.put(metric, object.getValue());
}
Assertions.assertEquals(50.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("success_bucket_count"));
Assertions.assertEquals(50.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("success_bucket_count"));
Assertions.assertEquals(50.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("timeoutError_bucket_count"));
Assertions.assertEquals(50.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("timeoutError_bucket_count"));
Assertions.assertEquals(100.0 / 15, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("qps"));
Assertions.assertEquals(100.0 / 15, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("qps"));
Assertions.assertEquals(50.0 / 100.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("success_rate"));
Assertions.assertEquals(50.0 / 100.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("success_rate"));
invoker.destroy();
}
use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.
the class RedisMetadataReportTest method testStoreConsumer.
private void testStoreConsumer(RedisMetadataReport redisMetadataReport, String version, long moreTime) throws ClassNotFoundException {
String interfaceName = "org.apache.dubbo.metadata.store.redis.RedisMetadata4TstService";
String group = null;
String application = "vic.redis.md";
MetadataIdentifier consumerMetadataIdentifier = storeConsumer(redisMetadataReport, interfaceName, version, group, application);
Jedis jedis = null;
try {
jedis = redisMetadataReport.pool.getResource();
String keyTmp = consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY);
String value = jedis.get(keyTmp);
if (value == null) {
Thread.sleep(moreTime);
value = jedis.get(keyTmp);
}
Assertions.assertEquals(value, "{\"paramConsumerTest\":\"redisCm\"}");
} catch (Throwable e) {
throw new RpcException("Failed to put to redis . cause: " + e.getMessage(), e);
} finally {
if (jedis != null) {
jedis.del(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY));
}
redisMetadataReport.pool.close();
}
}
use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.
the class RedisMetadataReportTest method testStoreProvider.
private void testStoreProvider(RedisMetadataReport redisMetadataReport, String version, long moreTime) throws ClassNotFoundException {
String interfaceName = "org.apache.dubbo.metadata.store.redis.RedisMetadata4TstService";
String group = null;
String application = "vic.redis.md";
MetadataIdentifier providerMetadataIdentifier = storePrivider(redisMetadataReport, interfaceName, version, group, application);
Jedis jedis = null;
try {
jedis = redisMetadataReport.pool.getResource();
String keyTmp = providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY);
String value = jedis.get(keyTmp);
if (value == null) {
Thread.sleep(moreTime);
value = jedis.get(keyTmp);
}
Assertions.assertNotNull(value);
Gson gson = new Gson();
FullServiceDefinition fullServiceDefinition = gson.fromJson(value, FullServiceDefinition.class);
Assertions.assertEquals(fullServiceDefinition.getParameters().get("paramTest"), "redisTest");
} catch (Throwable e) {
throw new RpcException("Failed to put to redis . cause: " + e.getMessage(), e);
} finally {
if (jedis != null) {
jedis.del(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY));
}
redisMetadataReport.pool.close();
}
}
use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.
the class ProviderAuthFilter method invoke.
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
URL url = invoker.getUrl();
boolean shouldAuth = url.getParameter(Constants.SERVICE_AUTH, false);
if (shouldAuth) {
Authenticator authenticator = ExtensionLoader.getExtensionLoader(Authenticator.class).getExtension(url.getParameter(Constants.AUTHENTICATOR, Constants.DEFAULT_AUTHENTICATOR));
try {
authenticator.authenticate(invocation, url);
} catch (Exception e) {
return AsyncRpcResult.newDefaultAsyncResult(e, invocation);
}
}
return invoker.invoke(invocation);
}
use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.
the class DubboProtocol method initClient.
/**
* Create new connection
*
* @param url
*/
private ExchangeClient initClient(URL url) {
// client type setting.
String str = url.getParameter(CLIENT_KEY, url.getParameter(SERVER_KEY, DEFAULT_REMOTING_CLIENT));
url = url.addParameter(CODEC_KEY, DubboCodec.NAME);
// enable heartbeat by default
url = url.addParameterIfAbsent(HEARTBEAT_KEY, String.valueOf(DEFAULT_HEARTBEAT));
// BIO is not allowed since it has severe performance issue.
if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) {
throw new RpcException("Unsupported client type: " + str + "," + " supported client type is " + StringUtils.join(ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions(), " "));
}
ExchangeClient client;
try {
// connection should be lazy
if (url.getParameter(LAZY_CONNECT_KEY, false)) {
client = new LazyConnectExchangeClient(url, requestHandler);
} else {
client = Exchangers.connect(url, requestHandler);
}
} catch (RemotingException e) {
throw new RpcException("Fail to create remoting client for service(" + url + "): " + e.getMessage(), e);
}
return client;
}
Aggregations