-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This reverts commit 6328415.
- Loading branch information
Showing
12 changed files
with
644 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
.../src/main/java/org/apache/dubbo/admin/registry/metadata/impl/ConsulMetaDataCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
...r/src/main/java/org/apache/dubbo/admin/registry/metadata/impl/NacosMetaDataCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...er/src/main/java/org/apache/dubbo/admin/registry/metadata/impl/NoOpMetadataCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.