Skip to content

Commit df084ad

Browse files
cvictorychickenlj
authored andcommitted
Support Redis cluster in Metadata Report (#3863)
fixes #3817
1 parent a7f56a3 commit df084ad

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public class MetadataReportConfig extends AbstractConfig {
7171
*/
7272
private Boolean syncReport;
7373

74+
/**
75+
* cluster
76+
*/
77+
private Boolean cluster;
78+
7479
public MetadataReportConfig() {
7580
}
7681

@@ -174,4 +179,12 @@ public String getGroup() {
174179
public void setGroup(String group) {
175180
this.group = group;
176181
}
182+
183+
public Boolean getCluster() {
184+
return cluster;
185+
}
186+
187+
public void setCluster(Boolean cluster) {
188+
this.cluster = cluster;
189+
}
177190
}

dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,11 @@
632632
<xsd:documentation><![CDATA[ Sync or Async report. ]]></xsd:documentation>
633633
</xsd:annotation>
634634
</xsd:attribute>
635+
<xsd:attribute name="cluster" type="xsd:boolean" use="optional">
636+
<xsd:annotation>
637+
<xsd:documentation><![CDATA[ Need cluster support, default false. ]]></xsd:documentation>
638+
</xsd:annotation>
639+
</xsd:attribute>
635640
</xsd:complexType>
636641

637642
<xsd:complexType name="configCenterType">

dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,11 @@
626626
<xsd:documentation><![CDATA[ Sync or Async report. ]]></xsd:documentation>
627627
</xsd:annotation>
628628
</xsd:attribute>
629+
<xsd:attribute name="cluster" type="xsd:boolean" use="optional">
630+
<xsd:annotation>
631+
<xsd:documentation><![CDATA[ Need cluster support, default false. ]]></xsd:documentation>
632+
</xsd:annotation>
633+
</xsd:attribute>
629634
</xsd:complexType>
630635

631636
<xsd:complexType name="configCenterType">

dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/identifier/MetadataIdentifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
public class MetadataIdentifier {
3232

3333
public static final String SEPARATOR = ":";
34-
final static String DEFAULT_PATH_TAG = "metadata";
35-
final static String META_DATA_STORE_TAG = ".metaData";
34+
public final static String DEFAULT_PATH_TAG = "metadata";
35+
public final static String META_DATA_STORE_TAG = ".metaData";
3636

3737
private String serviceInterface;
3838
private String version;

dubbo-metadata-report/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@
2222
import org.apache.dubbo.metadata.identifier.MetadataIdentifier;
2323
import org.apache.dubbo.metadata.support.AbstractMetadataReport;
2424
import org.apache.dubbo.rpc.RpcException;
25+
26+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
27+
import redis.clients.jedis.HostAndPort;
2528
import redis.clients.jedis.Jedis;
29+
import redis.clients.jedis.JedisCluster;
2630
import redis.clients.jedis.JedisPool;
2731
import redis.clients.jedis.JedisPoolConfig;
2832

33+
import java.util.HashSet;
34+
import java.util.List;
35+
import java.util.Set;
36+
2937
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT;
3038
import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY;
39+
import static org.apache.dubbo.common.constants.ConfigConstants.CLUSTER_KEY;
40+
import static org.apache.dubbo.metadata.identifier.MetadataIdentifier.META_DATA_STORE_TAG;
3141

3242
/**
3343
* RedisMetadataReport
@@ -36,12 +46,24 @@ public class RedisMetadataReport extends AbstractMetadataReport {
3646

3747
private final static Logger logger = LoggerFactory.getLogger(RedisMetadataReport.class);
3848

39-
final JedisPool pool;
49+
JedisPool pool;
50+
Set<HostAndPort> jedisClusterNodes;
51+
private int timeout;
52+
private String password;
53+
4054

4155
public RedisMetadataReport(URL url) {
4256
super(url);
43-
int timeout = url.getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
44-
pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort(), timeout, url.getPassword());
57+
timeout = url.getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
58+
if (url.getParameter(CLUSTER_KEY, false)) {
59+
jedisClusterNodes = new HashSet<HostAndPort>();
60+
List<URL> urls = url.getBackupUrls();
61+
for (URL tmpUrl : urls) {
62+
jedisClusterNodes.add(new HostAndPort(tmpUrl.getHost(), tmpUrl.getPort()));
63+
}
64+
} else {
65+
pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort(), timeout, url.getPassword());
66+
}
4567
}
4668

4769
@Override
@@ -55,6 +77,23 @@ protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdenti
5577
}
5678

5779
private void storeMetadata(MetadataIdentifier metadataIdentifier, String v) {
80+
if (pool != null) {
81+
storeMetadataStandalone(metadataIdentifier, v);
82+
} else {
83+
storeMetadataInCluster(metadataIdentifier, v);
84+
}
85+
}
86+
87+
private void storeMetadataInCluster(MetadataIdentifier metadataIdentifier, String v) {
88+
try (JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes, timeout, timeout, 2, password, new GenericObjectPoolConfig())) {
89+
jedisCluster.set(metadataIdentifier.getIdentifierKey() + META_DATA_STORE_TAG, v);
90+
} catch (Throwable e) {
91+
logger.error("Failed to put " + metadataIdentifier + " to redis cluster " + v + ", cause: " + e.getMessage(), e);
92+
throw new RpcException("Failed to put " + metadataIdentifier + " to redis cluster " + v + ", cause: " + e.getMessage(), e);
93+
}
94+
}
95+
96+
private void storeMetadataStandalone(MetadataIdentifier metadataIdentifier, String v) {
5897
try (Jedis jedis = pool.getResource()) {
5998
jedis.set(metadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), v);
6099
} catch (Throwable e) {
@@ -63,5 +102,4 @@ private void storeMetadata(MetadataIdentifier metadataIdentifier, String v) {
63102
}
64103
}
65104

66-
67105
}

0 commit comments

Comments
 (0)