Apollo(六)-源码解析-本地缓存文件位置

x33g5p2x  于2021-12-20 转载在 其他  
字(5.0k)|赞(0)|评价(0)|浏览(628)

前提

启动ConfigServiceApplication,否则无法拉取配置;

调试代码

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>helloworld</artifactId>
  7. <groupId>com.ydfind</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>apollo-configuration-demo</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>com.ctrip.framework.apollo</groupId>
  15. <artifactId>apollo-client</artifactId>
  16. <version>1.4.0</version>
  17. </dependency>
  18. <!-- 日志相关 -->
  19. <dependency>
  20. <groupId>org.apache.logging.log4j</groupId>
  21. <artifactId>log4j-api</artifactId>
  22. <version>2.11.0</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.apache.logging.log4j</groupId>
  26. <artifactId>log4j-slf4j-impl</artifactId>
  27. <version>2.11.0</version>
  28. </dependency>
  29. <!-- take over jcl -->
  30. <dependency>
  31. <groupId>org.slf4j</groupId>
  32. <artifactId>jcl-over-slf4j</artifactId>
  33. <version>1.7.25</version>
  34. </dependency>
  35. </dependencies>
  36. </project>

ApolloLocalFileDemo.java

  1. package com.ydfind.apollo;
  2. import com.ctrip.framework.apollo.Config;
  3. import com.ctrip.framework.apollo.ConfigService;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. public class ApolloLocalFileDemo {
  7. private static final Logger logger = LoggerFactory.getLogger(ApolloLocalFileDemo.class);
  8. private String DEFAULT_VALUE = "undefined";
  9. private Config config;
  10. public ApolloLocalFileDemo() {
  11. config = ConfigService.getAppConfig();
  12. }
  13. private String getConfig(String key) {
  14. String result = config.getProperty(key, DEFAULT_VALUE);
  15. logger.info(String.format("Loading key : %s with value: %s", key, result));
  16. return result;
  17. }
  18. public static void main(String[] args){
  19. System.setProperty("env", "dev");
  20. System.setProperty("dev_meta", "http://localhost:8080");
  21. ApolloLocalFileDemo apolloConfigDemo = new ApolloLocalFileDemo();
  22. apolloConfigDemo.getConfig("timeout");
  23. }
  24. }

log4j2.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration monitorInterval="60">
  3. <appenders>
  4. <Console name="Console" target="SYSTEM_OUT">
  5. <PatternLayout pattern="[%t]%d %-5p [%c] %m%n"/>
  6. </Console>
  7. <Async name="Async" includeLocation="true">
  8. <AppenderRef ref="Console"/>
  9. </Async>
  10. </appenders>
  11. <loggers>
  12. <logger name="com.ydfind.apollo" additivity="false" level="DEBUG">
  13. <AppenderRef ref="Async"/>
  14. </logger>
  15. <root level="INFO">
  16. <AppenderRef ref="Async"/>
  17. </root>
  18. </loggers>
  19. </configuration>

app.properties

  1. app.id=100004458

分析源码-缓存目录

Debug相关代码

先打断点确定主要的代码:

详细的函数调用情况如下所示:

配置文件的命名规则

分析源码

代码调用关系:findLocalCacheDir() -> LocalFileConfigRepository.findLocalCacheDir -> ConfigUtil.java

  1. private File findLocalCacheDir() {
  2. try {
  3. String defaultCacheDir = m_configUtil.getDefaultLocalCacheDir();
  4. Path path = Paths.get(defaultCacheDir);
  5. if (!Files.exists(path)) {
  6. Files.createDirectories(path);
  7. }
  8. if (Files.exists(path) && Files.isWritable(path)) {
  9. // private static final String CONFIG_DIR = "/config-cache";
  10. // 默认目录后面会加上/config-cache
  11. return new File(defaultCacheDir, CONFIG_DIR);
  12. }
  13. } catch (Throwable ex) {
  14. //ignore
  15. }
  16. return new File(ClassLoaderUtil.getClassPath(), CONFIG_DIR);
  17. }
  1. // ConfigUtil.java
  2. // 返回本地缓存目录
  3. public String getDefaultLocalCacheDir() {
  4. String cacheRoot = getCustomizedCacheRoot();
  5. if (!Strings.isNullOrEmpty(cacheRoot)) {
  6. return cacheRoot + File.separator + getAppId();
  7. }
  8. cacheRoot = isOSWindows() ? "C:\\opt\\data\\%s" : "/opt/data/%s";
  9. return String.format(cacheRoot, getAppId());
  10. }
  11. private String getCustomizedCacheRoot() {
  12. // 1. Get from System Property-----可以通过设置System的apollo.cacheDir制定缓存目录
  13. String cacheRoot = System.getProperty("apollo.cacheDir");
  14. if (Strings.isNullOrEmpty(cacheRoot)) {
  15. // 2. Get from OS environment variable
  16. cacheRoot = System.getenv("APOLLO_CACHEDIR");
  17. }
  18. if (Strings.isNullOrEmpty(cacheRoot)) {
  19. // 3. Get from server.properties-----配置文件设置apollo.cacheDir制定缓存目录
  20. cacheRoot = Foundation.server().getProperty("apollo.cacheDir", null);
  21. }
  22. return cacheRoot;
  23. }
  24. // 判断是否是windows操作系统
  25. public boolean isOSWindows() {
  26. String osName = System.getProperty("os.name");
  27. if (Strings.isNullOrEmpty(osName)) {
  28. return false;
  29. }
  30. return osName.startsWith("Windows");
  31. }

结论

  • 1)可以通过设置System(大多数是与程序有关)的apollo.cacheDir制定缓存目录
  1. System.setProperty("apollo.cacheDir", "C:\\ydfind\\data");

结果

  • 2)配置文件C:\opt\settings\server.properties设置apollo.cacheDir制定缓存目录
  1. apollo.cacheDir=C:\\ydfind\\data1

打断点可以看到目录正确设置了

3)getEnv系统相关变量,同理,这里不再尝试了。
4)文件拼接规则:设定目录\appid\config-cache\appid+cluster+namespace.properties

相关文章