Search in sources :

Example 1 with MonitorService

use of org.apache.dubbo.monitor.MonitorService in project dubbo by alibaba.

the class DubboMonitorTest method testLookUp.

@Test
public void testLookUp() {
    Invoker invoker = mock(Invoker.class);
    MonitorService monitorService = mock(MonitorService.class);
    URL queryUrl = URL.valueOf("dubbo://127.0.0.1:7070?interval=20");
    given(invoker.getUrl()).willReturn(queryUrl);
    DubboMonitor dubboMonitor = new DubboMonitor(invoker, monitorService);
    dubboMonitor.lookup(queryUrl);
    verify(monitorService).lookup(eq(queryUrl));
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) MonitorService(org.apache.dubbo.monitor.MonitorService) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 2 with MonitorService

use of org.apache.dubbo.monitor.MonitorService in project dubbo by alibaba.

the class DubboMonitorTest method testSum.

@Test
public void testSum() {
    URL statistics = new URLBuilder(DUBBO_PROTOCOL, "10.20.153.11", 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();
    Invoker invoker = mock(Invoker.class);
    MonitorService monitorService = mock(MonitorService.class);
    given(invoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:7070?interval=20"));
    DubboMonitor dubboMonitor = new DubboMonitor(invoker, monitorService);
    dubboMonitor.collect(statistics);
    dubboMonitor.collect(statistics.addParameter(MonitorService.SUCCESS, 3).addParameter(MonitorService.CONCURRENT, 2).addParameter(MonitorService.INPUT, 1).addParameter(MonitorService.OUTPUT, 2));
    dubboMonitor.collect(statistics.addParameter(MonitorService.SUCCESS, 6).addParameter(MonitorService.ELAPSED, 2));
    dubboMonitor.send();
    ArgumentCaptor<URL> summaryCaptor = ArgumentCaptor.forClass(URL.class);
    verify(monitorService, atLeastOnce()).collect(summaryCaptor.capture());
    List<URL> allValues = summaryCaptor.getAllValues();
    assertThat(allValues, not(nullValue()));
    assertThat(allValues, hasItem(new CustomMatcher<URL>("Monitor count should greater than 1") {

        @Override
        public boolean matches(Object item) {
            URL url = (URL) item;
            return Integer.valueOf(url.getParameter(MonitorService.SUCCESS)) > 1;
        }
    }));
}
Also used : CustomMatcher(org.hamcrest.CustomMatcher) Invoker(org.apache.dubbo.rpc.Invoker) MonitorService(org.apache.dubbo.monitor.MonitorService) URL(org.apache.dubbo.common.URL) URLBuilder(org.apache.dubbo.common.URLBuilder) Test(org.junit.jupiter.api.Test)

Example 3 with MonitorService

use of org.apache.dubbo.monitor.MonitorService in project dubbo by alibaba.

the class DubboMonitorTest method testAvailable.

@Test
public void testAvailable() {
    Invoker invoker = mock(Invoker.class);
    MonitorService monitorService = mock(MonitorService.class);
    given(invoker.isAvailable()).willReturn(true);
    given(invoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:7070?interval=20"));
    DubboMonitor dubboMonitor = new DubboMonitor(invoker, monitorService);
    assertThat(dubboMonitor.isAvailable(), is(true));
    verify(invoker).isAvailable();
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) MonitorService(org.apache.dubbo.monitor.MonitorService) Test(org.junit.jupiter.api.Test)

Example 4 with MonitorService

use of org.apache.dubbo.monitor.MonitorService 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();
    }
}
Also used : Monitor(org.apache.dubbo.monitor.Monitor) MonitorFactory(org.apache.dubbo.monitor.MonitorFactory) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) MonitorService(org.apache.dubbo.monitor.MonitorService) Protocol(org.apache.dubbo.rpc.Protocol) URL(org.apache.dubbo.common.URL) URLBuilder(org.apache.dubbo.common.URLBuilder) Test(org.junit.jupiter.api.Test)

Example 5 with MonitorService

use of org.apache.dubbo.monitor.MonitorService 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);
}
Also used : MonitorService(org.apache.dubbo.monitor.MonitorService) URLBuilder(org.apache.dubbo.common.URLBuilder)

Aggregations

MonitorService (org.apache.dubbo.monitor.MonitorService)5 Test (org.junit.jupiter.api.Test)4 URL (org.apache.dubbo.common.URL)3 URLBuilder (org.apache.dubbo.common.URLBuilder)3 Invoker (org.apache.dubbo.rpc.Invoker)3 Monitor (org.apache.dubbo.monitor.Monitor)1 MonitorFactory (org.apache.dubbo.monitor.MonitorFactory)1 Protocol (org.apache.dubbo.rpc.Protocol)1 ProxyFactory (org.apache.dubbo.rpc.ProxyFactory)1 CustomMatcher (org.hamcrest.CustomMatcher)1