请参见 元数据中心手册
org.apache.dubbo.metadata.store.MetadataReportFactory
org.apache.dubbo.metadata.store.MetadataReport
参考:org.apache.dubbo.metadata.store.MetadataReportFactory,org.apache.dubbo.metadata.store.MetadataReport
@SPI("redis")
public interface MetadataReportFactory {
@Adaptive({"protocol"})
MetadataReport getMetadataReport(URL url);
}
下面以Redis存储为例进行说明。
新建一个project,需要支持以下修改:
public class RedisMetadataReport extends AbstractMetadataReport {
private final static Logger logger = LoggerFactory.getLogger(RedisMetadataReport.class);
final JedisPool pool;
public RedisMetadataReport(URL url) {
super(url);
pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort());
}
@Override
protected void doStoreProviderMetadata(ProviderMetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) {
this.storeMetadata(providerMetadataIdentifier, serviceDefinitions);
}
@Override
protected void doStoreConsumerMetadata(ConsumerMetadataIdentifier consumerMetadataIdentifier, String value) {
this.storeMetadata(consumerMetadataIdentifier, value);
}
private void storeMetadata(MetadataIdentifier metadataIdentifier, String v) {
try (Jedis jedis = pool.getResource()) {
jedis.set(metadataIdentifier.getIdentifierKey() + META_DATA_SOTRE_TAG, v);
} catch (Throwable e) {
logger.error("Failed to put " + metadataIdentifier + " to redis " + v + ", cause: " + e.getMessage(), e);
throw new RpcException("Failed to put " + metadataIdentifier + " to redis " + v + ", cause: " + e.getMessage(), e);
}
}
}
public class RedisMetadataReportFactory extends AbstractMetadataReportFactory {
@Override
public MetadataReport createMetadataReport(URL url) {
return new RedisMetadataReport(url);
}
}
META-INF/dubbo/internal/org.apache.dubbo.metadata.store.MetadataReportFactory
redis=org.apache.dubbo.metadata.store.redis.RedisMetadataReportFactory
只要将上面的修改和project打包成jar包,然后配置元数据中心的url:redis://10.20.153.10:6379。
至此,一个自定义的元数据存储就可以运行了。