Skip to content

Commit

Permalink
Revert "Use MetadataReport to get metadata. (#1154)"
Browse files Browse the repository at this point in the history
This reverts commit 6328415.
  • Loading branch information
chickenlj committed Aug 31, 2023
1 parent 7654400 commit 20c5ede
Show file tree
Hide file tree
Showing 12 changed files with 644 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@
import org.apache.dubbo.admin.registry.mapping.impl.NacosServiceMapping;
import org.apache.dubbo.admin.registry.mapping.impl.NoOpServiceMapping;
import org.apache.dubbo.admin.registry.metadata.MetaDataCollector;
import org.apache.dubbo.admin.registry.metadata.impl.NoOpMetadataCollector;
import org.apache.dubbo.admin.service.impl.InstanceRegistryCache;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.metadata.MappingListener;
import org.apache.dubbo.metadata.report.MetadataReport;
import org.apache.dubbo.metadata.report.MetadataReportInstance;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.RegistryFactory;
import org.apache.dubbo.registry.RegistryService;
Expand All @@ -48,8 +46,8 @@
import org.springframework.context.annotation.DependsOn;

import java.util.Arrays;
import java.util.Optional;

import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.ENABLE_EMPTY_PROTECTION_KEY;
import static org.apache.dubbo.registry.client.ServiceDiscoveryFactory.getExtension;

