use of org.apache.dubbo.common.URLBuilder 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.common.URLBuilder in project dubbo by alibaba.
the class DubboMonitorTest method testCount.
@Test
public void testCount() throws Exception {
DubboMonitor monitor = new DubboMonitor(monitorInvoker, monitorService);
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();
monitor.collect(statistics);
monitor.send();
while (lastStatistics == null) {
Thread.sleep(10);
}
Assertions.assertEquals("morgan", lastStatistics.getParameter(MonitorService.APPLICATION));
Assertions.assertEquals("dubbo", lastStatistics.getProtocol());
Assertions.assertEquals("10.20.153.10", lastStatistics.getHost());
Assertions.assertEquals("morgan", lastStatistics.getParameter(MonitorService.APPLICATION));
Assertions.assertEquals("MemberService", lastStatistics.getParameter(MonitorService.INTERFACE));
Assertions.assertEquals("findPerson", lastStatistics.getParameter(MonitorService.METHOD));
Assertions.assertEquals("10.20.153.11", lastStatistics.getParameter(MonitorService.CONSUMER));
Assertions.assertEquals("1", lastStatistics.getParameter(MonitorService.SUCCESS));
Assertions.assertEquals("0", lastStatistics.getParameter(MonitorService.FAILURE));
Assertions.assertEquals("3", lastStatistics.getParameter(MonitorService.ELAPSED));
Assertions.assertEquals("3", lastStatistics.getParameter(MonitorService.MAX_ELAPSED));
Assertions.assertEquals("1", lastStatistics.getParameter(MonitorService.CONCURRENT));
Assertions.assertEquals("1", lastStatistics.getParameter(MonitorService.MAX_CONCURRENT));
monitor.destroy();
}
use of org.apache.dubbo.common.URLBuilder in project dubbo by alibaba.
the class StatisticsTest method testToString.
@Test
public void testToString() {
Statistics statistics = new Statistics(new URL("dubbo", "10.20.153.10", 0));
statistics.setApplication("demo");
statistics.setMethod("findPerson");
statistics.setServer("10.20.153.10");
statistics.setGroup("unit-test");
statistics.setService("MemberService");
assertThat(statistics.toString(), is("dubbo://10.20.153.10"));
Statistics statisticsWithDetailInfo = new 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.GROUP, "unit-test").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());
MatcherAssert.assertThat(statisticsWithDetailInfo.getServer(), equalTo(statistics.getServer()));
MatcherAssert.assertThat(statisticsWithDetailInfo.getService(), equalTo(statistics.getService()));
MatcherAssert.assertThat(statisticsWithDetailInfo.getMethod(), equalTo(statistics.getMethod()));
MatcherAssert.assertThat(statisticsWithDetailInfo.getGroup(), equalTo(statistics.getGroup()));
MatcherAssert.assertThat(statisticsWithDetailInfo, not(equalTo(statistics)));
}
use of org.apache.dubbo.common.URLBuilder in project dubbo by alibaba.
the class DubboMonitorFactory method createMonitor.
@Override
protected Monitor createMonitor(URL url) {
URLBuilder urlBuilder = URLBuilder.from(url);
urlBuilder.setProtocol(url.getParameter(PROTOCOL_KEY, DUBBO_PROTOCOL));
if (StringUtils.isEmpty(url.getPath())) {
urlBuilder.setPath(MonitorService.class.getName());
}
String filter = url.getParameter(REFERENCE_FILTER_KEY);
if (StringUtils.isEmpty(filter)) {
filter = "";
} else {
filter = filter + ",";
}
urlBuilder.addParameters(CHECK_KEY, String.valueOf(false), REFERENCE_FILTER_KEY, filter + "-monitor");
Invoker<MonitorService> monitorInvoker = protocol.refer(MonitorService.class, urlBuilder.build());
MonitorService monitorService = proxyFactory.getProxy(monitorInvoker);
return new DubboMonitor(monitorInvoker, monitorService);
}
use of org.apache.dubbo.common.URLBuilder in project dubbo by alibaba.
the class StubProxyFactoryWrapper method getProxy.
@Override
public <T> T getProxy(Invoker<T> invoker, boolean generic) throws RpcException {
T proxy = proxyFactory.getProxy(invoker, generic);
if (GenericService.class != invoker.getInterface()) {
URL url = invoker.getUrl();
String stub = url.getParameter(STUB_KEY, url.getParameter(LOCAL_KEY));
if (ConfigUtils.isNotEmpty(stub)) {
Class<?> serviceType = invoker.getInterface();
if (ConfigUtils.isDefault(stub)) {
if (url.hasParameter(STUB_KEY)) {
stub = serviceType.getName() + "Stub";
} else {
stub = serviceType.getName() + "Local";
}
}
try {
Class<?> stubClass = ReflectUtils.forName(stub);
if (!serviceType.isAssignableFrom(stubClass)) {
throw new IllegalStateException("The stub implementation class " + stubClass.getName() + " not implement interface " + serviceType.getName());
}
try {
Constructor<?> constructor = ReflectUtils.findConstructor(stubClass, serviceType);
proxy = (T) constructor.newInstance(new Object[] { proxy });
// export stub service
URLBuilder urlBuilder = URLBuilder.from(url);
if (url.getParameter(STUB_EVENT_KEY, DEFAULT_STUB_EVENT)) {
urlBuilder.addParameter(STUB_EVENT_METHODS_KEY, StringUtils.join(Wrapper.getWrapper(proxy.getClass()).getDeclaredMethodNames(), ","));
urlBuilder.addParameter(IS_SERVER_KEY, Boolean.FALSE.toString());
try {
export(proxy, (Class) invoker.getInterface(), urlBuilder.build());
} catch (Exception e) {
LOGGER.error("export a stub service error.", e);
}
}
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No such constructor \"public " + stubClass.getSimpleName() + "(" + serviceType.getName() + ")\" in stub implementation class " + stubClass.getName(), e);
}
} catch (Throwable t) {
LOGGER.error("Failed to create stub implementation class " + stub + " in consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", cause: " + t.getMessage(), t);
// ignore
}
}
}
return proxy;
}
Aggregations