Optimize some code of the dubbo metadata module (#8783)

* Optimize some code of the dubbo metadata module

* Optimize some code of the dubbo metadata module
This commit is contained in:
Wang Chengming 2021-09-20 22:04:00 +08:00 committed by GitHub
parent a1182923b9
commit feb53c3f6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View File

@ -65,13 +65,16 @@ import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.FILE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
import static org.apache.dubbo.common.utils.StringUtils.replace;
import static org.apache.dubbo.metadata.report.support.Constants.CACHE;
import static org.apache.dubbo.metadata.report.support.Constants.CYCLE_REPORT_KEY;
import static org.apache.dubbo.metadata.report.support.Constants.DEFAULT_METADATA_REPORT_CYCLE_REPORT;
import static org.apache.dubbo.metadata.report.support.Constants.DEFAULT_METADATA_REPORT_RETRY_PERIOD;
import static org.apache.dubbo.metadata.report.support.Constants.DEFAULT_METADATA_REPORT_RETRY_TIMES;
import static org.apache.dubbo.metadata.report.support.Constants.DUBBO_METADATA;
import static org.apache.dubbo.metadata.report.support.Constants.RETRY_PERIOD_KEY;
import static org.apache.dubbo.metadata.report.support.Constants.RETRY_TIMES_KEY;
import static org.apache.dubbo.metadata.report.support.Constants.SYNC_REPORT_KEY;
import static org.apache.dubbo.metadata.report.support.Constants.USER_HOME;
/**
*
@ -102,11 +105,9 @@ public abstract class AbstractMetadataReport implements MetadataReport {
public AbstractMetadataReport(URL reportServerURL) {
setUrl(reportServerURL);
// Start file save timer
String defaultFilename = System.getProperty("user.home") +
"/.dubbo/dubbo-metadata-" +
reportServerURL.getApplication() + "-" +
replace(reportServerURL.getAddress(), ":", "-") +
".cache";
String defaultFilename = System.getProperty(USER_HOME) + DUBBO_METADATA +
reportServerURL.getApplication() + "-" +
replace(reportServerURL.getAddress(), ":", "-") + CACHE;
String filename = reportServerURL.getParameter(FILE_KEY, defaultFilename);
File file = null;
if (ConfigUtils.isNotEmpty(filename)) {
@ -116,7 +117,7 @@ public abstract class AbstractMetadataReport implements MetadataReport {
throw new IllegalArgumentException("Invalid service store file " + file + ", cause: Failed to create directory " + file.getParentFile() + "!");
}
}
// if this file exist, firstly delete it.
// if this file exists, firstly delete it.
if (!initialized.getAndSet(true) && file.exists()) {
file.delete();
}
@ -125,7 +126,7 @@ public abstract class AbstractMetadataReport implements MetadataReport {
loadProperties();
syncReport = reportServerURL.getParameter(SYNC_REPORT_KEY, false);
metadataReportRetry = new MetadataReportRetry(reportServerURL.getParameter(RETRY_TIMES_KEY, DEFAULT_METADATA_REPORT_RETRY_TIMES),
reportServerURL.getParameter(RETRY_PERIOD_KEY, DEFAULT_METADATA_REPORT_RETRY_PERIOD));
reportServerURL.getParameter(RETRY_PERIOD_KEY, DEFAULT_METADATA_REPORT_RETRY_PERIOD));
// cycle report the data switch
if (reportServerURL.getParameter(CYCLE_REPORT_KEY, DEFAULT_METADATA_REPORT_CYCLE_REPORT)) {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("DubboMetadataReportTimer", true));

View File

@ -33,4 +33,10 @@ public interface Constants {
String CYCLE_REPORT_KEY = "cycle.report";
Boolean DEFAULT_METADATA_REPORT_CYCLE_REPORT = true;
String USER_HOME = "user.home";
String CACHE = ".cache";
String DUBBO_METADATA = "/.dubbo/dubbo-metadata-";
}