Skip to content

Commit

Permalink
Support Redis cluster in Metadata Report (apache#3863)
Browse files Browse the repository at this point in the history
  • Loading branch information
cvictory authored and chickenlj committed May 22, 2019
1 parent a7f56a3 commit df084ad
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public class MetadataReportConfig extends AbstractConfig {
*/
private Boolean syncReport;

/**
* cluster
*/
private Boolean cluster;

public MetadataReportConfig() {
}

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,11 @@
<xsd:documentation><![CDATA[ Sync or Async report. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="cluster" type="xsd:boolean" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[ Need cluster support, default false. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>

<xsd:complexType name="configCenterType">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,11 @@
<xsd:documentation><![CDATA[ Sync or Async report. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="cluster" type="xsd:boolean" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[ Need cluster support, default false. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>

<xsd:complexType name="configCenterType">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,12 +46,24 @@ public class RedisMetadataReport extends AbstractMetadataReport {

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

final JedisPool pool;
JedisPool pool;
Set<HostAndPort> 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<HostAndPort>();
List<URL> 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
Expand All @@ -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) {
Expand All @@ -63,5 +102,4 @@ private void storeMetadata(MetadataIdentifier metadataIdentifier, String v) {
}
}


}

0 comments on commit df084ad

Please sign in to comment.