use of org.apache.dubbo.rpc.Protocol in project dubbo by alibaba.
the class DubboMonitorTest method testMonitorFactory.
@Test
public void testMonitorFactory() throws Exception {
MockMonitorService monitorService = new MockMonitorService();
URL statistics = new URLBuilder(DUBBO_PROTOCOL, "10.20.153.10", 0).addParameter(MonitorService.APPLICATION, "morgan").addParameter(MonitorService.INTERFACE, "MemberService").addParameter(MonitorService.METHOD, "findPerson").addParameter(MonitorService.CONSUMER, "10.20.153.11").addParameter(MonitorService.SUCCESS, 1).addParameter(MonitorService.FAILURE, 0).addParameter(MonitorService.ELAPSED, 3).addParameter(MonitorService.MAX_ELAPSED, 3).addParameter(MonitorService.CONCURRENT, 1).addParameter(MonitorService.MAX_CONCURRENT, 1).build();
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
MonitorFactory monitorFactory = ExtensionLoader.getExtensionLoader(MonitorFactory.class).getAdaptiveExtension();
Exporter<MonitorService> exporter = protocol.export(proxyFactory.getInvoker(monitorService, MonitorService.class, URL.valueOf("dubbo://127.0.0.1:17979/" + MonitorService.class.getName())));
try {
Monitor monitor = null;
long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < 60000) {
monitor = monitorFactory.getMonitor(URL.valueOf("dubbo://127.0.0.1:17979?interval=10"));
if (monitor == null) {
continue;
}
try {
monitor.collect(statistics);
int i = 0;
while (monitorService.getStatistics() == null && i < 200) {
i++;
Thread.sleep(10);
}
URL result = monitorService.getStatistics();
Assertions.assertEquals(1, result.getParameter(MonitorService.SUCCESS, 0));
Assertions.assertEquals(3, result.getParameter(MonitorService.ELAPSED, 0));
} finally {
monitor.destroy();
}
break;
}
Assertions.assertNotNull(monitor);
} finally {
exporter.unexport();
}
}
use of org.apache.dubbo.rpc.Protocol in project dubbo by alibaba.
the class MetricsFilterTest method testInvokeMetricsService.
public void testInvokeMetricsService() {
IMetricManager metricManager = MetricManager.getIMetricManager();
metricManager.clear();
MetricsFilter metricsFilter = new MetricsFilter();
Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), "", new Class<?>[0], new Object[0]);
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++) {
try {
metricsFilter.invoke(serviceInvoker, invocation);
metricsFilter.invoke(timeoutInvoker, invocation);
} 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 = new RpcInvocation("getMetricsByGroup", DemoService.class.getName(), "", new Class<?>[] { String.class }, new Object[] { DUBBO_GROUP });
try {
Thread.sleep(5000);
} 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, Object> metricMap = new HashMap<>();
for (int i = 0; i < metricObjectList.size(); i++) {
MetricObject object = metricObjectList.get(i);
String metric = object.getMetric().substring(object.getMetric().lastIndexOf(".") + 1);
if ((double) object.getValue() > 0.0 && object.getMetricLevel().equals(MetricLevel.MAJOR))
metricMap.put(metric, object.getValue());
}
Assertions.assertEquals(50.0, metricMap.get("success_bucket_count"));
Assertions.assertEquals(50.0, metricMap.get("timeoutError_bucket_count"));
Assertions.assertEquals(100.0, metricMap.get("bucket_count"));
Assertions.assertEquals(100.0 / 5, metricMap.get("qps"));
Assertions.assertEquals(50.0 / 100.0, metricMap.get("success_rate"));
invoker.destroy();
}
use of org.apache.dubbo.rpc.Protocol in project dubbo by alibaba.
the class MetricsFilter method invoke.
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
if (exported.compareAndSet(false, true)) {
this.protocolName = invoker.getUrl().getParameter(METRICS_PROTOCOL) == null ? DEFAULT_PROTOCOL : invoker.getUrl().getParameter(METRICS_PROTOCOL);
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(protocolName);
this.port = invoker.getUrl().getParameter(METRICS_PORT) == null ? protocol.getDefaultPort() : Integer.parseInt(invoker.getUrl().getParameter(METRICS_PORT));
Invoker<MetricsService> metricsInvoker = initMetricsInvoker();
try {
protocol.export(metricsInvoker);
} catch (RuntimeException e) {
logger.error("Metrics Service need to be configured" + " when multiple processes are running on a host" + e.getMessage());
}
}
RpcContext context = RpcContext.getContext();
boolean isProvider = context.isProviderSide();
long start = System.currentTimeMillis();
try {
// proceed invocation chain
Result result = invoker.invoke(invocation);
long duration = System.currentTimeMillis() - start;
reportMetrics(invoker, invocation, duration, "success", isProvider);
return result;
} catch (RpcException e) {
long duration = System.currentTimeMillis() - start;
String result = "error";
if (e.isTimeout()) {
result = "timeoutError";
}
if (e.isBiz()) {
result = "bisError";
}
if (e.isNetwork()) {
result = "networkError";
}
if (e.isSerialization()) {
result = "serializationError";
}
reportMetrics(invoker, invocation, duration, result, isProvider);
throw e;
}
}
use of org.apache.dubbo.rpc.Protocol in project dubbo by alibaba.
the class SpringExtensionFactoryTest method testGetExtensionBySPI.
@Test
public void testGetExtensionBySPI() {
Protocol protocol = springExtensionFactory.getExtension(Protocol.class, "protocol");
Assertions.assertNull(protocol);
}
use of org.apache.dubbo.rpc.Protocol in project dubbo by alibaba.
the class RegistryProtocolTest method testExport.
@Test
public void testExport() {
RegistryProtocol registryProtocol = getRegistryProtocol();
// registryProtocol.setCluster(new FailfastCluster());
registryProtocol.setRegistryFactory(ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension());
ServiceDescriptor descriptor = ApplicationModel.getServiceRepository().registerService(DemoService.class);
ApplicationModel.getServiceRepository().registerProvider(service, new DemoServiceImpl(), descriptor, null, null);
Protocol dubboProtocol = DubboProtocol.getDubboProtocol();
registryProtocol.setProtocol(dubboProtocol);
URL newRegistryUrl = registryUrl.addParameter(EXPORT_KEY, serviceUrl);
DubboInvoker<DemoService> invoker = new DubboInvoker<DemoService>(DemoService.class, newRegistryUrl, new ExchangeClient[] { new MockedClient("10.20.20.20", 2222, true) });
Exporter<DemoService> exporter = registryProtocol.export(invoker);
Exporter<DemoService> exporter2 = registryProtocol.export(invoker);
// The same invoker, exporter that multiple exported are different
Assertions.assertNotSame(exporter, exporter2);
exporter.unexport();
exporter2.unexport();
}
Aggregations