@Override
    public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
        if (UrlUtils.isRegistry(url)) {
            return protocol.refer(type, url);
        }

        Invoker<T> invoker = protocol.refer(type, url);
        if (StringUtils.isEmpty(url.getParameter(REGISTRY_CLUSTER_TYPE_KEY))) {
            invoker = new ListenerInvokerWrapper<>(
                    invoker,
                    Collections.unmodifiableList(ScopeModelUtil.getExtensionLoader(
                                    InvokerListener.class, invoker.getUrl().getScopeModel())
                            .getActivateExtension(url, INVOKER_LISTENER_KEY)));
        }
        return invoker;
    }
@Test
    void testLoadingListenerForRemoteReference() {
        // verify that no listener is loaded by default
        URL urlWithoutListener = URL.valueOf("dubbo://127.0.0.1:20880/DemoService")
                .addParameter(INTERFACE_KEY, DemoService.class.getName());
        AbstractInvoker<DemoService> invokerWithoutListener =
                new AbstractInvoker<DemoService>(DemoService.class, urlWithoutListener) {
                    @Override
                    protected Result doInvoke(Invocation invocation) throws Throwable {
                        return null;
                    }
                };

        Protocol protocolWithoutListener = mock(Protocol.class);
        when(protocolWithoutListener.refer(DemoService.class, urlWithoutListener))
                .thenReturn(invokerWithoutListener);

        ProtocolListenerWrapper protocolListenerWrapperWithoutListener =
                new ProtocolListenerWrapper(protocolWithoutListener);

        Invoker<?> invoker = protocolListenerWrapperWithoutListener.refer(DemoService.class, urlWithoutListener);
        Assertions.assertTrue(invoker instanceof ListenerInvokerWrapper);
        Assertions.assertEquals(
                0, ((ListenerInvokerWrapper<?>) invoker).getListeners().size());

        // verify that if the invoker.listener is configured, then load the specified listener
        URL urlWithListener = URL.valueOf("dubbo://127.0.0.1:20880/DemoService")
                .addParameter(INTERFACE_KEY, DemoService.class.getName())
                .addParameter(INVOKER_LISTENER_KEY, "count");
        AbstractInvoker<DemoService> invokerWithListener =
                new AbstractInvoker<DemoService>(DemoService.class, urlWithListener) {
                    @Override
                    protected Result doInvoke(Invocation invocation) throws Throwable {
                        return null;
                    }
                };

        Protocol protocol = mock(Protocol.class);
        when(protocol.refer(DemoService.class, urlWithListener)).thenReturn(invokerWithListener);

        ProtocolListenerWrapper protocolListenerWrapper = new ProtocolListenerWrapper(protocol);

        invoker = protocolListenerWrapper.refer(DemoService.class, urlWithListener);
        Assertions.assertTrue(invoker instanceof ListenerInvokerWrapper);
        Assertions.assertEquals(1, CountInvokerListener.getCounter());
    }