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

songxiaosheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-spi-extensions.git


The following commit(s) were added to refs/heads/master by this push:
     new 8819a2b2 add SpringCloudServiceInstanceNotificationCustomizer (#587)
8819a2b2 is described below

commit 8819a2b2ee31c5f7a181549c126f3b84e967b1f9
Author: heliang666s <[email protected]>
AuthorDate: Sat Jan 18 13:56:36 2025 +0800

    add SpringCloudServiceInstanceNotificationCustomizer (#587)
    
    * add SpringCloudServiceInstanceNotificationCustomizer
    
    * add licenses
    
    * fix dependency
    
    * fix dependencies
    
    * fix file name
    
    * fix file name
    
    * fix bugs
---
 .../dubbo-service-discovery-springcloud/pom.xml    | 43 +++++++++++++
 ...CloudServiceInstanceNotificationCustomizer.java | 73 ++++++++++++++++++++++
 ....metadata.ServiceInstanceNotificationCustomizer |  1 +
 ...dServiceInstanceNotificationCustomizerTest.java | 66 +++++++++++++++++++
 dubbo-service-discovery-extensions/pom.xml         | 40 ++++++++++++
 pom.xml                                            |  1 +
 6 files changed, 224 insertions(+)

diff --git 
a/dubbo-service-discovery-extensions/dubbo-service-discovery-springcloud/pom.xml
 
b/dubbo-service-discovery-extensions/dubbo-service-discovery-springcloud/pom.xml
new file mode 100644
index 00000000..236b6845
--- /dev/null
+++ 
b/dubbo-service-discovery-extensions/dubbo-service-discovery-springcloud/pom.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0";
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <artifactId>dubbo-service-discovery-extensions</artifactId>
+        <groupId>org.apache.dubbo.extensions</groupId>
+        <version>${revision}</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>dubbo-service-discovery-springcloud</artifactId>
+    <version>${revision}</version>
+
+    <properties>
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo-registry-api</artifactId>
+        </dependency>
+    </dependencies>
+
+</project>
diff --git 
a/dubbo-service-discovery-extensions/dubbo-service-discovery-springcloud/src/main/java/org/apache/dubbo/service/discovery/springcloud/SpringCloudServiceInstanceNotificationCustomizer.java
 
b/dubbo-service-discovery-extensions/dubbo-service-discovery-springcloud/src/main/java/org/apache/dubbo/service/discovery/springcloud/SpringCloudServiceInstanceNotificationCustomizer.java
new file mode 100644
index 00000000..c6f1d72f
--- /dev/null
+++ 
b/dubbo-service-discovery-extensions/dubbo-service-discovery-springcloud/src/main/java/org/apache/dubbo/service/discovery/springcloud/SpringCloudServiceInstanceNotificationCustomizer.java
@@ -0,0 +1,73 @@
+/*
+ * 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.service.discovery.springcloud;
+
+import org.apache.dubbo.common.ProtocolServiceKey;
+import org.apache.dubbo.metadata.MetadataInfo;
+import org.apache.dubbo.registry.client.ServiceInstance;
+import 
org.apache.dubbo.registry.client.metadata.ServiceInstanceNotificationCustomizer;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class SpringCloudServiceInstanceNotificationCustomizer implements 
ServiceInstanceNotificationCustomizer {
+
+    @Override
+    public void customize(List<ServiceInstance> serviceInstance) {
+        if (serviceInstance.isEmpty()) {
+            return;
+        }
+
+        if (!serviceInstance.stream()
+            .allMatch(instance -> 
"SPRING_CLOUD".equals(instance.getMetadata("preserved.register.source")))) {
+            return;
+        }
+
+        for (ServiceInstance instance : serviceInstance) {
+            MetadataInfo.ServiceInfo serviceInfo =
+                new MetadataInfo.ServiceInfo("*", "*", "*", "rest", 
instance.getPort(), "*", new HashMap<>());
+            String revision = "SPRING_CLOUD-" + instance.getServiceName() + 
"-" + instance.getAddress() + "-"
+                + instance.getPort();
+            MetadataInfo metadataInfo =
+                new MetadataInfo(
+                    instance.getServiceName(),
+                    revision,
+                    new ConcurrentHashMap<>(Collections.singletonMap("*", 
serviceInfo))) {
+                    @Override
+                    public List<ServiceInfo> 
getMatchedServiceInfos(ProtocolServiceKey consumerProtocolServiceKey) {
+                        getServices()
+                            .putIfAbsent(
+                                
consumerProtocolServiceKey.getServiceKeyString(),
+                                new MetadataInfo.ServiceInfo(
+                                    
consumerProtocolServiceKey.getInterfaceName(),
+                                    consumerProtocolServiceKey.getGroup(),
+                                    consumerProtocolServiceKey.getVersion(),
+                                    consumerProtocolServiceKey.getProtocol(),
+                                    instance.getPort(),
+                                    
consumerProtocolServiceKey.getInterfaceName(),
+                                    new HashMap<>()));
+                        return 
super.getMatchedServiceInfos(consumerProtocolServiceKey);
+                    }
+                };
+
+            instance.setServiceMetadata(metadataInfo);
+        }
+    }
+}
diff --git 
a/dubbo-service-discovery-extensions/dubbo-service-discovery-springcloud/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.metadata.ServiceInstanceNotificationCustomizer
 
b/dubbo-service-discovery-extensions/dubbo-service-discovery-springcloud/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.metadata.ServiceInstanceNotificationCustomizer
new file mode 100644
index 00000000..cce5ce0b
--- /dev/null
+++ 
b/dubbo-service-discovery-extensions/dubbo-service-discovery-springcloud/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.metadata.ServiceInstanceNotificationCustomizer
@@ -0,0 +1 @@
+spring-cloud=org.apache.dubbo.service.discovery.springcloud.SpringCloudServiceInstanceNotificationCustomizer
diff --git 
a/dubbo-service-discovery-extensions/dubbo-service-discovery-springcloud/src/test/java/org/apache/dubbo/service/discovery/springcloud/test/SpringCloudServiceInstanceNotificationCustomizerTest.java
 
b/dubbo-service-discovery-extensions/dubbo-service-discovery-springcloud/src/test/java/org/apache/dubbo/service/discovery/springcloud/test/SpringCloudServiceInstanceNotificationCustomizerTest.java
new file mode 100644
index 00000000..df007e30
--- /dev/null
+++ 
b/dubbo-service-discovery-extensions/dubbo-service-discovery-springcloud/src/test/java/org/apache/dubbo/service/discovery/springcloud/test/SpringCloudServiceInstanceNotificationCustomizerTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.service.discovery.springcloud.test;
+
+import org.apache.dubbo.metadata.MetadataInfo;
+import org.apache.dubbo.registry.client.DefaultServiceInstance;
+import org.apache.dubbo.registry.client.ServiceInstance;
+import 
org.apache.dubbo.service.discovery.springcloud.SpringCloudServiceInstanceNotificationCustomizer;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class SpringCloudServiceInstanceNotificationCustomizerTest {
+    private SpringCloudServiceInstanceNotificationCustomizer customizer;
+
+    @BeforeEach
+    void setUp() {
+        customizer = new SpringCloudServiceInstanceNotificationCustomizer();
+    }
+
+    @Test
+    void testCustomize() {
+        // Mock ServiceInstance
+        ServiceInstance instance = Mockito.spy(new DefaultServiceInstance());
+        
Mockito.when(instance.getMetadata("preserved.register.source")).thenReturn("SPRING_CLOUD");
+        Mockito.when(instance.getServiceName()).thenReturn("myService");
+        Mockito.when(instance.getAddress()).thenReturn("127.0.0.1");
+        Mockito.when(instance.getPort()).thenReturn(8080);
+
+        List<ServiceInstance> instances = Collections.singletonList(instance);
+        // Call customize method with the mocked instance
+        customizer.customize(instances);
+
+        // Verify that the metadata is correctly set
+        
Mockito.verify(instance).setServiceMetadata(Mockito.any(MetadataInfo.class));
+
+        // Check if MetadataInfo is set properly
+        MetadataInfo metadataInfo = instance.getServiceMetadata();
+        assertNotNull(metadataInfo);
+
+        // Additional assertions to verify metadata details
+        assertTrue(metadataInfo.getRevision().startsWith("SPRING_CLOUD"));
+    }
+}
diff --git a/dubbo-service-discovery-extensions/pom.xml 
b/dubbo-service-discovery-extensions/pom.xml
new file mode 100644
index 00000000..bf24ee82
--- /dev/null
+++ b/dubbo-service-discovery-extensions/pom.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0";
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.dubbo.extensions</groupId>
+        <artifactId>extensions-parent</artifactId>
+        <version>${revision}</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+    <version>${revision}</version>
+    <packaging>pom</packaging>
+    <artifactId>dubbo-service-discovery-extensions</artifactId>
+
+    <properties>
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <modules>
+        <module>dubbo-service-discovery-springcloud</module>
+    </modules>
+
+</project>
diff --git a/pom.xml b/pom.xml
index 6e2cf7a2..c19689bc 100644
--- a/pom.xml
+++ b/pom.xml
@@ -102,6 +102,7 @@
         <module>dubbo-kubernetes</module>
         <module>dubbo-wasm</module>
         <module>dubbo-proxy-extensions</module>
+        <module>dubbo-service-discovery-extensions</module>
     </modules>
 
     <properties>

Reply via email to