From df084addc804ef727ae9f1959b10f88ffd5a0ddc Mon Sep 17 00:00:00 2001 From: cvictory Date: Wed, 22 May 2019 15:31:37 +0800 Subject: [PATCH] Support Redis cluster in Metadata Report (#3863) fixes #3817 --- .../dubbo/config/MetadataReportConfig.java | 13 ++++++ .../main/resources/META-INF/compat/dubbo.xsd | 5 ++ .../src/main/resources/META-INF/dubbo.xsd | 5 ++ .../identifier/MetadataIdentifier.java | 4 +- .../store/redis/RedisMetadataReport.java | 46 +++++++++++++++++-- 5 files changed, 67 insertions(+), 6 deletions(-) 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 a7c72f6f41b..5233171e011 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 c12389f805e..756f6dae665 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 5cc621fc872..4a8cf56c8d8 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-api/src/main/java/org/apache/dubbo/metadata/identifier/MetadataIdentifier.java b/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/identifier/MetadataIdentifier.java index 602e4b298b7..3b20bc7a373 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/identifier/MetadataIdentifier.java +++ b/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/identifier/MetadataIdentifier.java @@ -31,8 +31,8 @@ public class MetadataIdentifier { public static final String SEPARATOR = ":"; - final static String DEFAULT_PATH_TAG = "metadata"; - final static String META_DATA_STORE_TAG = ".metaData"; + public final static String DEFAULT_PATH_TAG = "metadata"; + public final static String META_DATA_STORE_TAG = ".metaData"; private String serviceInterface; private String version; 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 c0b992da466..a9245eb28d8 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 @@ -22,12 +22,22 @@ import org.apache.dubbo.metadata.identifier.MetadataIdentifier; import org.apache.dubbo.metadata.support.AbstractMetadataReport; import org.apache.dubbo.rpc.RpcException; + +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +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; + import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT; import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY; +import static org.apache.dubbo.common.constants.ConfigConstants.CLUSTER_KEY; +import static org.apache.dubbo.metadata.identifier.MetadataIdentifier.META_DATA_STORE_TAG; /** * RedisMetadataReport @@ -36,12 +46,24 @@ public class RedisMetadataReport extends AbstractMetadataReport { private final static Logger logger = LoggerFactory.getLogger(RedisMetadataReport.class); - final JedisPool pool; + JedisPool pool; + Set jedisClusterNodes; + private int timeout; + private String password; + public RedisMetadataReport(URL url) { super(url); - int timeout = url.getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT); - pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort(), timeout, url.getPassword()); + timeout = url.getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT); + if (url.getParameter(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(), timeout, url.getPassword()); + } } @Override @@ -55,6 +77,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, timeout, timeout, 2, password, new GenericObjectPoolConfig())) { + 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 cluster " + v + ", cause: " + e.getMessage(), e); + } + } + + private void storeMetadataStandalone(MetadataIdentifier metadataIdentifier, String v) { try (Jedis jedis = pool.getResource()) { jedis.set(metadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), v); } catch (Throwable e) { @@ -63,5 +102,4 @@ private void storeMetadata(MetadataIdentifier metadataIdentifier, String v) { } } - }