You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by gu...@apache.org on 2021/09/08 03:02:56 UTC

[dubbo] branch 3.0 updated: [3.0] Add preferredProtocol fallback strategy (#8699)

This is an automated email from the ASF dual-hosted git repository.

guohao pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 0fe0321  [3.0] Add preferredProtocol fallback strategy (#8699)
0fe0321 is described below

commit 0fe0321d96e6201043e7a5218d542fa77707f586
Author: plusmancn <pl...@gmail.com>
AuthorDate: Wed Sep 8 11:02:46 2021 +0800

    [3.0] Add preferredProtocol fallback strategy (#8699)
    
    * fix: add preferredProtocol fallback strategy
    
    * test: add unit test for preferredProtocol
    
    * test: isolate applicationModel from default
---
 .../ServiceInstanceHostPortCustomizer.java         | 12 ++++
 .../ServiceInstanceHostPortCustomizerTest.java     | 72 ++++++++++++++++++++++
 2 files changed, 84 insertions(+)

diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/ServiceInstanceHostPortCustomizer.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/ServiceInstanceHostPortCustomizer.java
index 07d8ffa..edccd63 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/ServiceInstanceHostPortCustomizer.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/ServiceInstanceHostPortCustomizer.java
@@ -17,6 +17,8 @@
 package org.apache.dubbo.config.metadata;
 
 import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.metadata.WritableMetadataService;
 import org.apache.dubbo.registry.client.DefaultServiceInstance;
@@ -32,6 +34,8 @@ import java.util.Set;
  * @since 2.7.5
  */
 public class ServiceInstanceHostPortCustomizer implements ServiceInstanceCustomizer {
+    private static final Logger logger = LoggerFactory.getLogger(ServiceInstanceHostPortCustomizer.class);
+    
 
     @Override
     public void customize(ServiceInstance serviceInstance) {
@@ -56,11 +60,19 @@ public class ServiceInstanceHostPortCustomizer implements ServiceInstanceCustomi
                         break;
                     }
                 }
+                
+                if (host == null || port == -1) {
+                    logger.warn("The default preferredProtocol \"" + preferredProtocol + "\" is not found, fall back to the strategy that pick the first found protocol. Please try to modify 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);
diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ServiceInstanceHostPortCustomizerTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ServiceInstanceHostPortCustomizerTest.java
new file mode 100644
index 0000000..1dab33e
--- /dev/null
+++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ServiceInstanceHostPortCustomizerTest.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.config.metadata;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.metadata.WritableMetadataService;
+import org.apache.dubbo.registry.client.DefaultServiceInstance;
+import org.apache.dubbo.registry.client.ServiceInstance;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test for https://github.com/apache/dubbo/issues/8698
+ */
+class ServiceInstanceHostPortCustomizerTest {
+    private static ServiceInstanceHostPortCustomizer serviceInstanceHostPortCustomizer;
+    
+    @BeforeAll
+    public static void setUp() {
+        serviceInstanceHostPortCustomizer = new ServiceInstanceHostPortCustomizer();
+    }
+    
+    @Test
+    void customizePreferredProtocol() {
+        ApplicationModel applicationModel= new ApplicationModel(new FrameworkModel());
+        applicationModel.getApplicationConfigManager().setApplication(new ApplicationConfig("service-preferredProtocol"));
+        
+        WritableMetadataService writableMetadataService = WritableMetadataService.getDefaultExtension(applicationModel);
+        
+        // Only have tri protocol
+        writableMetadataService.exportURL(
+            URL.valueOf("tri://127.1.1.1:50052/org.apache.dubbo.demo.GreetingService")
+        );
+        
+        // Trigger the fallback strategy
+        ServiceInstance serviceInstance1 = new DefaultServiceInstance("without-preferredProtocol", applicationModel);
+        serviceInstanceHostPortCustomizer.customize(serviceInstance1);
+        Assertions.assertEquals("127.1.1.1", serviceInstance1.getHost());
+        Assertions.assertEquals(50052, serviceInstance1.getPort());
+        
+        
+        // Add the default protocol
+        writableMetadataService.exportURL(
+            URL.valueOf("dubbo://127.1.2.3:20889/org.apache.dubbo.demo.HelloService")
+        );
+        
+        // pick the preferredProtocol
+        ServiceInstance serviceInstance2 = new DefaultServiceInstance("with-preferredProtocol", applicationModel);
+        serviceInstanceHostPortCustomizer.customize(serviceInstance2);
+        Assertions.assertEquals("127.1.2.3", serviceInstance2.getHost());
+        Assertions.assertEquals(20889, serviceInstance2.getPort());
+    }
+}