Expand Down Expand Up @@ -169,19 +167,22 @@ Registry getRegistry() {
@Bean("metaDataCollector")
@DependsOn("governanceConfiguration")
MetaDataCollector getMetadataCollector() {
ApplicationModel applicationModel = ApplicationModel.defaultModel();
ScopeBeanFactory beanFactory = applicationModel.getBeanFactory();
MetadataReportInstance metadataReportInstance = beanFactory.registerBean(MetadataReportInstance.class);

Optional<MetadataReport> metadataReport = metadataReportInstance.getMetadataReports(true)
.values().stream().findAny();

if (metadataReport.isPresent()) {
return metadataReport.get()::getServiceDefinition;
MetaDataCollector metaDataCollector = new NoOpMetadataCollector();
if (metadataUrl == null) {
if (StringUtils.isNotEmpty(metadataAddress)) {
metadataUrl = formUrl(metadataAddress, metadataGroup, metadataGroupNameSpace, username, password);
metadataUrl = metadataUrl.addParameter(CLUSTER_KEY, cluster);
}
logger.info("Admin using metadata address: " + metadataUrl);
}
if (metadataUrl != null) {
metaDataCollector = ApplicationModel.defaultModel().getExtensionLoader(MetaDataCollector.class).getExtension(metadataUrl.getProtocol());
metaDataCollector.setUrl(metadataUrl);
metaDataCollector.init();
} else {
//NoOpMetadataCollector
return key -> null;
logger.warn("you are using dubbo.registry.address, which is not recommend, please refer to: https://github.com/apache/dubbo-admin/wiki/Dubbo-Admin-configuration");
}
return metaDataCollector;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,20 @@

package org.apache.dubbo.admin.registry.metadata;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;

@SPI("zookeeper")
public interface MetaDataCollector {

String getMetaData(MetadataIdentifier key);

void setUrl(URL url);

URL getUrl();

void init();

String getProviderMetaData(MetadataIdentifier key);

String getConsumerMetaData(MetadataIdentifier key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.dubbo.admin.registry.metadata.impl;


import org.apache.dubbo.admin.registry.metadata.MetaDataCollector;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.kv.model.GetValue;
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.report.identifier.KeyTypeEnum;
import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;

import java.util.Objects;


public class ConsulMetaDataCollector implements MetaDataCollector {
private static final Logger LOG = LoggerFactory.getLogger(ConsulMetaDataCollector.class);
private static final int DEFAULT_PORT = 8500;
private URL url;
private ConsulClient client;

@Override
public URL getUrl() {
return this.url;
}

@Override
public void setUrl(URL url) {
this.url = url;
}

@Override
public void init() {
Objects.requireNonNull(this.url, "metadataUrl require not null");
String host = this.url.getHost();
int port = this.url.getPort() != 0 ? url.getPort() : DEFAULT_PORT;
this.client = new ConsulClient(host, port);
}

@Override
public String getProviderMetaData(MetadataIdentifier key) {
return doGetMetaData(key);
}

@Override
public String getConsumerMetaData(MetadataIdentifier key) {
return doGetMetaData(key);
}

private String doGetMetaData(MetadataIdentifier key) {
try {
Response<GetValue> response = this.client.getKVValue(key.getUniqueKey(KeyTypeEnum.UNIQUE_KEY));
return response.getValue().getDecodedValue();
} catch (Exception e) {
LOG.error(String.format("Failed to fetch metadata for %s from consul, cause: %s",
key.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), e.getMessage()), e);
}
return null;
}

//just for test
ConsulClient getClient() {
return this.client;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.dubbo.admin.registry.metadata.impl;

import com.alibaba.nacos.api.PropertyKeyConst;
import java.util.Map;
import org.apache.dubbo.admin.common.util.Constants;
import org.apache.dubbo.admin.registry.metadata.MetaDataCollector;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringConstantFieldValuePredicate;
import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum;
import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.apache.commons.lang3.StringUtils;

import java.util.Properties;

import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR;
import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE;

public class NacosMetaDataCollector implements MetaDataCollector {
private static final Logger logger = LoggerFactory.getLogger(NacosMetaDataCollector.class);
private ConfigService configService;
private String group;
private URL url;
@Override
public void setUrl(URL url) {
this.url = url;
}

@Override
public URL getUrl() {
return url;
}

@Override
public void init() {
group = url.getParameter(Constants.GROUP_KEY, "DEFAULT_GROUP");
configService = buildConfigService(url);
}

private ConfigService buildConfigService(URL url) {
Properties nacosProperties = buildNacosProperties(url);
try {
configService = NacosFactory.createConfigService(nacosProperties);
} catch (NacosException e) {
if (logger.isErrorEnabled()) {
logger.error(e.getErrMsg(), e);
}
throw new IllegalStateException(e);
}
return configService;
}

private Properties buildNacosProperties(URL url) {
Properties properties = new Properties();
setServerAddr(url, properties);
setNamespace(url, properties);
Map<String, String> parameters = url.getParameters(
StringConstantFieldValuePredicate.of(PropertyKeyConst.class));
properties.putAll(parameters);
return properties;
}

private void setServerAddr(URL url, Properties properties) {

String serverAddr = url.getHost() + // Host
":" +
url.getPort() // Port
;
properties.put(SERVER_ADDR, serverAddr);
}

private void setNamespace(URL url, Properties properties) {
String namespace = url.getParameter(NAMESPACE);
if (StringUtils.isNotBlank(namespace)) {
properties.put(NAMESPACE, namespace);
}
}

@Override
public String getProviderMetaData(MetadataIdentifier key) {
return getMetaData(key);
}

@Override
public String getConsumerMetaData(MetadataIdentifier key) {
return getMetaData(key);
}

private String getMetaData(MetadataIdentifier identifier) {
try {
String fromDubboGroup = configService.getConfig(identifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY),
"dubbo", 1000 * 10);
return org.apache.dubbo.common.utils.StringUtils.isNotEmpty(fromDubboGroup) ? fromDubboGroup :
configService.getConfig(identifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY),
group, 1000 * 10);
} catch (NacosException e) {
logger.warn("Failed to get " + identifier + " from nacos, cause: " + e.getMessage(), e);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.dubbo.admin.registry.metadata.impl;

import org.apache.dubbo.admin.registry.metadata.MetaDataCollector;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;

public class NoOpMetadataCollector implements MetaDataCollector {

@Override
public void setUrl(URL url) {

}

@Override
public URL getUrl() {
return null;
}

@Override
public void init() {

}

@Override
public String getProviderMetaData(MetadataIdentifier key) {
return null;
}

@Override
public String getConsumerMetaData(MetadataIdentifier key) {
return null;
}
}
Loading

0 comments on commit 20c5ede

Please sign in to comment.