In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the principle and application of LocalConfigInfoProcessor in nacos". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the principle and application of LocalConfigInfoProcessor in nacos".
Order
This paper mainly studies the LocalConfigInfoProcessor of nacos.
LocalConfigInfoProcessor
Nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalConfigInfoProcessor.java
Public class LocalConfigInfoProcessor {private static final Logger LOGGER = LogUtils.logger (LocalConfigInfoProcessor.class); static public String getFailover (String serverName, String dataId, String group, String tenant) {File localPath = getFailoverFile (serverName, dataId, group, tenant); if (! localPath.exists () | |! localPath.isFile ()) {return null;} try {return readFile (localPath) } catch (IOException ioe) {LOGGER.error ("[" + serverName + "] get failover error," + localPath, ioe); return null;}} / * get the contents of the local cache file. NULL indicates that there are no local files or an exception is thrown. * / static public String getSnapshot (String name, String dataId, String group, String tenant) {if (! SnapShotSwitch.getIsSnapShot ()) {return null;} File file = getSnapshotFile (name, dataId, group, tenant); if (! file.exists () | |! file.isFile ()) {return null;} try {return readFile (file) } catch (IOException ioe) {LOGGER.error ("[" + name + "] + get snapshot error," + file, ioe); return null;}} static private String readFile (File file) throws IOException {if (! file.exists () | |! file.isFile ()) {return null } if (JVMUtil.isMultiInstance ()) {return ConcurrentDiskUtil.getFileContent (file, Constants.ENCODE);} else {InputStream is = null; try {is = new FileInputStream (file); return IOUtils.toString (is, Constants.ENCODE) } finally {try {if (null! = is) {is.close () } catch (IOException ioe) {}} static public void saveSnapshot (String envName, String dataId, String group, String tenant, String config) {if (! SnapShotSwitch.getIsSnapShot ()) {return;} File file = getSnapshotFile (envName, dataId, group, tenant) If (null = = config) {try {IOUtils.delete (file);} catch (IOException ioe) {LOGGER.error ("[" + envName + "] delete snapshot error," + file, ioe);}} else {try {File parentFile = file.getParentFile () If (! parentFile.exists ()) {boolean isMdOk = parentFile.mkdirs (); if (! isMdOk) {LOGGER.error ("[{}] save snapshot error", envName) }} if (JVMUtil.isMultiInstance ()) {ConcurrentDiskUtil.writeFileContent (file, config, Constants.ENCODE);} else {IOUtils.writeStringToFile (file, config, Constants.ENCODE) }} catch (IOException ioe) {LOGGER.error ("[" + envName + "] save snapshot error," + file, ioe);} / * clear all cache files in the snapshot directory. * / static public void cleanAllSnapshot () {try {File rootFile = new File (LOCAL_SNAPSHOT_PATH); File [] files = rootFile.listFiles (); if (files = = null | | files.length = = 0) {return } for (File file: files) {if (file.getName (). EndsWith ("_ nacos")) {IOUtils.cleanDirectory (file);} catch (IOException ioe) {LOGGER.error ("clean all snapshot error," + ioe.toString (), ioe) } static public void cleanEnvSnapshot (String envName) {File tmp = new File (LOCAL_SNAPSHOT_PATH, envName + "_ nacos"); tmp = new File (tmp, "snapshot"); try {IOUtils.cleanDirectory (tmp); LOGGER.info ("success delete" + envName + "- snapshot") } catch (IOException e) {LOGGER.info ("fail delete" + envName + "- snapshot," + e.toString ()); e.printStackTrace ();} static File getFailoverFile (String serverName, String dataId, String group, String tenant) {File tmp = new File (LOCAL_SNAPSHOT_PATH, serverName + "_ nacos"); tmp = new File (tmp, "data") If (StringUtils.isBlank (tenant)) {tmp = new File (tmp, "config-data");} else {tmp = new File (tmp, "config-data-tenant"); tmp = new File (tmp, tenant);} return new File (new File (tmp, group), dataId) } static File getSnapshotFile (String envName, String dataId, String group, String tenant) {File tmp = new File (LOCAL_SNAPSHOT_PATH, envName + "_ nacos"); if (StringUtils.isBlank (tenant)) {tmp = new File (tmp, "snapshot");} else {tmp = new File (tmp, "snapshot-tenant"); tmp = new File (tmp, tenant) } return new File (new File (tmp, group), dataId);} public static final String LOCAL_FILEROOT_PATH; public static final String LOCAL_SNAPSHOT_PATH; static {LOCAL_FILEROOT_PATH = System.getProperty ("JM.LOG.PATH", System.getProperty ("user.home")) + File.separator + "nacos" + File.separator + "config" LOCAL_SNAPSHOT_PATH = System.getProperty ("JM.SNAPSHOT.PATH", System.getProperty ("user.home") + File.separator + "nacos" + File.separator + "config"; LOGGER.info ("LOCAL_SNAPSHOT_PATH: {}", LOCAL_SNAPSHOT_PATH);}}
The getFailover method first gets the local configuration file through getFailoverFile and then reads it through readFile; the getSnapshot method first gets the snapshot file through getSnapshotFile and then reads it through readFile; the saveSnapshot method stores the new config;cleanAllSnapshot method and clears all cache files in the snapshot directory
CacheData
Nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java
Public class CacheData {/ /. Public CacheData (ConfigFilterChainManager configFilterChainManager, String name, String dataId, String group) {if (null = = dataId | | null = = group) {throw new IllegalArgumentException ("dataId=" + dataId + ", group=" + group);} this.name = name; this.configFilterChainManager = configFilterChainManager; this.dataId = dataId; this.group = group; this.tenant = TenantUtil.getUserTenantForAcm (); listeners = new CopyOnWriteArrayList () This.isInitializing = true; this.content = loadCacheContentFromDiskLocal (name, dataId, group, tenant); this.md5 = getMd5String (content);} public CacheData (ConfigFilterChainManager configFilterChainManager, String name, String dataId, String group, String tenant) {if (null = = dataId | | null = = group) {throw new IllegalArgumentException ("dataId=" + dataId + ", group=" + group);} this.name = name This.configFilterChainManager = configFilterChainManager; this.dataId = dataId; this.group = group; this.tenant = tenant; listeners = new CopyOnWriteArrayList (); this.isInitializing = true; this.content = loadCacheContentFromDiskLocal (name, dataId, group, tenant); this.md5 = getMd5String (content) } private String loadCacheContentFromDiskLocal (String name, String dataId, String group, String tenant) {String content = LocalConfigInfoProcessor.getFailover (name, dataId, group, tenant); content = (null! = content)? Content: LocalConfigInfoProcessor.getSnapshot (name, dataId, group, tenant); return content;} /.}
The constructor of CacheData loads cached data from the local disk through loadCacheContentFromDiskLocal; the loadCacheContentFromDiskLocal method gets the data through LocalConfigInfoProcessor.getFailover or LocalConfigInfoProcessor.getSnapshot
Summary
The getFailover method of LocalConfigInfoProcessor first gets the local configuration file through getFailoverFile and then reads it through readFile; the getSnapshot method first gets the snapshot file through getSnapshotFile and then reads it through readFile; the saveSnapshot method stores the new config;cleanAllSnapshot method and clears all cache files in the snapshot directory
Thank you for reading, the above is the content of "the principle and application of LocalConfigInfoProcessor in nacos". After the study of this article, I believe you have a deeper understanding of the principle and application of LocalConfigInfoProcessor in nacos, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.