diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java index e7add0a0e81..957703bf1cd 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java @@ -71,6 +71,11 @@ public class MetadataReportConfig extends AbstractConfig { */ private Boolean syncReport; + /** + * cluster + */ + private Boolean cluster; + public MetadataReportConfig() { } @@ -174,4 +179,12 @@ public String getGroup() { public void setGroup(String group) { this.group = group; } + + public Boolean getCluster() { + return cluster; + } + + public void setCluster(Boolean cluster) { + this.cluster = cluster; + } } diff --git a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd index 4f0b1bf7a16..7461eb5d737 100644 --- a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd +++ b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd @@ -632,6 +632,11 @@ + + + + + diff --git a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd index 41af239deb5..8691a74acea 100644 --- a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd +++ b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd @@ -626,6 +626,11 @@ + + + + + diff --git a/dubbo-metadata-report/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java b/dubbo-metadata-report/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java index 4f001484422..1d9f69bdae9 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java +++ b/dubbo-metadata-report/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java @@ -16,16 +16,24 @@ */ package org.apache.dubbo.metadata.store.redis; +import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.metadata.identifier.MetadataIdentifier; import org.apache.dubbo.metadata.support.AbstractMetadataReport; import org.apache.dubbo.rpc.RpcException; + +import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + /** * RedisMetadataReport */ @@ -33,11 +41,21 @@ public class RedisMetadataReport extends AbstractMetadataReport { private final static Logger logger = LoggerFactory.getLogger(RedisMetadataReport.class); - final JedisPool pool; + JedisPool pool; + Set jedisClusterNodes; + public RedisMetadataReport(URL url) { super(url); - pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort()); + if (url.getParameter(Constants.CLUSTER_KEY, false)) { + jedisClusterNodes = new HashSet(); + List urls = url.getBackupUrls(); + for (URL tmpUrl : urls) { + jedisClusterNodes.add(new HostAndPort(tmpUrl.getHost(), tmpUrl.getPort())); + } + } else { + pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort()); + } } @Override @@ -51,6 +69,23 @@ protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdenti } private void storeMetadata(MetadataIdentifier metadataIdentifier, String v) { + if (pool != null) { + storeMetadataStandalone(metadataIdentifier, v); + } else { + storeMetadataInCluster(metadataIdentifier, v); + } + } + + private void storeMetadataInCluster(MetadataIdentifier metadataIdentifier, String v) { + try (JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes)) { + jedisCluster.set(metadataIdentifier.getIdentifierKey() + META_DATA_STORE_TAG, v); + } catch (Throwable e) { + logger.error("Failed to put " + metadataIdentifier + " to redis cluster " + v + ", cause: " + e.getMessage(), e); + throw new RpcException("Failed to put " + metadataIdentifier + " to redis " + v + ", cause: " + e.getMessage(), e); + } + } + + private void storeMetadataStandalone(MetadataIdentifier metadataIdentifier, String v) { try (Jedis jedis = pool.getResource()) { jedis.set(metadataIdentifier.getIdentifierKey() + META_DATA_STORE_TAG, v); } catch (Throwable e) { @@ -59,5 +94,4 @@ private void storeMetadata(MetadataIdentifier metadataIdentifier, String v) { } } - }