@Override
    public void customize(ServiceInstance serviceInstance, ApplicationModel applicationModel) {
        if (serviceInstance.getPort() > 0) {
            return;
        }

        MetadataInfo metadataInfo = serviceInstance.getServiceMetadata();
        if (metadataInfo == null || CollectionUtils.isEmptyMap(metadataInfo.getExportedServiceURLs())) {
            return;
        }

        String host = null;
        int port = -1;
        Set<URL> urls = metadataInfo.collectExportedURLSet();

        if (CollectionUtils.isNotEmpty(urls)) {
            String preferredProtocol = applicationModel.getCurrentConfig().getProtocol();
            if (preferredProtocol != null) {
                for (URL exportedURL : urls) {
                    if (preferredProtocol.equals(exportedURL.getProtocol())) {
                        host = exportedURL.getHost();
                        port = exportedURL.getPort();
                        break;
                    }
                }

                if (host == null || port == -1) {

                    // 4-2 - Can't find an instance URL using the default preferredProtocol.

                    logger.warn(
                            PROTOCOL_FAILED_INIT_SERIALIZATION_OPTIMIZER,
                            "typo in preferred protocol",
                            "",
                            "Can't find an instance URL using the default preferredProtocol \"" + preferredProtocol
                                    + "\", " + "falling back to the strategy that pick the first found protocol. "
                                    + "Please try modifying the config of dubbo.application.protocol");

                    URL url = urls.iterator().next();
                    host = url.getHost();
                    port = url.getPort();
                }
            } else {
                URL url = urls.iterator().next();
                host = url.getHost();
                port = url.getPort();
            }

            if (serviceInstance instanceof DefaultServiceInstance) {
                DefaultServiceInstance instance = (DefaultServiceInstance) serviceInstance;
                instance.setHost(host);
                instance.setPort(port);
            }
        }
    }
@Test
    void customizePreferredProtocol() throws ExecutionException, InterruptedException {
        ScopeBeanFactory beanFactory = mock(ScopeBeanFactory.class);
        MetadataService metadataService = mock(MetadataService.class);
        when(beanFactory.getBean(MetadataService.class)).thenReturn(metadataService);
        ApplicationModel applicationModel = spy(ApplicationModel.defaultModel());
        when(applicationModel.getBeanFactory()).thenReturn(beanFactory);

        // test protocol set
        ApplicationConfig applicationConfig = new ApplicationConfig("aa");
        // when(applicationModel.getCurrentConfig()).thenReturn(applicationConfig);
        doReturn(applicationConfig).when(applicationModel).getCurrentConfig();
        DefaultServiceInstance serviceInstance1 =
                new DefaultServiceInstance("without-preferredProtocol", applicationModel);
        MetadataInfo metadataInfo = new MetadataInfo();
        metadataInfo.addService(URL.valueOf("tri://127.1.1.1:50052/org.apache.dubbo.demo.GreetingService"));
        serviceInstance1.setServiceMetadata(metadataInfo);
        serviceInstanceHostPortCustomizer.customize(serviceInstance1, applicationModel);
        Assertions.assertEquals("127.1.1.1", serviceInstance1.getHost());
        Assertions.assertEquals(50052, serviceInstance1.getPort());

        // pick the preferredProtocol
        applicationConfig.setProtocol("tri");
        metadataInfo.addService(URL.valueOf("dubbo://127.1.2.3:20889/org.apache.dubbo.demo.HelloService"));
        serviceInstanceHostPortCustomizer.customize(serviceInstance1, applicationModel);
        Assertions.assertEquals("127.1.1.1", serviceInstance1.getHost());
        Assertions.assertEquals(50052, serviceInstance1.getPort());
    }