Skip to content

Commit

Permalink
【3.0】Enhance and fix check config (apache#8483)
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyHZM authored Aug 23, 2021
1 parent 1a466da commit acb96d4
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,51 +41,69 @@
* ConfigCenterConfig
*/
public class ConfigCenterConfig extends AbstractConfig {
private AtomicBoolean inited = new AtomicBoolean(false);
private final AtomicBoolean initialized = new AtomicBoolean(false);

private String protocol;
private String address;
private Integer port;

/* The config center cluster, it's real meaning may very on different Config Center products. */
/**
* The config center cluster, it's real meaning may very on different Config Center products.
*/
private String cluster;

/* The namespace of the config center, generally it's used for multi-tenant,
but it's real meaning depends on the actual Config Center you use.
*/
/**
* The namespace of the config center, generally it's used for multi-tenant,
* but it's real meaning depends on the actual Config Center you use.
* The default value is CommonConstants.DUBBO
*/
private String namespace;

private String namespace; // CommonConstants.DUBBO;
/* The group of the config center, generally it's used to identify an isolated space for a batch of config items,
but it's real meaning depends on the actual Config Center you use.
*/
private String group; // CommonConstants.DUBBO;
/**
* The group of the config center, generally it's used to identify an isolated space for a batch of config items,
* but it's real meaning depends on the actual Config Center you use.
* The default value is CommonConstants.DUBBO
*/
private String group;
private String username;
private String password;
private Long timeout; // 3000L;

/**
* The default value is 3000L;
*/
private Long timeout;

/**
* If the Config Center is given the highest priority, it will override all the other configurations
* The default value is true
* @deprecated no longer used
*/
private Boolean highestPriority; // true;
private Boolean highestPriority;

// Decide the behaviour when initial connection try fails, 'true' means interrupt the whole process once fail.
private Boolean check; // true;
/**
* Decide the behaviour when initial connection try fails, 'true' means interrupt the whole process once fail.
* The default value is true
*/
private Boolean check;

/* Used to specify the key that your properties file mapping to, most of the time you do not need to change this parameter.
Notice that for Apollo, this parameter is meaningless, set the 'namespace' is enough.
*/
private String configFile; // CommonConstants.DEFAULT_DUBBO_PROPERTIES;
/**
* Used to specify the key that your properties file mapping to, most of the time you do not need to change this parameter.
* Notice that for Apollo, this parameter is meaningless, set the 'namespace' is enough.
* The default value is CommonConstants.DEFAULT_DUBBO_PROPERTIES
*/
private String configFile;

/* the .properties file under 'configFile' is global shared while .properties under this one is limited only to this application
*/
/**
* the properties file under 'configFile' is global shared while .properties under this one is limited only to this application
*/
private String appConfigFile;

/* If the Config Center product you use have some special parameters that is not covered by this class, you can add it to here.
For example, with XML:
<dubbo:config-center>
<dubbo:parameter key="{your key}" value="{your value}" />
</dubbo:config-center>
/**
* If the Config Center product you use have some special parameters that is not covered by this class, you can add it to here.
* For example, with XML:
* <dubbo:config-center>
* <dubbo:parameter key="{your key}" value="{your value}" />
* </dubbo:config-center>
*/
private Map<String, String> parameters;

Expand Down Expand Up @@ -127,15 +145,15 @@ public URL toUrl() {
address = ANYHOST_VALUE;
}
map.put(PATH_KEY, ConfigCenterConfig.class.getSimpleName());
// use 'zookeeper' as the default configcenter.
// use 'zookeeper' as the default config center.
if (StringUtils.isEmpty(map.get(PROTOCOL_KEY))) {
map.put(PROTOCOL_KEY, ZOOKEEPER_PROTOCOL);
}
return UrlUtils.parseURL(address, map);
}

public boolean checkOrUpdateInited() {
return inited.compareAndSet(false, true);
public boolean checkOrUpdateInitialized(boolean update) {
return initialized.compareAndSet(false, update);
}

public Map<String, String> getExternalConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,46 @@ public class MetadataReportConfig extends AbstractConfig {

private String protocol;

// metadata center address
/**
* metadata center address
*/
private String address;

/**
* Default port for metadata center
*/
private Integer port;

// Username to login metadata center
/**
* Username to login metadata center
*/
private String username;

// Password to login metadata center
/**
* Password to login metadata center
*/
private String password;

// Request timeout in milliseconds for metadata center
/**
* Request timeout in milliseconds for metadata center
*/
private Integer timeout;

/**
* The group the metadata in . It is the same as registry
*/
private String group;

// Customized parameters
/**
* Customized parameters
*/
private Map<String, String> parameters;

private Integer retryTimes;

private Integer retryPeriod;
/**
* By default the metadatastore will store full metadata repeatedly every day .
* By default the metadata store will store full metadata repeatedly every day .
*/
private Boolean cycleReport;

Expand All @@ -93,6 +103,13 @@ public class MetadataReportConfig extends AbstractConfig {
*/
private String file;

/**
* Decide the behaviour when initial connection try fails,
* 'true' means interrupt the whole process once fail.
* The default value is true
*/
private Boolean check;


public MetadataReportConfig() {
}
Expand Down Expand Up @@ -280,4 +297,12 @@ public void updateParameters(Map<String, String> parameters) {
this.parameters.putAll(parameters);
}
}

public Boolean isCheck() {
return check;
}

public void setCheck(Boolean check) {
this.check = check;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ private void initMetadataService() {
*/
public synchronized DubboBootstrap start() {
// avoid re-entry start method multiple times in same thread
if (isCurrentlyInStart){
if (isCurrentlyInStart) {
return this;
}

Expand Down Expand Up @@ -1330,10 +1330,23 @@ private ConsumerBuilder createConsumerBuilder(String id) {

private DynamicConfiguration prepareEnvironment(ConfigCenterConfig configCenter) {
if (configCenter.isValid()) {
if (!configCenter.checkOrUpdateInited()) {
if (!configCenter.checkOrUpdateInitialized(true)) {
return null;
}
DynamicConfiguration dynamicConfiguration = getDynamicConfiguration(configCenter.toUrl());

DynamicConfiguration dynamicConfiguration = null;
try {
dynamicConfiguration = getDynamicConfiguration(configCenter.toUrl());
} catch (Exception e) {
if (!configCenter.isCheck()) {
logger.warn("The configuration center failed to initialize", e);
configCenter.checkOrUpdateInitialized(false);
return null;
} else {
throw new IllegalStateException(e);
}
}

String configContent = dynamicConfiguration.getProperties(configCenter.getConfigFile(), configCenter.getGroup());

String appGroup = getApplication().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,41 @@
*/
public class MetadataReportBuilder extends AbstractBuilder<MetadataReportConfig, MetadataReportBuilder> {

// Register center address
/**
* Register center address
*/
private String address;

// Username to login register center
/**
* Username to login register center
*/
private String username;

// Password to login register center
/**
* Password to login register center
*/
private String password;

// Request timeout in milliseconds for register center
/**
* Request timeout in milliseconds for register center
*/
private Integer timeout;

/**
* The group the metadata in . It is the same as registry
*/
private String group;

// Customized parameters
/**
* Customized parameters
*/
private Map<String, String> parameters;

private Integer retryTimes;

private Integer retryPeriod;
/**
* By default the metadatastore will store full metadata repeatedly every day .
* By default the metadata store will store full metadata repeatedly every day .
*/
private Boolean cycleReport;

Expand All @@ -60,6 +70,13 @@ public class MetadataReportBuilder extends AbstractBuilder<MetadataReportConfig,
*/
private Boolean syncReport;

/**
* Decide the behaviour when initial connection try fails,
* 'true' means interrupt the whole process once fail.
* The default value is true
*/
private Boolean check;

public static MetadataReportBuilder newBuilder() {
return new MetadataReportBuilder();
}
Expand Down Expand Up @@ -119,6 +136,12 @@ public MetadataReportBuilder syncReport(Boolean syncReport) {
return getThis();
}

public MetadataReportBuilder check(Boolean check) {
this.check = check;
return getThis();
}

@Override
public MetadataReportConfig build() {
MetadataReportConfig metadataReport = new MetadataReportConfig();
super.build(metadataReport);
Expand All @@ -133,6 +156,7 @@ public MetadataReportConfig build() {
metadataReport.setRetryPeriod(retryPeriod);
metadataReport.setCycleReport(cycleReport);
metadataReport.setSyncReport(syncReport);
metadataReport.setCheck(check);

return metadataReport;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@
</xsd:attribute>
<xsd:attribute name="check" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ Check registry status on stratup. ]]></xsd:documentation>
<xsd:documentation><![CDATA[ Check registry status on startup. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="dynamic" type="xsd:string">
Expand Down Expand Up @@ -737,6 +737,12 @@
<xsd:documentation><![CDATA[ The metadata report file store. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="check" type="xsd:boolean" use="optional">
<xsd:annotation>
<xsd:documentation>
<![CDATA[ The policy to apply when connecting to metadata center fails. ]]></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 @@ -49,15 +49,18 @@ public static void init(MetadataReportConfig config) {
if (METADATA_REPORT_KEY.equals(url.getProtocol())) {
String protocol = url.getParameter(METADATA_REPORT_KEY, DEFAULT_DIRECTORY);
url = URLBuilder.from(url)
.setProtocol(protocol)
.removeParameter(METADATA_REPORT_KEY)
.build();
.setProtocol(protocol)
.removeParameter(METADATA_REPORT_KEY)
.build();
}
url = url.addParameterIfAbsent(APPLICATION_KEY, ApplicationModel.getApplicationConfig().getName());
String relatedRegistryId = config.getRegistry() == null ? DEFAULT_KEY : config.getRegistry();
// RegistryConfig registryConfig = ApplicationModel.getConfigManager().getRegistry(relatedRegistryId)
// .orElseThrow(() -> new IllegalStateException("Registry id " + relatedRegistryId + " does not exist."));
metadataReports.put(relatedRegistryId, metadataReportFactory.getMetadataReport(url));
MetadataReport metadataReport = metadataReportFactory.getMetadataReport(url);
if (metadataReport != null) {
metadataReports.put(relatedRegistryId, metadataReport);
}
init.set(true);
}

Expand All @@ -80,7 +83,7 @@ public static MetadataReport getMetadataReport(String registryKey) {

private static void checkInit() {
if (!init.get()) {
throw new IllegalStateException("the metadata report was not inited.");
throw new IllegalStateException("the metadata report was not initialized.");
}
}

Expand Down
Loading

0 comments on commit acb96d4

Please sign in to comment.